Skip to content

Commit

Permalink
add namespace support in migrations & seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey N. Mokhov committed Mar 30, 2017
1 parent 1b71e03 commit 00c1a67
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/Phinx/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
* @package Phinx
* @author Rob Morgan
*/
class Config implements ConfigInterface
class Config implements ConfigInterface, NamespaceAwareInterface
{
use NamespaceAwareTrait;

/**
* The value that identifies a version order by creation time.
*/
Expand Down
46 changes: 46 additions & 0 deletions src/Phinx/Config/NamespaceAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2017 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Config
*/

namespace Phinx\Config;

/**
* Config aware getNamespaceByPath method.
* @package Phinx\Config
* @author Andrey N. Mokhov
*/
interface NamespaceAwareInterface
{
/**
* Get Namespace associated with path.
*
* @param string $path
* @return string|null
*/
public function getNamespaceByPath($path);
}
59 changes: 59 additions & 0 deletions src/Phinx/Config/NamespaceAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Phinx
*
* (The MIT license)
* Copyright (c) 2017 Rob Morgan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated * documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package Phinx
* @subpackage Phinx\Config
*/

namespace Phinx\Config;

/**
* Trait implemented NamespaceAwareInterface.
* @package Phinx\Config
* @author Andrey N. Mokhov
*
* @method array getMigrationPaths()
*/
trait NamespaceAwareTrait
{
/**
* Get Namespace associated with path.
*
* @param string $path
* @return string|null
*/
public function getNamespaceByPath($path)
{
$path = realpath($path);

$paths = $this->getMigrationPaths();
$paths = array_map('realpath', $paths);

$key = array_search($path, $paths);

return is_int($key) ? null : trim($key, '\\');
}

}
13 changes: 9 additions & 4 deletions src/Phinx/Console/Command/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
namespace Phinx\Console\Command;

use Phinx\Config\NamespaceAwareInterface;
use Phinx\Migration\CreationInterface;
use Phinx\Util\Util;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -272,12 +273,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$contents = file_get_contents($altTemplate ?: $this->getMigrationTemplateFilename());
}

$config = $this->getConfig();
$namespace = $config instanceof NamespaceAwareInterface ? $config->getNamespaceByPath($path) : null;

// inject the class names appropriate to this migration
$classes = array(
'$useClassName' => $this->getConfig()->getMigrationBaseClassName(false),
'$className' => $className,
'$version' => Util::getVersionFromFileName($fileName),
'$baseClassName' => $this->getConfig()->getMigrationBaseClassName(true),
'$defineNamespace' => null !== $namespace ? ('namespace ' . $namespace . ';') : '',
'$useClassName' => $this->getConfig()->getMigrationBaseClassName(false),
'$className' => $className,
'$version' => Util::getVersionFromFileName($fileName),
'$baseClassName' => $this->getConfig()->getMigrationBaseClassName(true),
);
$contents = strtr($contents, $classes);

Expand Down
11 changes: 8 additions & 3 deletions src/Phinx/Console/Command/SeedCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
namespace Phinx\Console\Command;

use Phinx\Config\NamespaceAwareInterface;
use Phinx\Util\Util;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -173,10 +174,14 @@ protected function execute(InputInterface $input, OutputInterface $output)

// inject the class names appropriate to this seeder
$contents = file_get_contents($this->getSeedTemplateFilename());

$config = $this->getConfig();
$namespace = $config instanceof NamespaceAwareInterface ? $config->getNamespaceByPath($path) : null;
$classes = array(
'$useClassName' => 'Phinx\Seed\AbstractSeed',
'$className' => $className,
'$baseClassName' => 'AbstractSeed',
'$defineNamespace' => null !== $namespace ? ('namespace ' . $namespace . ';') : '',
'$useClassName' => 'Phinx\Seed\AbstractSeed',
'$className' => $className,
'$baseClassName' => 'AbstractSeed',
);
$contents = strtr($contents, $classes);

Expand Down
11 changes: 9 additions & 2 deletions src/Phinx/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
namespace Phinx\Migration;

use Phinx\Config\NamespaceAwareInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Phinx\Config\ConfigInterface;
Expand Down Expand Up @@ -650,8 +651,11 @@ public function getMigrations()
throw new \InvalidArgumentException(sprintf('Duplicate migration - "%s" has the same version as "%s"', $filePath, $versions[$version]->getVersion()));
}

$config = $this->getConfig();
$namespace = $config instanceof NamespaceAwareInterface ? $config->getNamespaceByPath(dirname($filePath)) : null;

// convert the filename to a class name
$class = Util::mapFileNameToClassName(basename($filePath));
$class = (null === $namespace ? '' : $namespace . '\\') . Util::mapFileNameToClassName(basename($filePath));

if (isset($fileNames[$class])) {
throw new \InvalidArgumentException(sprintf(
Expand Down Expand Up @@ -747,8 +751,11 @@ public function getSeeds()

foreach ($phpFiles as $filePath) {
if (Util::isValidSeedFileName(basename($filePath))) {
$config = $this->getConfig();
$namespace = $config instanceof NamespaceAwareInterface ? $config->getNamespaceByPath(dirname($filePath)) : null;

// convert the filename to a class name
$class = pathinfo($filePath, PATHINFO_FILENAME);
$class = (null === $namespace ? '' : $namespace . '\\') . pathinfo($filePath, PATHINFO_FILENAME);
$fileNames[$class] = basename($filePath);

// load the seed file
Expand Down
1 change: 1 addition & 0 deletions src/Phinx/Migration/Migration.template.php.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
$defineNamespace

use $useClassName;

Expand Down
1 change: 1 addition & 0 deletions src/Phinx/Seed/Seed.template.php.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
$defineNamespace

use $useClassName;

Expand Down

0 comments on commit 00c1a67

Please sign in to comment.