Skip to content

Commit

Permalink
Merge pull request #110 from AntonLee/update-readme
Browse files Browse the repository at this point in the history
update readme
  • Loading branch information
davedevelopment committed Aug 8, 2016
2 parents b87452a + fb04901 commit a3bc706
Showing 1 changed file with 61 additions and 63 deletions.
124 changes: 61 additions & 63 deletions README.md
Expand Up @@ -92,25 +92,25 @@ The init command creates a bootstrap file that specifies a flat file to use to
track which migrations have been run, which isn't great. You can use the
provided adapters to store this information in your database.

``` php
```php
<?php

# phpmig.php

use \Phpmig\Adapter,
\Pimple;
use Phpmig\Adapter;
use Pimple\Container;

$container = new Pimple();
$container = new Container();

$container['db'] = $container->share(function() {
$container['db'] = function () {
$dbh = new PDO('mysql:dbname=testdb;host=127.0.0.1','username','passwd');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
});
};

$container['phpmig.adapter'] = $container->share(function() use ($container) {
return new Adapter\PDO\Sql($container['db'], 'migrations');
});
$container['phpmig.adapter'] = function ($c) {
return new Adapter\PDO\Sql($c['db'], 'migrations');
};

$container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations';

Expand All @@ -126,20 +126,20 @@ Adds support for qualifying the migrations table with a schema.

# phpmig

use \Phpmig\Adapter,
\Pimple;
use Phpmig\Adapter;
use Pimple\Container;

$container = new Pimple();
$container = new Container();

$container['db'] = $container->share(function() {
$container['db'] = function () {
$dbh = new PDO(sprintf('pgsql:dbname=%s;host=%s;password=%s', 'dbname', 'localhost', 'password'), 'dbuser', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
});
};

$container['phpmig.adapter'] = $container->share(function() use ($container) {
return new Adapter\PDO\SqlPgsql($container['db'], 'migrations', 'migrationschema');
});
$container['phpmig.adapter'] = function ($c) {
return new Adapter\PDO\SqlPgsql($c['db'], 'migrations', 'migrationschema');
};

return $container;
```
Expand All @@ -148,34 +148,32 @@ return $container;

Or you can use Doctrine's DBAL:

``` php
```php
<?php

# phpmig.php

// do some autoloading of Doctrine here

use \Phpmig\Adapter,
\Pimple,
\Doctrine\DBAL\DriverManager;
use Phpmig\Adapter;
use Pimple\Container;
use Doctrine\DBAL\DriverManager;

$container = new Pimple();
$container = new Container();

$container['db'] = $container->share(function() {
$container['db'] = function () {
return DriverManager::getConnection(array(
'driver' => 'pdo_sqlite',
'path' => __DIR__ . DIRECTORY_SEPARATOR . 'db.sqlite',
));
});

$container['phpmig.adapter'] = $container->share(function() use ($container) {
return new Adapter\Doctrine\DBAL($container['db'], 'migrations');
});
};

$container['phpmig.migrations_path'] = function() {
return __DIR__ . DIRECTORY_SEPARATOR . 'migrations';
$container['phpmig.adapter'] = function ($c) {
return new Adapter\Doctrine\DBAL($c['db'], 'migrations');
};

$container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations';

return $container;
```

Expand All @@ -184,7 +182,7 @@ setting up migrations requires couple additional steps. You first need to prepar
the configuration. It might be in any format supported by Zend_Config. Here is an
example in YAML for MySQL:

``` yaml
```yaml
phpmig:
tableName: migrations
createStatement: CREATE TABLE migrations ( version VARCHAR(255) NOT NULL );
Expand All @@ -196,7 +194,7 @@ in the config folder for some common RDBMS.

Here is how the bootstrap file should look like:

``` php
```php
<?php

# phpmig.php
Expand All @@ -211,35 +209,32 @@ require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Zend_');

use \Pimple,
\Phpmig\Adapter\Zend\Db;
use Phpmig\Adapter\Zend\Db;
use Pimple\Container;

$container = new Pimple();
$container = new Container();

$container['db'] = $container->share(function() {
$container['db'] = function () {
return Zend_Db::factory('pdo_mysql', array(
'dbname' => 'DBNAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'host' => 'localhost'
));
});
};

$container['phpmig.adapter'] = $container->share(function() use ($container) {
$container['phpmig.adapter'] = function($c) {
$configuration = null;
$configurationFile = PHPMIG_PATH . '/config/mysql.yaml';

if (file_exists($configurationFile)) {
$configuration = new Zend_Config_Yaml($configurationFile, null, array('ignore_constants' => true));
}

return new Db($container['db'], $configuration);
});

$container['phpmig.migrations_path'] = function() {
return __DIR__ . DIRECTORY_SEPARATOR . 'migrations';
return new Db($c['db'], $configuration);
};

$container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations';

return $container;
```
Expand All @@ -249,11 +244,11 @@ Example with Eloquent ORM 5.1
```php
<?php

use \Phpmig\Adapter;
use \Phpmig\Pimple\Pimple,
\Illuminate\Database\Capsule\Manager as Capsule;
use Phpmig\Adapter;
use Pimple\Container;
use Illuminate\Database\Capsule\Manager as Capsule;

$container = new Pimple();
$container = new Container();

$container['config'] = [
'driver' => 'xxx',
Expand All @@ -266,21 +261,19 @@ $container['config'] = [
'prefix' => '',
];

$container['db'] = $container->share(function() use ($container) {
$container['db'] = function ($c) {
$capsule = new Capsule();
$capsule->addConnection($container['config']);
$capsule->addConnection($c['config']);
$capsule->setAsGlobal();
$capsule->bootEloquent();

return $capsule;
});
};

$container['phpmig.adapter'] = $container->share(function() use ($container) {
return new Adapter\Illuminate\Database($container['db'], 'migrations');
});
$container['phpmig.migrations_path'] = function() {
return __DIR__ . DIRECTORY_SEPARATOR . 'migrations';
$container['phpmig.adapter'] = function($c) {
return new Adapter\Illuminate\Database($c['db'], 'migrations');
};
$container['phpmig.migrations_path'] = __DIR__ . DIRECTORY_SEPARATOR . 'migrations';

return $container;
```
Expand All @@ -293,7 +286,7 @@ The migrations should extend the Phpmig\Migration\Migration class, and have
access to the container. For example, assuming you've rewritten your bootstrap
file like above:

``` php
```php
<?php

use Phpmig\Migration\Migration;
Expand Down Expand Up @@ -391,8 +384,11 @@ Module Migrations

If you have an application that consists of different modules and you want to be able to separate the migration, Phpmig has a built-in way to achieve this.

``` php
$container['phpmig.sets'] = $container->share(function ($container) {
```php
<?php

/** @var Pimple\Container $container */
$container['phpmig.sets'] = function ($container) {
return array(
'cms' => array(
'adapter' => new Adapter\File\Flat('modules/migrationLogs/cms_migrations.log'),
Expand All @@ -403,7 +399,7 @@ $container['phpmig.sets'] = $container->share(function ($container) {
'migrations_path' => 'migrations/blog'
)
);
});
};
```

this way each set has their own migration log and the ability to migrate changes independently of each other.
Expand Down Expand Up @@ -436,9 +432,11 @@ organize your migrations script however you like and have several migrations
directory. To do this you can provide an array of migration file paths to the
container :

``` php
```php
<?php

$container['phpmig.migrations'] = function() {
/** @var Pimple\Container $container */
$container['phpmig.migrations'] = function () {
return array_merge(
glob('migrations_1/*.php'),
glob('migrations_2/*.php')
Expand Down Expand Up @@ -498,12 +496,12 @@ In order to use the migration tool outside the cli context use `Phpmig\Api\Phpmi
use Phpmig\Api\PhpmigApplication;

// require the composer autoloader
require __DIR__ . "/vendor/autoload.php";
require __DIR__ . '/vendor/autoload.php';

$output = new \Symfony\Component\Console\Output\NullOutput();

// create container from bootstrap file
$container = require __DIR__ . "/tests/dom/phpmig.php";
$container = require __DIR__ . '/tests/dom/phpmig.php';

$app = new PhpmigApplication($container, $output);

Expand Down

0 comments on commit a3bc706

Please sign in to comment.