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

Commit

Permalink
Created AbstractRepository to force fetchAll to return a LinkedVersio…
Browse files Browse the repository at this point in the history
…ns collection.
  • Loading branch information
gsomoza committed Jul 13, 2015
1 parent 243e1f4 commit 1b6ef1d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 25 deletions.
29 changes: 29 additions & 0 deletions lib/Exception/RepositoryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <https://github.com/baleen/migrations>.
*/

namespace Baleen\Migrations\Exception;

/**
* Class RepositoryException
* @author Gabriel Somoza <gabriel@strategery.io>
*/
class RepositoryException extends BaleenException
{
}
71 changes: 71 additions & 0 deletions lib/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <https://github.com/baleen/migrations>.
*/

namespace Baleen\Migrations\Repository;

use Baleen\Migrations\Exception\RepositoryException;
use Baleen\Migrations\Migration\Factory\FactoryInterface;
use Baleen\Migrations\Version\Collection\LinkedVersions;

/**
* Class AbstractRepository
* @author Gabriel Somoza <gabriel@strategery.io>
*/
abstract class AbstractRepository implements RepositoryInterface
{

/**
* @var FactoryInterface
*/
protected $factory;

/**
* @inheritdoc
*/
public function setMigrationFactory(FactoryInterface $factory)
{
$this->factory = $factory;
}

/**
* @inheritdoc
* @return LinkedVersions
* @throws RepositoryException
*/
public function fetchAll()
{
$result = $this->doFetchAll();
if (!is_object($result) || !$result instanceof LinkedVersions) {
throw new RepositoryException(
'Method AbstractRepository::doFetchAll() must return a LinkedVersions object'
);
}
return $result;
}

/**
* Must fetch all versions available to the repository, load them with their migrations, and return them as a
* LinkedVersions collection. It must use a factory (default or supplied by 'setMigrationFactory()') to instantiate
* each of the migrations.
*
* @return mixed
*/
abstract protected function doFetchAll();
}
27 changes: 6 additions & 21 deletions lib/Repository/DirectoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
use Baleen\Migrations\Migration\Factory\SimpleFactory;
use Baleen\Migrations\Migration\MigrationInterface;
use Baleen\Migrations\Version;
use Baleen\Migrations\Version\Collection\LinkedVersions;
use Zend\Code\Scanner\DerivedClassScanner;
use Zend\Code\Scanner\DirectoryScanner;

/**
* @author Gabriel Somoza <gabriel@strategery.io>
*/
class DirectoryRepository implements RepositoryInterface
class DirectoryRepository extends AbstractRepository
{
const PATTERN_DEFAULT = '/v([0-9]+).*/';

Expand All @@ -45,11 +46,6 @@ class DirectoryRepository implements RepositoryInterface
*/
private $classNameRegex;

/**
* @var FactoryInterface
*/
private $factory;

/**
* @param $path
* @param FactoryInterface $migrationFactory
Expand All @@ -72,13 +68,11 @@ public function __construct($path, FactoryInterface $migrationFactory = null)
}

/**
* Returns all migrations available to the repository.
*
* @return array Array of Versions, each with a MigrationInstance object
* @inheritdoc
*/
public function fetchAll()
public function doFetchAll()
{
$versions = [];
$versions = new LinkedVersions();
$classes = $this->scanner->getClasses(true);
foreach ($classes as $class) {
/* @var DerivedClassScanner $class */
Expand All @@ -89,25 +83,16 @@ public function fetchAll()
&& isset($matches[1])) {
$migration = $this->factory->create($className);
if ($migration instanceof MigrationInterface) {
/* @var \Baleen\Migrations\Migration\MigrationInterface $migration */
$version = new Version($matches[1]);
$version->setMigration($migration);
$versions[] = $version;
$versions->add($version);
}
}
}

return $versions;
}

/**
* @inheritdoc
*/
public function setMigrationFactory(FactoryInterface $factory)
{
$this->factory = $factory;
}

/**
* @return string
*/
Expand Down
9 changes: 5 additions & 4 deletions lib/Repository/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace Baleen\Migrations\Repository;

use Baleen\Migrations\Migration\Factory\FactoryInterface;
use Baleen\Migrations\Migration\MigrationInterface;
use Baleen\Migrations\Version\Collection\LinkedVersions;

/**
* In charge of loading Migration files and instantiating them.
Expand All @@ -31,10 +31,11 @@
interface RepositoryInterface
{
/**
* Returns all migrations available to the repository. It must use a factory (default or supplied by
* 'setMigrationFactory' to instantiate a Migration.
* Must fetch all versions available to the repository, load them with their migrations, and return them as a
* LinkedVersions collection. It must use a factory (default or supplied by 'setMigrationFactory()') to instantiate
* each of the migrations.
*
* @return MigrationInterface[] Array of MigrationInterface objects
* @return LinkedVersions
*/
public function fetchAll();

Expand Down

0 comments on commit 1b6ef1d

Please sign in to comment.