| Total Complexity | 23 |
| Total Lines | 139 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 24 | class AbstractCommand extends Command |
||
| 25 | { |
||
| 26 | const CONFIG_FILE = 'config.yml'; |
||
| 27 | const TMP_DIR = '.cecil'; |
||
| 28 | |||
| 29 | /** @var InputInterface */ |
||
| 30 | protected $input; |
||
| 31 | |||
| 32 | /** @var OutputInterface */ |
||
| 33 | protected $output; |
||
| 34 | |||
| 35 | /** @var SymfonyStyle */ |
||
| 36 | protected $io; |
||
| 37 | |||
| 38 | /** @var Filesystem */ |
||
| 39 | protected $fs; |
||
| 40 | |||
| 41 | /** @var string */ |
||
| 42 | protected $path; |
||
| 43 | |||
| 44 | /** @var array */ |
||
| 45 | protected $configFiles; |
||
| 46 | |||
| 47 | /** @var Builder */ |
||
| 48 | protected $builder; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 54 | { |
||
| 55 | $this->input = $input; |
||
| 56 | $this->output = $output; |
||
| 57 | $this->io = new SymfonyStyle($input, $output); |
||
| 58 | $this->fs = new Filesystem(); |
||
| 59 | |||
| 60 | if (!in_array($this->getName(), ['self-update'])) { |
||
| 61 | // working directory |
||
| 62 | $this->path = getcwd(); |
||
| 63 | if ($input->getArgument('path') !== null) { |
||
| 64 | $this->path = (string) $input->getArgument('path'); |
||
| 65 | } |
||
| 66 | if (realpath($this->getPath()) === false) { |
||
| 67 | $this->fs->mkdir($this->getPath()); |
||
| 68 | } |
||
| 69 | $this->path = realpath($this->getPath()); |
||
| 70 | // config file(s) |
||
| 71 | if (!in_array($this->getName(), ['new:site'])) { |
||
| 72 | // default |
||
| 73 | $this->configFiles[self::CONFIG_FILE] = realpath(Util::joinFile($this->getPath(), self::CONFIG_FILE)); |
||
| 74 | // from --config=<file> |
||
| 75 | if ($input->hasOption('config') && $input->getOption('config') !== null) { |
||
| 76 | foreach (explode(',', (string) $input->getOption('config')) as $configFile) { |
||
| 77 | $this->configFiles[$configFile] = realpath($configFile); |
||
| 78 | if (!Util\File::getFS()->isAbsolutePath($configFile)) { |
||
| 79 | $this->configFiles[$configFile] = realpath(Util::joinFile($this->getPath(), $configFile)); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | // checks file(s) |
||
| 84 | foreach ($this->configFiles as $fileName => $filePath) { |
||
| 85 | if (!file_exists($filePath)) { |
||
| 86 | $this->getBuilder()->getLogger()->error(\sprintf('Could not find configuration file "%s": uses others/default.', $fileName)); |
||
| 87 | unset($this->configFiles[$fileName]); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | parent::initialize($input, $output); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function run(InputInterface $input, OutputInterface $output) |
||
| 100 | { |
||
| 101 | if ($output->isDebug()) { |
||
| 102 | putenv('CECIL_DEBUG=true'); |
||
| 103 | |||
| 104 | return parent::run($input, $output); |
||
| 105 | } |
||
| 106 | // simplified error message |
||
| 107 | try { |
||
| 108 | return parent::run($input, $output); |
||
| 109 | } catch (\Exception $e) { |
||
| 110 | if ($this->io === null) { |
||
| 111 | $this->io = new SymfonyStyle($input, $output); |
||
| 112 | } |
||
| 113 | $this->io->error($e->getMessage()); |
||
| 114 | |||
| 115 | exit(1); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the working directory. |
||
| 121 | */ |
||
| 122 | protected function getPath(): ?string |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns config file(s) path. |
||
| 129 | */ |
||
| 130 | protected function getConfigFiles(): array |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Creates or returns a Builder instance. |
||
| 137 | */ |
||
| 138 | protected function getBuilder(array $config = []): Builder |
||
| 165 |