|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Somnambulist\ProjectManager\Commands; |
| 4 | + |
| 5 | +use Somnambulist\ProjectManager\Commands\Behaviours\ProjectConfigAwareCommand; |
| 6 | +use Somnambulist\ProjectManager\Contracts\ProjectConfigAwareInterface; |
| 7 | +use Somnambulist\ProjectManager\Models\Template; |
| 8 | +use Symfony\Component\Console\Helper\Table; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use function file_exists; |
| 13 | +use function file_get_contents; |
| 14 | +use function filesize; |
| 15 | +use function is_dir; |
| 16 | +use function trim; |
| 17 | +use const DIRECTORY_SEPARATOR; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class CheckCommand |
| 21 | + * |
| 22 | + * @package Somnambulist\ProjectManager\Commands |
| 23 | + * @subpackage Somnambulist\ProjectManager\Commands\CheckCommand |
| 24 | + */ |
| 25 | +class CheckCommand extends AbstractCommand implements ProjectConfigAwareInterface |
| 26 | +{ |
| 27 | + |
| 28 | + use ProjectConfigAwareCommand; |
| 29 | + |
| 30 | + protected function configure() |
| 31 | + { |
| 32 | + $this |
| 33 | + ->setName('check') |
| 34 | + ->setDescription('Check spm configuration for issues to help with debugging') |
| 35 | + ->addOption('debug', 'd', InputOption::VALUE_NONE, 'Display debug information with check data') |
| 36 | + ; |
| 37 | + } |
| 38 | + |
| 39 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 40 | + { |
| 41 | + $this->setupConsoleHelper($input, $output); |
| 42 | + |
| 43 | + $dir = $_SERVER['SOMNAMBULIST_PROJECTS_CONFIG_DIR']; |
| 44 | + $cfg = $dir . DIRECTORY_SEPARATOR . 'project_manager.yaml'; |
| 45 | + $env = $dir . DIRECTORY_SEPARATOR . '.env'; |
| 46 | + |
| 47 | + $this->tools()->info('spm config dir: <info>%s</info>', $dir); |
| 48 | + $this->tools()->info('spm config dir exists: <info>%s</info>', (is_dir($dir) ? 'yes' : 'no')); |
| 49 | + $this->tools()->info('spm config file: <info>%s</info>', $cfg); |
| 50 | + $this->tools()->info('spm config file initialised: <info>%s</info>', (0 !== filesize($cfg) ? 'yes' : 'no')); |
| 51 | + $this->tools()->info('spm config file exists: <info>%s</info>', (file_exists($cfg) ? 'yes' : 'no')); |
| 52 | + |
| 53 | + if (!file_exists($cfg) || 0 === filesize($cfg)) { |
| 54 | + $this->tools()->warning('config file has not been properly initialised, re-run <info>spm init</info>'); |
| 55 | + $this->tools()->newline(); |
| 56 | + |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + if ($input->getOption('debug') && file_exists($cfg)) { |
| 61 | + $this->tools()->info('spm config'); |
| 62 | + $this->tools()->message(trim(file_get_contents($cfg))); |
| 63 | + } |
| 64 | + |
| 65 | + $this->tools()->newline(); |
| 66 | + $this->tools()->info('spm environment file: <info>%s</info>', $env); |
| 67 | + $this->tools()->info('spm environment file exists: <info>%s</info>', (file_exists($env) ? 'yes' : 'no')); |
| 68 | + |
| 69 | + if ($input->getOption('debug') && file_exists($env)) { |
| 70 | + $this->tools()->info('spm environment'); |
| 71 | + $this->tools()->message(trim(file_get_contents($env))); |
| 72 | + } |
| 73 | + |
| 74 | + $this->tools()->newline(); |
| 75 | + $info = $this->config->active() ? sprintf('(includes templates for <info>%s</info>)', $this->config->active()) : ''; |
| 76 | + |
| 77 | + $this->tools()->info('configured templates %s', $info); |
| 78 | + |
| 79 | + $table = new Table($output); |
| 80 | + $table->setHeaders(['Name', 'Type', 'Source']); |
| 81 | + |
| 82 | + $this->config->templates()->list()->each(function (Template $template) use ($table) { |
| 83 | + $table->addRow([$template->name(), $template->type(), $template->source() ?: '~']); |
| 84 | + }); |
| 85 | + |
| 86 | + $table->render(); |
| 87 | + $this->tools()->newline(); |
| 88 | + |
| 89 | + return 0; |
| 90 | + } |
| 91 | +} |
0 commit comments