From 0111e4616e6c83e1b6941c82678031fb884be1cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zat=C5=99ep=C3=A1lek?= Date: Mon, 21 Jan 2019 14:37:03 +0100 Subject: [PATCH 1/5] PSR-4 autoloading for exceptions --- composer.bridgeless.json | 2 +- composer.json | 2 +- src/Exception.php | 14 +++++++++ src/ExecutionException.php | 17 +++++++++++ src/IOException.php | 17 +++++++++++ src/LockException.php | 17 +++++++++++ src/LogicException.php | 17 +++++++++++ src/RuntimeException.php | 17 +++++++++++ src/exceptions.php | 58 -------------------------------------- 9 files changed, 101 insertions(+), 60 deletions(-) create mode 100644 src/Exception.php create mode 100644 src/ExecutionException.php create mode 100644 src/IOException.php create mode 100644 src/LockException.php create mode 100644 src/LogicException.php create mode 100644 src/RuntimeException.php delete mode 100644 src/exceptions.php diff --git a/composer.bridgeless.json b/composer.bridgeless.json index 74aa5598..5a7fb0b8 100644 --- a/composer.bridgeless.json +++ b/composer.bridgeless.json @@ -5,7 +5,7 @@ }, "autoload": { "psr-4": { "Nextras\\Migrations\\": "src/" }, - "classmap": ["src/exceptions.php", "src/deprecated"] + "classmap": ["src/deprecated"] }, "autoload-dev": { "classmap": ["tests/inc"] diff --git a/composer.json b/composer.json index 29c2e670..26d29b65 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ }, "autoload": { "psr-4": { "Nextras\\Migrations\\": "src/" }, - "classmap": ["src/exceptions.php", "src/deprecated"] + "classmap": ["src/deprecated"] }, "autoload-dev": { "classmap": ["tests/inc"] diff --git a/src/Exception.php b/src/Exception.php new file mode 100644 index 00000000..426ddba6 --- /dev/null +++ b/src/Exception.php @@ -0,0 +1,14 @@ + Date: Tue, 22 Jan 2019 16:25:42 +0100 Subject: [PATCH 2/5] Phalcon adapter and migration task --- composer.json | 1 + src/Bridges/Phalcon/MigrationsTask.php | 245 +++++++++++++++++++++++++ src/Bridges/Phalcon/PhalconAdapter.php | 80 ++++++++ 3 files changed, 326 insertions(+) create mode 100644 src/Bridges/Phalcon/MigrationsTask.php create mode 100755 src/Bridges/Phalcon/PhalconAdapter.php diff --git a/composer.json b/composer.json index 26d29b65..8005053e 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "nette/tester": "~1.7 | ~2.0", "nette/utils": "~2.3", "nextras/dbal": "~1.0 | ~2.0 | ~3.0", + "phalcon/ide-stubs": "~3.4", "symfony/config": "~2.6 | ~3.0 | ~4.0", "symfony/console": "~2.6 | ~3.0 | ~4.0", "symfony/dependency-injection": "~2.6 | ~3.0 | ~4.0", diff --git a/src/Bridges/Phalcon/MigrationsTask.php b/src/Bridges/Phalcon/MigrationsTask.php new file mode 100644 index 00000000..a6a09fef --- /dev/null +++ b/src/Bridges/Phalcon/MigrationsTask.php @@ -0,0 +1,245 @@ + self::GROUP_BASIC_DATA, + self::GROUP_DUMMY_DATA_ABBR => self::GROUP_DUMMY_DATA, + self::GROUP_STRUCTURES_ABBR => self::GROUP_STRUCTURES, + ]; + + private function getContainer() + { + if ($this->di === null) { + $this->di = $this->getDI(); + } + + return $this->di; + } + + /** + * @return string + */ + private function getMigrationsDir() + { + if ($this->migrationsDir === null) { + $this->migrationsDir = $this->getContainer()->get('config')->migrationsDir; + } + + return $this->migrationsDir; + } + + /** + * @return IDriver + */ + private function getDriver() + { + if ($this->driver === null) { + $this->driver = $this->getContainer()->get('driver'); + } + return $this->driver; + } + + /** + * @param array $params + */ + public function mainAction($params) + { + error_reporting(E_ALL); + ini_set('display_errors', 1); + + try { + $params = $this->handleParams($params); + } catch (\InvalidArgumentException $e) { + $this->error($e->getMessage()); + } + + $controller = new ConsoleController($this->getDriver()); + + $controller->addGroup('structures', $this->getMigrationsDir() . '/structures'); + $controller->addGroup('basic-data', $this->getMigrationsDir() . '/basic-data', ['structures']); + $controller->addGroup('dummy-data', $this->getMigrationsDir() . '/dummy-data', ['basic-data']); + $controller->addExtension('sql', new SqlHandler($this->getDriver())); + + // override parameters + $_SERVER['argv'] = array_merge([$_SERVER['argv'][0]], $params); + + $controller->run(); + } + + /** + * @param array $params + * @return array + * @throws \InvalidArgumentException + */ + private function handleParams($params) + { + if (count($params) !== 1) { + throw new \InvalidArgumentException('Invalid number of params.'); + } + + $param_parts = explode(':', $params[0]); + $count = count($param_parts); + + // continue or reset + if ($count > 0 && $count < 3) { + $production = false; + if (isset($param_parts[1])) { + if ($param_parts[1] === 'production') { + $production = true; + } else { + throw new \InvalidArgumentException('Invalid params.'); + } + } + + return $this->runMigrations($param_parts[0], $production); + } elseif ($count === 3) { + // create + if (!in_array($param_parts[0], [self::ACTION_CREATE, self::ACTION_CREATE_ABBR], true)) { + throw new \InvalidArgumentException('Invalid params.'); // 3 arguments can have only create action + } + + $this->createMigration($param_parts[1], $param_parts[2]); + } + + throw new \InvalidArgumentException('Invalid params.'); + } + + /** + * @param string $message + */ + private function error($message) + { + echo 'ERROR: ' . $message . PHP_EOL; + $this->printUsage(); + exit(1); + } + + /** + * @param string $action + * @param bool $production + * @return array + * @throws \InvalidArgumentException + */ + private function runMigrations($action, $production) + { + if (!in_array($action, [self::ACTION_CONTINUE, self::ACTION_CONTINUE_ABBR, self::ACTION_RESET, self::ACTION_RESET_ABBR], true)) { + throw new \InvalidArgumentException('Invalid action.'); + } + + $return = [self::GROUP_STRUCTURES, self::GROUP_BASIC_DATA]; + + if ($production === false) { + $return[] = self::GROUP_DUMMY_DATA; + } + + if (in_array($action, [self::ACTION_RESET, self::ACTION_RESET_ABBR], true)) { + $return[] = '--reset'; + } + + return $return; + } + + /** + * @param string $group + * @param string $label + * @throws \InvalidArgumentException + */ + private function createMigration($group, $label) + { + if (!in_array($group, array_merge(array_keys(self::$groups), array_values(self::$groups)), true)) { + throw new \InvalidArgumentException('Invalid group.'); + } + + // replace group abbreviation for group name + if (array_key_exists($group, self::$groups)) { + $group = self::$groups[$group]; + } + + $dir = $this->getMigrationsDir() . '/' . $group; + $name = date('Y-m-d-His-') . preg_replace('/[[:^print:]]/', '', $label) . '.sql'; + @mkdir($dir, 0777, true); + touch("$dir/$name"); + + exit; + } + + private function printUsage() + { + $scriptFile = isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : 'app/cli.php'; + + echo '------------------------------' . PHP_EOL; + echo 'Usage: php ' . $scriptFile . ' Nextras\\\\Migrations\\\\Bridges\\\\Phalcon\\\\Migrations main [::