LippertComponents /
Orchestrator
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LCI\MODX\Orchestrator\Console; |
||
| 4 | |||
| 5 | use LCI\MODX\Console\Application; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use LCI\MODX\Console\Command\PackageCommands; |
||
| 7 | use LCI\MODX\Console\Console; |
||
| 8 | |||
| 9 | class ActivePackageCommands implements PackageCommands |
||
| 10 | { |
||
| 11 | /** @var Console */ |
||
| 12 | protected $console; |
||
| 13 | |||
| 14 | /** @var array */ |
||
| 15 | protected $commands = [ |
||
| 16 | 'orchestrator_installed' => [ |
||
| 17 | 'LCI\MODX\Orchestrator\Console\Command\DeployCommand', |
||
| 18 | 'LCI\MODX\Orchestrator\Console\Command\InstallPackages', |
||
| 19 | 'LCI\MODX\Orchestrator\Console\Command\UninstallPackages', |
||
| 20 | 'LCI\MODX\Orchestrator\Console\Command\ListMODXPackages', |
||
| 21 | 'LCI\MODX\Orchestrator\Console\Command\RequireMODXPackages', |
||
| 22 | ], |
||
| 23 | 'orchestrator_not_installed' => [ |
||
| 24 | 'LCI\MODX\Orchestrator\Console\Command\Package' |
||
| 25 | ] |
||
| 26 | ]; |
||
| 27 | |||
| 28 | public function __construct(Console $console) |
||
| 29 | { |
||
| 30 | $this->console = $console; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @return array ~ of Fully qualified names of all command class |
||
| 35 | */ |
||
| 36 | public function getAllCommands() |
||
| 37 | { |
||
| 38 | $all_commands = []; |
||
| 39 | foreach ($this->commands as $group => $commands) { |
||
| 40 | foreach ($commands as $command) { |
||
| 41 | $all_commands[] = $command; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | return $all_commands; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return array ~ of Fully qualified names of active command classes. This could differ from all if package creator |
||
| 50 | * has different commands based on the state like the DB. Example has Install and Uninstall, only one would |
||
| 51 | * be active/available depending on the state |
||
| 52 | */ |
||
| 53 | public function getActiveCommands() |
||
| 54 | { |
||
| 55 | $active_commands = []; |
||
| 56 | |||
| 57 | if ($this->console->isModxInstalled()) { |
||
|
0 ignored issues
–
show
|
|||
| 58 | |||
| 59 | if ($this->isOrchestratorInstalled() && !$this->isOrchestratorRequireUpdate()) { |
||
| 60 | $commands = $this->commands['orchestrator_installed']; |
||
| 61 | foreach ($commands as $command) { |
||
| 62 | $active_commands[] = $command; |
||
| 63 | } |
||
| 64 | } else { |
||
| 65 | $commands = $this->commands['orchestrator_not_installed']; |
||
| 66 | foreach ($commands as $command) { |
||
| 67 | $active_commands[] = $command; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
|
0 ignored issues
–
show
|
|||
| 71 | } |
||
| 72 | |||
| 73 | return $active_commands; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param \LCI\MODX\Console\Application $application |
||
| 78 | * @return \LCI\MODX\Console\Application |
||
| 79 | */ |
||
| 80 | public function loadActiveCommands(Application $application) |
||
| 81 | { |
||
| 82 | $commands = $this->getActiveCommands(); |
||
| 83 | |||
| 84 | foreach ($commands as $command) { |
||
| 85 | $class = new $command(); |
||
| 86 | |||
| 87 | if (is_object($class) ) { |
||
|
0 ignored issues
–
show
|
|||
| 88 | if (method_exists($class, 'setConsole')) { |
||
| 89 | $class->setConsole($this->console); |
||
| 90 | } |
||
| 91 | |||
| 92 | $application->add($class); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | return $application; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | public function isOrchestratorInstalled() |
||
| 103 | { |
||
| 104 | $modx = $this->console->loadMODX(); |
||
| 105 | if (!empty($modx->getOption('orchestrator.vendor_path'))) { |
||
| 106 | return true; |
||
| 107 | } |
||
| 108 | |||
| 109 | return false; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | public function isOrchestratorRequireUpdate() |
||
| 116 | { |
||
| 117 | return false; |
||
| 118 | } |
||
| 119 | } |
||
| 120 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: