Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr/phalcon framework #96

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ php:
- 7.2
- 7.3

cache:
directories:
- ~/cphalcon

matrix:
fast_finish: true

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.bridgeless.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"autoload": {
"psr-4": { "Nextras\\Migrations\\": "src/" },
"classmap": ["src/exceptions.php", "src/deprecated"]
"classmap": ["src/deprecated"]
},
"autoload-dev": {
"classmap": ["tests/inc"]
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
},
Expand All @@ -39,7 +41,7 @@
},
"autoload": {
"psr-4": { "Nextras\\Migrations\\": "src/" },
"classmap": ["src/exceptions.php", "src/deprecated"]
"classmap": ["src/deprecated"]
},
"autoload-dev": {
"classmap": ["tests/inc"]
Expand Down
175 changes: 175 additions & 0 deletions doc/default.texy
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
declare(strict_types=1);

return [
'migrationsDir' => __DIR__ . '/../migrations',
'database' => [
'host' => getenv('DB_HOST'),
'username' => getenv('DB_NAME'),
'password' => getenv('DB_USER'),
'dbname' => getenv('DB_PASS'),
],
];
\--

`app/cli.php`
/--code php
<?php
declare(strict_types=1);


// Using the CLI factory default services container
$di = new \Phalcon\Di\FactoryDefault\Cli();


// Autoloader - register Nextras/Migrations
$loader = new \Phalcon\Loader();
$loader->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 <action>[:<group>:<label>][:production]`

Examples:
* `php app/cli.php Nextras\\Migrations\\Bridges\\Phalcon\\Migrations main create:dummy-data:users`
* `php app/cli.php Nextras\\Migrations\\Bridges\\Phalcon\\Migrations main cr:d:users`
* `php app/cli.php Nextras\\Migrations\\Bridges\\Phalcon\\Migrations main reset`
* `php app/cli.php Nextras\\Migrations\\Bridges\\Phalcon\\Migrations main co:production`

**Actions:**
* create
* Can be aliased as "cr".
* Creates empty sql file named YYYY-MM-DD-HHMMSS-label.sql.
* E.g. 2015-03-16-170342-users.sql.
* <label> is mandatory for "create" action.
* continue
* Can be aliased as "co".
* Migrates not migrated sql files only.
* Optional flag "production" (if present all dummy-data files are skipped).
* reset
* Can be aliased as "re".
* Drop whole database and then migrates all sql files.
* Optional flag "production" (if present all dummy-data files are skipped).

**Groups:**
* basic-data
* Can be aliased as "b".
* Data for both development and production.
* dummy-data
* Can be aliased as "d".
* Data for development on localhost.
* structures
* Can be aliased as "s".
* Creates, alter tables, etc.

**Label:**
* For "create" action only. Should be some brief name for sql file contents.

**Production:**
* For "continue" and "reset" actions only.
* If present all dummy-data files are skipped.

-------------------------
Loading