|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Modulus\Console\Commands; |
| 4 | + |
| 5 | +use ReflectionClass; |
| 6 | +use Modulus\Utility\Plugin; |
| 7 | +use Modulus\Console\ModulusCLI; |
| 8 | +use Modulus\Support\Filesystem; |
| 9 | +use AtlantisPHP\Console\Command; |
| 10 | +use Modulus\Framework\Plugin\Validate; |
| 11 | +use Symfony\Component\Console\Input\InputInterface; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | + |
| 14 | +class PluginInstall extends Command |
| 15 | +{ |
| 16 | + /** |
| 17 | + * The name and signature of the console command. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected $signature = 'plugin:install {--class=}'; |
| 22 | + |
| 23 | + /** |
| 24 | + * The full command description. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $help = 'Verify and install a new plugin'; |
| 29 | + |
| 30 | + /** |
| 31 | + * The descriptions of the console commands. |
| 32 | + * |
| 33 | + * @var array |
| 34 | + */ |
| 35 | + protected $descriptions = [ |
| 36 | + 'plugin:install' => 'Verify and install a new plugin' |
| 37 | + ]; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param InputInterface $input |
| 41 | + * @param OutputInterface $output |
| 42 | + * |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 46 | + { |
| 47 | + return $this->install($input, $output); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Install plugin |
| 52 | + * |
| 53 | + * @param InputInterface $input |
| 54 | + * @param OutputInterface $output |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + private function install(InputInterface $input, OutputInterface $output) |
| 58 | + { |
| 59 | + $plugin = $this->getPlugin($input); |
| 60 | + |
| 61 | + $pluginInfo = $this->getInfo($plugin); |
| 62 | + |
| 63 | + if (Validate::check($plugin, $pluginInfo)) { |
| 64 | + |
| 65 | + $package = $plugin::PACKAGE; |
| 66 | + $version = $plugin::VERSION; |
| 67 | + |
| 68 | + if (config('app.plugins') && in_array($pluginInfo->name, config('app.plugins'))) { |
| 69 | + return $output->writeln("<error>{$package} is a registered plugin. Aborting...</error>"); |
| 70 | + } |
| 71 | + |
| 72 | + $output->writeln("<info>Installing: {$package} {$version}</info>"); |
| 73 | + |
| 74 | + if (!$this->addConfig($plugin, $pluginInfo)) { |
| 75 | + throw new \Exception('Could not add config file'); |
| 76 | + } |
| 77 | + |
| 78 | + if (!$this->addMigration($plugin, $pluginInfo)) { |
| 79 | + throw new \Exception('Could not add migration file'); |
| 80 | + } |
| 81 | + |
| 82 | + return $output->writeln("<info>Installed: {$package} {$version}. Append \"{$pluginInfo->name}::class\" in your plugins</info>"); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + $output->writeln("<error>Failed: {$package} {$version}</error>"); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get plugin instance |
| 91 | + * |
| 92 | + * @param InputInterface $input |
| 93 | + * @return string |
| 94 | + */ |
| 95 | + private function getPlugin(InputInterface $input) : Plugin |
| 96 | + { |
| 97 | + $class = $input->getOption('class'); |
| 98 | + |
| 99 | + if (!class_exists($class)) { |
| 100 | + throw new \Exception("Plugin {$class} does not exist"); |
| 101 | + } |
| 102 | + |
| 103 | + $plugin = new $class; |
| 104 | + |
| 105 | + if (!$plugin instanceof Plugin) { |
| 106 | + throw new \Exception("{$class} is not a Modulus Plugin"); |
| 107 | + } |
| 108 | + |
| 109 | + return $plugin; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get plugin information |
| 114 | + * |
| 115 | + * @param Plugin $plugin |
| 116 | + * @return ReflectionClass |
| 117 | + */ |
| 118 | + private function getInfo(Plugin $plugin) : ReflectionClass |
| 119 | + { |
| 120 | + return new ReflectionClass($plugin); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Add plugin config |
| 125 | + * |
| 126 | + * @param Plugin $plugin |
| 127 | + * @param ReflectionClass $pluginInfo |
| 128 | + * @return bool |
| 129 | + */ |
| 130 | + private function addConfig(Plugin $plugin, ReflectionClass $pluginInfo) : bool |
| 131 | + { |
| 132 | + $pluginConfig = dirname($pluginInfo->getFileName()) . DS . '..' . DS . 'install' . DS . 'config.php'; |
| 133 | + |
| 134 | + if ( |
| 135 | + array_key_exists('CONFIG', $pluginInfo->getConstants()) && |
| 136 | + file_exists($pluginConfig) |
| 137 | + ) { |
| 138 | + $appConfig = config('app.dir') . 'config' . DS . $plugin::CONFIG . '.php'; |
| 139 | + |
| 140 | + if (file_exists($appConfig)) return false; |
| 141 | + |
| 142 | + $pluginConfig = Filesystem::get($pluginConfig); |
| 143 | + |
| 144 | + return Filesystem::put($appConfig, $pluginConfig); |
| 145 | + } |
| 146 | + |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Add plugin migration |
| 152 | + * |
| 153 | + * @param Plugin $plugin |
| 154 | + * @param ReflectionClass $pluginInfo |
| 155 | + * @return bool |
| 156 | + */ |
| 157 | + private function addMigration(Plugin $plugin, ReflectionClass $pluginInfo) : bool |
| 158 | + { |
| 159 | + $pluginMigration = dirname($pluginInfo->getFileName()) . DS . '..' . DS . 'install' . DS . 'migration.php'; |
| 160 | + |
| 161 | + if ( |
| 162 | + array_key_exists('MIGRATION', $pluginInfo->getConstants()) && |
| 163 | + file_exists($pluginMigration) |
| 164 | + ) { |
| 165 | + $name = $plugin::MIGRATION; |
| 166 | + |
| 167 | + $class = implode('', array_map('ucfirst', explode('_', $name))); |
| 168 | + |
| 169 | + return $this->add($name, $class, Filesystem::get($pluginMigration)); |
| 170 | + } |
| 171 | + |
| 172 | + return true; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Add asset |
| 177 | + * |
| 178 | + * @param string $name |
| 179 | + * @param string $class |
| 180 | + * @param string $content |
| 181 | + * @return bool |
| 182 | + */ |
| 183 | + private function add(string $name, string $class, string $content) : bool |
| 184 | + { |
| 185 | + $migrations = ModulusCLI::$appdir . 'database' . DIRECTORY_SEPARATOR . 'migrations'; |
| 186 | + $date = date('Y_m_d_H_i_s'); |
| 187 | + $migration = $migrations . DIRECTORY_SEPARATOR . $date . '_' . $name . '.php'; |
| 188 | + |
| 189 | + ModulusCLI::_dir($migrations); |
| 190 | + |
| 191 | + if (file_exists($migration)) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + |
| 195 | + return file_put_contents($migration, $content); |
| 196 | + } |
| 197 | +} |
0 commit comments