Skip to content

Commit 4f69bbe

Browse files
Phalcon integration docs
1 parent 34cfdf2 commit 4f69bbe

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

doc/default.texy

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,178 @@ $controller->addExtension('php', new Extensions\PhpHandler([
148148
'dibi' => $conn,
149149
]));
150150
\--
151+
152+
153+
Using as Phalcon Framework Task
154+
===============================
155+
156+
Migrations come with predefined Phalcon CLI Task. Task uses these two services:
157+
158+
* **config** (`\Phalcon\Config`) with key `migrationsDir`. See example config.php below.
159+
* **driver** (`\Nextras\Migrations\IDriver`). See example in cli.php below.
160+
161+
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.
162+
163+
`config/config.php`
164+
/--code php
165+
<?php
166+
declare(strict_types=1);
167+
168+
return [
169+
'migrationsDir' => __DIR__ . '/../migrations',
170+
'database' => [
171+
'host' => getenv('DB_HOST'),
172+
'username' => getenv('DB_NAME'),
173+
'password' => getenv('DB_USER'),
174+
'dbname' => getenv('DB_PASS'),
175+
],
176+
];
177+
\--
178+
179+
`app/cli.php`
180+
/--code php
181+
<?php
182+
declare(strict_types=1);
183+
184+
185+
// Using the CLI factory default services container
186+
$di = new \Phalcon\Di\FactoryDefault\Cli();
187+
188+
189+
// Autoloader - register Nextras/Migrations
190+
$loader = new \Phalcon\Loader();
191+
$loader->registerNamespaces(
192+
[
193+
'Nextras\Migrations' => __DIR__ . '/../vendor/nextras/migrations/src',
194+
]
195+
);
196+
$loader->register();
197+
198+
// DI services
199+
$di->set(
200+
'config',
201+
function () {
202+
$configFile = __DIR__ . '/../config/config.php';
203+
if (!is_readable($configFile)) {
204+
die('Config file not readable.');
205+
}
206+
$config = include $configFile;
207+
return new Phalcon\Config($config);
208+
}
209+
);
210+
$di->set(
211+
'migrationsDir',
212+
function () {
213+
/** @var \Phalcon\Config $config */
214+
$config = $this->get('config');
215+
return $config->migrationsDir;
216+
}
217+
);
218+
$di->set(
219+
'connection',
220+
function () {
221+
/** @var \Phalcon\Config $config */
222+
$config = $this->get('config');
223+
return new \Phalcon\Db\Adapter\Pdo\Mysql([
224+
'host' => $config->database->host,
225+
'username' => $config->database->username,
226+
'password' => $config->database->password,
227+
'dbname' => $config->database->dbname,
228+
'dialectClass' => new \Phalcon\Db\Dialect\Mysql(),
229+
]);
230+
}
231+
);
232+
$di->set(
233+
'phalconAdapter',
234+
function () {
235+
/** @var \Phalcon\Db\Adapter\Pdo $connection */
236+
$connection = $this->get('connection');
237+
return new \Nextras\Migrations\Bridges\Phalcon\PhalconAdapter($connection);
238+
}
239+
);
240+
$di->set(
241+
'driver',
242+
function () {
243+
/** @var \Nextras\Migrations\Bridges\Phalcon\PhalconAdapter $phalconAdapter */
244+
$phalconAdapter = $this->get('phalconAdapter');
245+
return new \Nextras\Migrations\Drivers\MySqlDriver($phalconAdapter);
246+
}
247+
);
248+
249+
// Create a console application
250+
$console = new \Phalcon\Cli\Console();
251+
$console->setDI($di);
252+
253+
// Process the console arguments
254+
$arguments = [];
255+
256+
foreach ($argv as $k => $arg) {
257+
if ($k === 1) {
258+
$arguments['task'] = $arg;
259+
} elseif ($k === 2) {
260+
$arguments['action'] = $arg;
261+
} elseif ($k >= 3) {
262+
$arguments['params'][] = $arg;
263+
}
264+
}
265+
266+
try {
267+
// Handle incoming arguments
268+
$console->handle($arguments);
269+
} catch (\Phalcon\Exception $e) {
270+
// Do Phalcon related stuff here
271+
// ..
272+
fwrite(STDERR, $e->getMessage() . PHP_EOL);
273+
exit(1);
274+
} catch (\Throwable $throwable) {
275+
fwrite(STDERR, $throwable->getMessage() . PHP_EOL);
276+
exit(1);
277+
}
278+
279+
\--
280+
281+
Usage
282+
-----
283+
284+
`php app/cli.php Nextras\\Migrations\\Bridges\\Phalcon\\Migrations main <action>[:<group>:<label>][:production]`
285+
286+
Examples:
287+
* `php app/cli.php Nextras\\Migrations\\Bridges\\Phalcon\\Migrations main create:dummy-data:users`
288+
* `php app/cli.php Nextras\\Migrations\\Bridges\\Phalcon\\Migrations main cr:d:users`
289+
* `php app/cli.php Nextras\\Migrations\\Bridges\\Phalcon\\Migrations main reset`
290+
* `php app/cli.php Nextras\\Migrations\\Bridges\\Phalcon\\Migrations main co:production`
291+
292+
**Actions:**
293+
* create
294+
* Can be aliased as "cr".
295+
* Creates empty sql file named YYYY-MM-DD-HHMMSS-label.sql.
296+
* E.g. 2015-03-16-170342-users.sql.
297+
* <label> is mandatory for "create" action.
298+
* continue
299+
* Can be aliased as "co".
300+
* Migrates not migrated sql files only.
301+
* Optional flag "production" (if present all dummy-data files are skipped).
302+
* reset
303+
* Can be aliased as "re".
304+
* Drop whole database and then migrates all sql files.
305+
* Optional flag "production" (if present all dummy-data files are skipped).
306+
307+
**Groups:**
308+
* basic-data
309+
* Can be aliased as "b".
310+
* Data for both development and production.
311+
* dummy-data
312+
* Can be aliased as "d".
313+
* Data for development on localhost.
314+
* structures
315+
* Can be aliased as "s".
316+
* Creates, alter tables, etc.
317+
318+
**Label:**
319+
* For "create" action only. Should be some brief name for sql file contents.
320+
321+
**Production:**
322+
* For "continue" and "reset" actions only.
323+
* If present all dummy-data files are skipped.
324+
325+
-------------------------

0 commit comments

Comments
 (0)