Skip to content
This repository was archived by the owner on Jan 17, 2023. It is now read-only.
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Simple Migrations for Zend Framework 2. Project originally based on [ZendDbMigrations](https://github.com/vadim-knyzev/ZendDbMigrations) but module author did not response for issues and pull-requests so fork became independent project.


## Supported Drivers
The following DB adapter drivers are supported by this module.

Expand Down
46 changes: 27 additions & 19 deletions src/ZfSimpleMigrations/Controller/MigrateControllerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,50 @@
use Zend\Console\Request;
use Zend\Mvc\Router\Console\RouteMatch;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use ZfSimpleMigrations\Library\Migration;
use ZfSimpleMigrations\Library\MigrationSkeletonGenerator;
use Interop\Container\ContainerInterface;


class MigrateControllerFactory implements FactoryInterface
{

/**
* Create service
* canCreate
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
* @param mixed $container
* @param mixed $requestedName
* @return void
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
if($serviceLocator instanceof AbstractPluginManager)
{
$serviceLocator = $serviceLocator->getServiceLocator();
}

public function canCreate(ContainerInterface $container, $requestedName) {
return preg_match(self::FACTORY_PATTERN, $name);
}

/**
* __invoke
*
* @param mixed $container
* @param mixed $requestedName
* @param mixed $options
* @return void
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {

/** @var RouteMatch $routeMatch */
$routeMatch = $serviceLocator->get('Application')->getMvcEvent()->getRouteMatch();

$routeMatch = $container->get('Application')->getMvcEvent()->getRouteMatch();
$name = $routeMatch->getParam('name', 'default');

/** @var Migration $migration */
$migration = $serviceLocator->get('migrations.migration.' . $name);
$migration = $container->get('migrations.migration.' . $name);

/** @var MigrationSkeletonGenerator $generator */
$generator = $serviceLocator->get('migrations.skeleton-generator.' . $name);

$generator = $container->get('migrations.skeleton-generator.' . $name);
$controller = new MigrateController();

$controller->setMigration($migration);
$controller->setSkeletonGenerator($generator);

return $controller;
}
}
25 changes: 25 additions & 0 deletions src/ZfSimpleMigrations/Library/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,43 @@

use Zend\Db\Metadata\MetadataInterface;
use ZfSimpleMigrations\Library\MigrationInterface;
use Interop\Container\ContainerInterface;

abstract class AbstractMigration implements MigrationInterface
{
private $sql = [];
private $metadata;
private $writer;
protected $serviceLocator;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected $serviceLocator;
/** @var ServiceLocatorInterface */
protected $serviceLocator;


public function __construct(MetadataInterface $metadata, OutputWriter $writer)
{
$this->metadata = $metadata;
$this->writer = $writer;
}

/**
* Set service locator
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function setServiceLocator(ContainerInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
return $this;
}

/**
* Set service locator
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function getServiceLocator()
{
return $this->serviceLocator;
}

/**
* Add migration query
Expand Down
15 changes: 7 additions & 8 deletions src/ZfSimpleMigrations/Library/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
use Zend\Db\Metadata\Metadata;
use Zend\Db\Sql\Ddl;
use Zend\Db\Sql\Sql;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfSimpleMigrations\Library\OutputWriter;
use ZfSimpleMigrations\Model\MigrationVersion;
use ZfSimpleMigrations\Model\MigrationVersionTable;

use Interop\Container\ContainerInterface;
/**
* Main migration logic
*/
class Migration implements ServiceLocatorAwareInterface
class Migration
{
/** @var string */
protected $migrationsDir;
Expand Down Expand Up @@ -293,7 +292,7 @@ protected function applyMigration(array $migration, $down = false, $dryRun = fal
/** @var AbstractMigration $migrationObject */
$migrationObject = new $migration['class']($this->metadata, $this->outputWriter);

if ($migrationObject instanceof ServiceLocatorAwareInterface) {
if ($migrationObject instanceof MigrationInterface) {
if (is_null($this->serviceLocator)) {
throw new RuntimeException(
sprintf(
Expand Down Expand Up @@ -355,9 +354,9 @@ protected function applyMigration(array $migration, $down = false, $dryRun = fal
* Set service locator
*
* @param ServiceLocatorInterface $serviceLocator
* @return $this
* @return mixed
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator): Migration
public function setServiceLocator(ContainerInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;

Expand All @@ -369,7 +368,7 @@ public function setServiceLocator(ServiceLocatorInterface $serviceLocator): Migr
*
* @return ServiceLocatorInterface
*/
public function getServiceLocator(): ServiceLocatorInterface
public function getServiceLocator()
{
return $this->serviceLocator;
}
Expand Down
63 changes: 29 additions & 34 deletions src/ZfSimpleMigrations/Library/MigrationAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,70 @@

use RuntimeException;
use Zend\Db\Adapter\Adapter;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfSimpleMigrations\Model\MigrationVersionTable;
use Interop\Container\ContainerInterface;

class MigrationAbstractFactory implements AbstractFactoryInterface
{
const FACTORY_PATTERN = '/migrations\.migration\.(.*)/';

/**
* Determine if we can create a service with name
* canCreate
*
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @param Interop\Container\ContainerInterface $container
* @param mixed $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName): bool
{
return preg_match(self::FACTORY_PATTERN, $name) || preg_match(self::FACTORY_PATTERN, $requestedName);
public function canCreate(ContainerInterface $container, $requestedName) {
return preg_match(self::FACTORY_PATTERN, $requestedName) ;
}

/**
* Create service with name
* __invoke
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @param Interop\Container\ContainerInterface $container
* @param mixed $requestedName
* @param mixed $options
* @return Migration
* @throws MigrationException
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName): Migration
{
if ($serviceLocator instanceof AbstractPluginManager) {
$serviceLocator = $serviceLocator->getServiceLocator();
}

$config = $serviceLocator->get('Config');
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
$config = $container->get('Config');

preg_match(self::FACTORY_PATTERN, $name, $matches)
preg_match(self::FACTORY_PATTERN, $requestedName, $matches)
|| preg_match(self::FACTORY_PATTERN, $requestedName, $matches);

$name = $matches[1];

if (!isset($config['migrations'][$name])) {
throw new RuntimeException(sprintf("`%s` does not exist in migrations configuration", $name));
if (! isset($config['migrations'][$name])) {
throw new RuntimeException(sprintf("`%s` does not exist in migrations configuration", $requestedName));
}

$migrationConfig = $config['migrations'][$name];
$migration_config = $config['migrations'][$name];

$adapter_name = isset($migration_config['adapter'])
? $migration_config['adapter'] : 'Zend\Db\Adapter\Adapter';
/** @var $adapter \Zend\Db\Adapter\Adapter */
$adapter = $container->get($adapter_name);

$adapterName = $migrationConfig['adapter'] ?: Adapter::class;
/** @var Adapter $adapter */
$adapter = $serviceLocator->get($adapterName);

$output = null;
if (isset($migrationConfig['show_log']) && $migrationConfig['show_log']) {
/** @var OutputWriter $console */
$console = $serviceLocator->get('console');
if (isset($migration_config['show_log']) && $migration_config['show_log']) {
$console = $container->get('console');
$output = new OutputWriter(function ($message) use ($console) {
$console->write($message . "\n");
});
}

/** @var MigrationVersionTable $versionTable */
$versionTable = $serviceLocator->get('migrations.versiontable.' . $adapterName);
/** @var MigrationVersionTable $version_table */
$version_table = $container->get('migrations.versiontable.' . $adapter_name);

$migration = new Migration($adapter, $migrationConfig, $versionTable, $output);
$migration = new Migration($adapter, $migration_config, $version_table, $output);

$migration->setServiceLocator($serviceLocator);
$migration->setServiceLocator($container);

return $migration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,39 @@


use RuntimeException;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;

class MigrationSkeletonGeneratorAbstractFactory implements AbstractFactoryInterface
{
const FACTORY_PATTERN = '/migrations\.skeletongenerator\.(.*)/';
const FACTORY_PATTERN = '/migrations\.skeleton-generator\.(.*)/';

/**
* Determine if we can create a service with name
* canCreate
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @param Interop\Container\ContainerInterface $container
* @param mixed $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return preg_match(self::FACTORY_PATTERN, $name)
|| preg_match(self::FACTORY_PATTERN, $requestedName);
public function canCreate(ContainerInterface $container, $requestedName) {
return preg_match(self::FACTORY_PATTERN, $requestedName);
}

/**
* Create service with name
* __invoke
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return mixed
* @param Interop\Container\ContainerInterface $container
* @param mixed $requestedName
* @param mixed $options
* @return MigrationSkeletonGenerator
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if ($serviceLocator instanceof AbstractPluginManager) {
$serviceLocator = $serviceLocator->getServiceLocator();
}

preg_match(self::FACTORY_PATTERN, $name, $matches)
|| preg_match(self::FACTORY_PATTERN, $requestedName, $matches);
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {

preg_match(self::FACTORY_PATTERN, $requestedName, $matches);
$migration_name = $matches[1];


$config = $serviceLocator->get('Config');
$config = $container->get('Config');

if (! isset($config['migrations'][$migration_name])) {
throw new RuntimeException(sprintf("`%s` is not in migrations configuration", $migration_name));
Expand All @@ -62,7 +54,7 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
}

$generator = new MigrationSkeletonGenerator($migration_config['dir'], $migration_config['namespace']);

return $generator;
}
}
Loading