*/ namespace Flasher\Laravel\Command; use Flasher\Laravel\Support\ServiceProvider as FlasherServiceProvider; use Flasher\Prime\Plugin\PluginInterface; use Flasher\Laravel\Bridge\Command\FlasherCommand; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\App; use Illuminate\Support\ServiceProvider; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class InstallCommand extends FlasherCommand { /** * @return void */ protected function configure() { $this ->setName('flasher:install') ->setDescription('Installs all PHPFlasher resources to the public and config directories.') ->setHelp('The command copies PHPFlasher assets to public/vendor/flasher/ directory and config files to the config/ directory without overwriting any existing config files.'); } /** * @return int */ protected function flasherExecute(InputInterface $input, OutputInterface $output) { $output->writeln(''); $output->writeln(' ██████╗ ██╗ ██╗██████╗ ███████╗██╗ █████╗ ███████╗██╗ ██╗███████╗██████╗ ██╔══██╗██║ ██║██╔══██╗██╔════╝██║ ██╔══██╗██╔════╝██║ ██║██╔════╝██╔══██╗ ██████╔╝███████║██████╔╝█████╗ ██║ ███████║███████╗███████║█████╗ ██████╔╝ ██╔═══╝ ██╔══██║██╔═══╝ ██╔══╝ ██║ ██╔══██║╚════██║██╔══██║██╔══╝ ██╔══██╗ ██║ ██║ ██║██║ ██║ ███████╗██║ ██║███████║██║ ██║███████╗██║ ██║ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ '); $output->writeln(''); $output->writeln(''); $output->writeln(' INFO Copying PHPFlasher resources...'); $output->writeln(''); $publicDir = App::publicPath().'/vendor/flasher/'; $exitCode = 0; foreach (ServiceProvider::publishableProviders() as $provider) { if (!is_a($provider, 'Flasher\Laravel\Support\ServiceProvider', true)) { continue; } /** @var FlasherServiceProvider $provider */ $provider = App::getProvider($provider); $plugin = $provider->createPlugin(); $configFile = $provider->getConfigurationFile(); try { $this->publishAssets($plugin, $publicDir); $this->publishConfig($plugin, $configFile); $status = sprintf('%s', '\\' === \DIRECTORY_SEPARATOR ? 'OK' : "\xE2\x9C\x94" /* HEAVY CHECK MARK (U+2714) */); $output->writeln(sprintf(' %s %s', $status, $plugin->getAlias())); } catch (\Exception $e) { $exitCode = 1; $status = sprintf('%s', '\\' === \DIRECTORY_SEPARATOR ? 'ERROR' : "\xE2\x9C\x98" /* HEAVY BALLOT X (U+2718) */); $output->writeln(sprintf(' %s %s %s', $status, $plugin->getAlias(), $e->getMessage())); } } $output->writeln(''); if (0 === $exitCode) { $output->writeln(' SUCCESS PHPFlasher resources have been successfully installed.'); } else { $output->writeln(' ERROR An error occurred during the installation of PHPFlasher resources.'); } $output->writeln(''); return $exitCode; } /** * @param string $publicDir * * @return void */ private function publishAssets(PluginInterface $plugin, $publicDir) { $originDir = $plugin->getAssetsDir(); if (!is_dir($originDir)) { return; } $filesystem = new Filesystem(); $filesystem->ensureDirectoryExists($originDir, 0777); $filesystem->copyDirectory($originDir, $publicDir); } /** * @param string $configFile * * @return void */ private function publishConfig(PluginInterface $plugin, $configFile) { if (!file_exists($configFile)) { return; } $target = App::configPath($plugin->getName().'.php'); if (file_exists($target)) { return; } $filesystem = new Filesystem(); $filesystem->copy($configFile, $target); } }