diff --git a/.travis.yml b/.travis.yml index 90f471b..7ad0c13 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,10 @@ php: - 7.2 - 7.3 +cache: + directories: + - ~/cphalcon + matrix: fast_finish: true @@ -23,6 +27,7 @@ before_script: - composer self-update - composer install --no-interaction --no-progress + - if [[ $TRAVIS_PHP_VERSION == "5.5" ]] || [[ $TRAVIS_PHP_VERSION == "5.6" ]] || [[ $TRAVIS_PHP_VERSION == "7.0" ]] || [[ $TRAVIS_PHP_VERSION == "7.1" ]] || [[ $TRAVIS_PHP_VERSION == "7.2" ]]; then vendor/bin/install-phalcon.sh 3.4.x; fi script: - ./tests/run-unit.sh diff --git a/composer.bridgeless.json b/composer.bridgeless.json index 74aa559..5a7fb0b 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 29c2e67..20954f4 100644 --- a/composer.json +++ b/composer.json @@ -17,10 +17,12 @@ "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", "symfony/http-kernel": "~2.6 | ~3.0 | ~4.0", + "techpivot/phalcon-ci-installer": "~1.0", "tracy/tracy": "^2.2", "ext-openssl": "*" }, @@ -39,7 +41,7 @@ }, "autoload": { "psr-4": { "Nextras\\Migrations\\": "src/" }, - "classmap": ["src/exceptions.php", "src/deprecated"] + "classmap": ["src/deprecated"] }, "autoload-dev": { "classmap": ["tests/inc"] diff --git a/doc/default.texy b/doc/default.texy index 16c863f..e268f04 100644 --- a/doc/default.texy +++ b/doc/default.texy @@ -148,3 +148,178 @@ $controller->addExtension('php', new Extensions\PhpHandler([ 'dibi' => $conn, ])); \-- + + +Using as Phalcon Framework Task +=============================== + +Migrations come with predefined Phalcon CLI Task. Task uses these two services: + +* **config** (`\Phalcon\Config`) with key `migrationsDir`. See example config.php below. +* **driver** (`\Nextras\Migrations\IDriver`). See example in cli.php below. + +Service `driver` needs `\Nextras\Migrations\Bridges\Phalcon\PhalconAdapter` as argument. Therefore you will probably need to declare other services (see cli.php) or create phalcon adapter inside **driver**'s lambda function. + +`config/config.php` +/--code php + __DIR__ . '/../migrations', + 'database' => [ + 'host' => getenv('DB_HOST'), + 'username' => getenv('DB_NAME'), + 'password' => getenv('DB_USER'), + 'dbname' => getenv('DB_PASS'), + ], +]; +\-- + +`app/cli.php` +/--code php +registerNamespaces( + [ + 'Nextras\Migrations' => __DIR__ . '/../vendor/nextras/migrations/src', + ] +); +$loader->register(); + +// DI services +$di->set( + 'config', + function () { + $configFile = __DIR__ . '/../config/config.php'; + if (!is_readable($configFile)) { + die('Config file not readable.'); + } + $config = include $configFile; + return new Phalcon\Config($config); + } +); +$di->set( + 'migrationsDir', + function () { + /** @var \Phalcon\Config $config */ + $config = $this->get('config'); + return $config->migrationsDir; + } +); +$di->set( + 'connection', + function () { + /** @var \Phalcon\Config $config */ + $config = $this->get('config'); + return new \Phalcon\Db\Adapter\Pdo\Mysql([ + 'host' => $config->database->host, + 'username' => $config->database->username, + 'password' => $config->database->password, + 'dbname' => $config->database->dbname, + 'dialectClass' => new \Phalcon\Db\Dialect\Mysql(), + ]); + } +); +$di->set( + 'phalconAdapter', + function () { + /** @var \Phalcon\Db\Adapter\Pdo $connection */ + $connection = $this->get('connection'); + return new \Nextras\Migrations\Bridges\Phalcon\PhalconAdapter($connection); + } +); +$di->set( + 'driver', + function () { + /** @var \Nextras\Migrations\Bridges\Phalcon\PhalconAdapter $phalconAdapter */ + $phalconAdapter = $this->get('phalconAdapter'); + return new \Nextras\Migrations\Drivers\MySqlDriver($phalconAdapter); + } +); + +// Create a console application +$console = new \Phalcon\Cli\Console(); +$console->setDI($di); + +// Process the console arguments +$arguments = []; + +foreach ($argv as $k => $arg) { + if ($k === 1) { + $arguments['task'] = $arg; + } elseif ($k === 2) { + $arguments['action'] = $arg; + } elseif ($k >= 3) { + $arguments['params'][] = $arg; + } +} + +try { + // Handle incoming arguments + $console->handle($arguments); +} catch (\Phalcon\Exception $e) { + // Do Phalcon related stuff here + // .. + fwrite(STDERR, $e->getMessage() . PHP_EOL); + exit(1); +} catch (\Throwable $throwable) { + fwrite(STDERR, $throwable->getMessage() . PHP_EOL); + exit(1); +} + +\-- + +Usage +----- + +`php app/cli.php Nextras\\Migrations\\Bridges\\Phalcon\\Migrations main [::