From 235b2178bab916b760107ac136780d3369280520 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 22 Jan 2020 16:22:06 +0300 Subject: [PATCH 1/2] remove not used ScriptHandler --- src/Composer/ScriptHandler.php | 203 --------------------------------- 1 file changed, 203 deletions(-) delete mode 100644 src/Composer/ScriptHandler.php diff --git a/src/Composer/ScriptHandler.php b/src/Composer/ScriptHandler.php deleted file mode 100644 index c06f452..0000000 --- a/src/Composer/ScriptHandler.php +++ /dev/null @@ -1,203 +0,0 @@ - - * @copyright Copyright (c) 2017, Peter Gribanov - * @license http://opensource.org/licenses/MIT - */ - -namespace GpsLab\Bundle\GeoIP2Bundle\Composer; - -use Composer\Script\Event; -use Symfony\Component\Process\PhpExecutableFinder; -use Symfony\Component\Process\Process; - -class ScriptHandler -{ - /** - * Composer variables are declared static so that an event could update - * a composer.json and set new options, making them immediately available - * to forthcoming listeners. - */ - private static $options = [ - 'symfony-app-dir' => 'app', - ]; - - /** - * @param Event $event - */ - public static function updateDatabase(Event $event): void - { - $options = static::getOptions($event); - $console_dir = static::getConsoleDir($event, 'clear the cache'); - - if (null === $console_dir) { - return; - } - - static::executeCommand($event, $console_dir, 'geoip2:update --no-debug', $options['process-timeout']); - } - - /** - * @param Event $event - * @param string $console_dir - * @param string $cmd - * @param int $timeout - */ - protected static function executeCommand(Event $event, string $console_dir, string $cmd, int $timeout = 300): void - { - $php = escapeshellarg(self::getPhp(false)); - $php_args = implode(' ', array_map('escapeshellarg', self::getPhpArguments())); - $console = escapeshellarg($console_dir.'/console'); - - if ($event->getIO()->isDecorated()) { - $console .= ' --ansi'; - } - - $command = $php.($php_args ? ' '.$php_args : '').' '.$console.' '.$cmd; - - if (method_exists('Symfony\Component\Process\Process', 'fromShellCommandline')) { - // Symfony 4.2 + - $process = Process::fromShellCommandline($command, null, null, null, $timeout); - } else { - // Symfony 4.1 and below - $process = new Process($command, null, null, null, $timeout); - } - - $process->run(static function ($type, $buffer) use ($event): void { - $event->getIO()->write($buffer, false); - }); - - if (!$process->isSuccessful()) { - throw new \RuntimeException(sprintf("An error occurred when executing the \"%s\" command:\n\n%s\n\n%s.", escapeshellarg($cmd), $process->getOutput(), $process->getErrorOutput())); - } - } - - /** - * @param Event $event - * - * @return array - */ - protected static function getOptions(Event $event): array - { - $options = array_merge(self::$options, $event->getComposer()->getPackage()->getExtra()); - - $options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout'); - - return $options; - } - - /** - * Returns a relative path to the directory that contains the `console` command or null if not found. - * - * @param Event $event The command event - * @param string $action_name The name of the action - * - * @return string|null - */ - protected static function getConsoleDir(Event $event, string $action_name): ?string - { - $options = static::getOptions($event); - - if (self::useNewDirectoryStructure($options)) { - if (!self::hasDirectory($event, 'symfony-bin-dir', $options['symfony-bin-dir'], $action_name)) { - return null; - } - - return $options['symfony-bin-dir']; - } - - if (!self::hasDirectory($event, 'symfony-app-dir', $options['symfony-app-dir'], 'execute command')) { - return null; - } - - return $options['symfony-app-dir']; - } - - /** - * @param Event $event - * @param string $config_name - * @param string $path - * @param string $action_name - * - * @return bool - */ - private static function hasDirectory(Event $event, string $config_name, string $path, string $action_name): bool - { - if (!is_dir($path)) { - $event->getIO()->write(sprintf( - 'The %s (%s) specified in composer.json was not found in %s, can not %s.', - $config_name, - $path, - getcwd(), - $action_name - )); - - return false; - } - - return true; - } - - /** - * Returns true if the new directory structure is used. - * - * @param array $options Composer options - * - * @return bool - */ - private static function useNewDirectoryStructure(array $options): bool - { - return isset($options['symfony-bin-dir']) && is_dir($options['symfony-bin-dir']); - } - - /** - * Get path to php executable. - * - * @param bool $include_args - * - * @throws \RuntimeException - * - * @return string - */ - private static function getPhp(bool $include_args = true): string - { - $php_finder = new PhpExecutableFinder(); - $php_path = $php_finder->find($include_args); - - if (!$php_path) { - throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again'); - } - - return $php_path; - } - - /** - * @return array - */ - private static function getPhpArguments(): array - { - $arguments = []; - $php_finder = new PhpExecutableFinder(); - - if (method_exists($php_finder, 'findArguments')) { - $arguments = $php_finder->findArguments(); - } - - $env = (string) getenv('COMPOSER_ORIGINAL_INIS'); - - if ($env) { - $paths = explode(PATH_SEPARATOR, $env); - $ini = array_shift($paths); - } else { - $ini = php_ini_loaded_file(); - } - - if ($ini) { - $arguments[] = '--php-ini='.$ini; - } - - return $arguments; - } -} From 4b443664ac9304ee7b2a11ad4d6ce501af6e48ed Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Wed, 22 Jan 2020 16:34:05 +0300 Subject: [PATCH 2/2] add upgrade info --- UPGRADE-2.0.md | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/UPGRADE-2.0.md b/UPGRADE-2.0.md index ddc5b41..e40d3e8 100644 --- a/UPGRADE-2.0.md +++ b/UPGRADE-2.0.md @@ -1,17 +1,59 @@ UPGRADE FROM 1.x to 2.0 ======================= +Update `composer.json` if you use composer vent callbacks. + +Before in Symfony <3.0 + +```json +{ + "scripts": { + "post-install-cmd": [ + "GpsLab\\Bundle\\GeoIP2Bundle\\Composer\\ScriptHandler::updateDatabase" + ], + "post-update-cmd": [ + "GpsLab\\Bundle\\GeoIP2Bundle\\Composer\\ScriptHandler::updateDatabase" + ] + } +} +``` + +Before in Symfony >3.1 + +```json +{ + "scripts": { + "symfony-scripts": [ + "GpsLab\\Bundle\\GeoIP2Bundle\\Composer\\ScriptHandler::updateDatabase" + ] + } +} +``` + +After in Symfony >4 + +```json +{ + "scripts": { + "auto-scripts": { + "geoip2:update": "symfony-cmd" + } + } +} +``` + ### Dependencies * The `UpdateDatabaseCommand` command not dependency a `CompressorInterface`. -### Renamed services +### Renamed - * The `gpslab.command.geoip2.update` renamed to `GpsLab\Bundle\GeoIP2Bundle\Command\UpdateDatabaseCommand`. + * The `gpslab.command.geoip2.update` service renamed to `GpsLab\Bundle\GeoIP2Bundle\Command\UpdateDatabaseCommand`. -### Removed service +### Removed * The `gpslab.geoip2.component.gzip` service removed. + * The `ScriptHandler` removed. Updating Dependencies ---------------------