Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from codeliner/access-service-locator
Browse files Browse the repository at this point in the history
Access service locator
  • Loading branch information
vgarvardt committed Sep 10, 2014
2 parents 65f831f + 39e2e4e commit 305484c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,36 @@ class Version20130403165433 extends AbstractMigration
}
}
```

### Accessing ServiceLocator In Migration Class

By implementing the `Zend\ServiceManager\ServiceLocatorAwareInterface` in your migration class you get access to the
ServiceLocator used in the application.

``` php
<?php

namespace ZfSimpleMigrations\Migrations;

use ZfSimpleMigrations\Library\AbstractMigration;
use Zend\Db\Metadata\MetadataInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;

class Version20130403165433 extends AbstractMigration implements ServiceLocatorAwareInterface
{
public static $description = "Migration description";

public function up(MetadataInterface $schema)
{
//$this->getServiceLocator()->get(/*Get service by alias*/);
//$this->addSql(/*Sql instruction*/);

}

public function down(MetadataInterface $schema)
{
//$this->getServiceLocator()->get(/*Get service by alias*/);
//$this->addSql(/*Sql instruction*/);
}
}
```
2 changes: 2 additions & 0 deletions src/ZfSimpleMigrations/Controller/MigrateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ protected function getMigration()
}

$this->migration = new Migration($adapter, $config['migrations'], $this->getMigrationVersionTable(), $output);

$this->migration->setServiceLocator($this->getServiceLocator());
}
return $this->migration;
}
Expand Down
44 changes: 43 additions & 1 deletion src/ZfSimpleMigrations/Library/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
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;

/**
* Main migration logic
*/
class Migration
class Migration implements ServiceLocatorAwareInterface
{
protected $migrationsDir;
protected $migrationsNamespace;
Expand All @@ -28,6 +30,10 @@ class Migration
protected $migrationVersionTable;
protected $outputWriter;

/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator;
/**
* @param \Zend\Db\Adapter\Adapter $adapter
* @param array $config
Expand Down Expand Up @@ -247,6 +253,19 @@ protected function applyMigration(array $migration, $down = false, $fake = false
/** @var $migrationObject AbstractMigration */
$migrationObject = new $migration['class']($this->metadata, $this->outputWriter);

if ($migrationObject instanceof ServiceLocatorAwareInterface) {
if (is_null($this->serviceLocator)) {
throw new \RuntimeException(
sprintf(
'Migration class %s requires a ServiceLocator, but it is no instance available.',
get_class($migrationObject)
)
);
}

$migrationObject->setServiceLocator($this->serviceLocator);
}

$this->outputWriter->writeLine(sprintf("%sExecute migration class %s %s",
$fake ? '[FAKE] ' : '', $migration['class'], $down ? 'down' : 'up'));

Expand Down Expand Up @@ -274,4 +293,27 @@ protected function applyMigration(array $migration, $down = false, $fake = false
throw new MigrationException($msg);
}
}

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

return $this;
}

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

0 comments on commit 305484c

Please sign in to comment.