diff --git a/src/Command/DeployCommand.php b/src/Command/DeployCommand.php index f39c893..b018540 100644 --- a/src/Command/DeployCommand.php +++ b/src/Command/DeployCommand.php @@ -12,7 +12,7 @@ namespace EasyCorp\Bundle\EasyDeployBundle\Command; use EasyCorp\Bundle\EasyDeployBundle\Context; -use EasyCorp\Bundle\EasyDeployBundle\Exception\SymfonyVersionException; +use EasyCorp\Bundle\EasyDeployBundle\Helper\SymfonyConfigPathGuesser; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -21,7 +21,6 @@ use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel\Config\FileLocator; -use Symfony\Component\HttpKernel\Kernel; class DeployCommand extends Command { @@ -62,7 +61,7 @@ protected function initialize(InputInterface $input, OutputInterface $output) return $this->configFilePath = $customConfigPath; } - $defaultConfigPath = $this->getDefaultConfigPath($input->getArgument('stage')); + $defaultConfigPath = SymfonyConfigPathGuesser::guess($this->projectDir, $input->getArgument('stage')); if (is_readable($defaultConfigPath)) { return $this->configFilePath = $defaultConfigPath; } @@ -80,22 +79,6 @@ protected function execute(InputInterface $input, OutputInterface $output) $deployer->doDeploy(); } - private function getDefaultConfigPath(string $stageName) : string - { - $symfonyVersion = Kernel::MAJOR_VERSION; - $defaultConfigPaths = [ - 2 => sprintf('%s/app/config/deploy_%s.php', $this->projectDir, $stageName), - 3 => sprintf('%s/app/config/deploy_%s.php', $this->projectDir, $stageName), - 4 => sprintf('%s/etc/%s/deploy.php', $this->projectDir, $stageName), - ]; - - if (!isset($defaultConfigPaths[$symfonyVersion])) { - throw new SymfonyVersionException($symfonyVersion); - } - - return $defaultConfigPaths[$symfonyVersion]; - } - private function createDefaultConfigFile(InputInterface $input, OutputInterface $output, string $defaultConfigPath, string $stageName) : void { $helper = $this->getHelper('question'); diff --git a/src/Command/RollbackCommand.php b/src/Command/RollbackCommand.php index aad8181..3a6fcb7 100644 --- a/src/Command/RollbackCommand.php +++ b/src/Command/RollbackCommand.php @@ -12,13 +12,12 @@ namespace EasyCorp\Bundle\EasyDeployBundle\Command; use EasyCorp\Bundle\EasyDeployBundle\Context; -use EasyCorp\Bundle\EasyDeployBundle\Exception\SymfonyVersionException; +use EasyCorp\Bundle\EasyDeployBundle\Helper\SymfonyConfigPathGuesser; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\HttpKernel\Kernel; class RollbackCommand extends Command { @@ -57,7 +56,7 @@ protected function initialize(InputInterface $input, OutputInterface $output) return $this->configFilePath = $customConfigPath; } - $defaultConfigPath = $this->getDefaultConfigPath($input->getArgument('stage')); + $defaultConfigPath = SymfonyConfigPathGuesser::guess($this->projectDir, $input->getArgument('stage')); if (is_readable($defaultConfigPath)) { return $this->configFilePath = $defaultConfigPath; } @@ -74,20 +73,4 @@ protected function execute(InputInterface $input, OutputInterface $output) $deployer->initialize($context); $deployer->doRollback(); } - - private function getDefaultConfigPath(string $stageName) : string - { - $symfonyVersion = Kernel::MAJOR_VERSION; - $defaultConfigPaths = [ - 2 => sprintf('%s/app/config/deploy_%s.php', $this->projectDir, $stageName), - 3 => sprintf('%s/app/config/deploy_%s.php', $this->projectDir, $stageName), - 4 => sprintf('%s/etc/%s/deploy.php', $this->projectDir, $stageName), - ]; - - if (!isset($defaultConfigPaths[$symfonyVersion])) { - throw new SymfonyVersionException($symfonyVersion); - } - - return $defaultConfigPaths[$symfonyVersion]; - } } diff --git a/src/Exception/SymfonyVersionException.php b/src/Exception/SymfonyVersionException.php deleted file mode 100644 index afddfd6..0000000 --- a/src/Exception/SymfonyVersionException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace EasyCorp\Bundle\EasyDeployBundle\Exception; - -class SymfonyVersionException extends \RuntimeException -{ - public function __construct(int $version) - { - parent::__construct(sprintf('The application uses the unsupported Symfony "%d" version.', $version)); - } -} diff --git a/src/Helper/SymfonyConfigPathGuesser.php b/src/Helper/SymfonyConfigPathGuesser.php new file mode 100644 index 0000000..ca41d74 --- /dev/null +++ b/src/Helper/SymfonyConfigPathGuesser.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace EasyCorp\Bundle\EasyDeployBundle\Helper; + +/** + * @author Jules Pietri + */ +class SymfonyConfigPathGuesser +{ + private const LEGACY_CONFIG_DIR = '%s/app/config'; + private const CONFIG_DIR = '%s/etc'; + + public static function guess(string $projectDir, string $stage): string + { + if (is_dir($configDir = sprintf(self::CONFIG_DIR, $projectDir))) { + return sprintf('%s/%s/deploy.php', $configDir, $stage); + } + + if (is_dir($configDir = sprintf(self::LEGACY_CONFIG_DIR, $projectDir))) { + return sprintf('%s/deploy_%s.php', $configDir, $stage); + } + + throw new \RuntimeException(sprintf('None of the usual Symfony config dirs exist in the application. Create one of these dirs before continuing: "%s" or "%s".', self::CONFIG_DIR, self::LEGACY_CONFIG_DIR)); + } +}