Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 10, 2022. It is now read-only.

Commit 1a623a2

Browse files
InitController::$commands now allows usage of callable
1 parent ebb40e0 commit 1a623a2

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Yii 2 Install extension Change Log
44
1.0.1 under development
55
-----------------------
66

7+
- Enh #3: `InitController::$commands` now allows usage of callable (klimov-paul)
78
- Bug #2: `InitController::actionRequirements()` prevents installation on warning in case requirements checking is performed by output analyzes (klimov-paul)
89

910

‎InitController.php‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class InitController extends Controller
119119
public $requirementsFileName = '@app/requirements.php';
120120
/**
121121
* @var array list of shell commands, which should be executed during project installation.
122+
* If command is a string it will be executed as shell command, otherwise as PHP callback.
122123
* For example:
123124
*
124125
* ```php
@@ -545,11 +546,23 @@ public function actionCommands()
545546
return self::EXIT_CODE_NORMAL;
546547
}
547548

548-
if ($this->confirm("Following commands will be executed:\n" . implode("\n", $this->commands) . "\nDo you wish to proceed?")) {
549-
foreach ($this->commands as $command) {
550-
$this->log($command . "\n");
551-
exec($command, $outputRows);
552-
$this->log(implode("\n", $outputRows));
549+
$commandTitles = [];
550+
foreach ($this->commands as $key => $command) {
551+
if (is_string($command)) {
552+
$commandTitles[$key] = $command;
553+
} else {
554+
$commandTitles[$key] = 'Unknown (Closure)';
555+
}
556+
}
557+
if ($this->confirm("Following commands will be executed:\n" . implode("\n", $commandTitles) . "\nDo you wish to proceed?")) {
558+
foreach ($this->commands as $key => $command) {
559+
$this->log($commandTitles[$key] . "\n");
560+
if (is_string($command)) {
561+
exec($command, $outputRows);
562+
$this->log(implode("\n", $outputRows));
563+
} else {
564+
$this->log(call_user_func($command));
565+
}
553566
$this->log("\n");
554567
}
555568
}

‎tests/InitControllerTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,21 @@ public function testActionRequirementsFromOutput()
399399
$runResult = $consoleCommand->actionRequirements(false);
400400
$this->assertEquals(0, $runResult, 'Requirements check failed for warning requirements!');
401401
}
402+
403+
public function testActionCommands()
404+
{
405+
$consoleCommand = $this->createController();
406+
407+
$object = new \stdClass();
408+
$object->value = '';
409+
$consoleCommand->commands = [
410+
function () use ($object) {
411+
$object->value = 'callback';
412+
}
413+
];
414+
415+
$consoleCommand->actionCommands();
416+
417+
$this->assertEquals('callback', $object->value);
418+
}
402419
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /