Moved the schema-locating code outside the AbstractCommand

This commit is contained in:
Kévin Gomez 2013-10-31 21:19:24 +00:00
parent 07530d09ac
commit c6087f7191
3 changed files with 87 additions and 50 deletions

View file

@ -19,7 +19,6 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\KernelInterface;
@ -207,39 +206,10 @@ abstract class AbstractCommand extends ContainerAwareCommand
protected function getFinalSchemas(KernelInterface $kernel, BundleInterface $bundle = null)
{
if (null !== $bundle) {
return $this->getSchemasFromBundle($bundle);
return $this->getSchemaLocator()->locateFromBundle($bundle);
}
$finalSchemas = array();
foreach ($kernel->getBundles() as $bundle) {
$finalSchemas = array_merge($finalSchemas, $this->getSchemasFromBundle($bundle));
}
return $finalSchemas;
}
/**
* @param \Symfony\Component\HttpKernel\Bundle\BundleInterface
*/
protected function getSchemasFromBundle(BundleInterface $bundle)
{
$finalSchemas = array();
if (is_dir($dir = $bundle->getPath().'/Resources/config')) {
$finder = new Finder();
$schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);
if (iterator_count($schemas)) {
foreach ($schemas as $schema) {
$logicalName = $this->transformToLogicalName($schema, $bundle);
$finalSchema = new \SplFileInfo($this->getFileLocator()->locate($logicalName));
$finalSchemas[(string) $finalSchema] = array($bundle, $finalSchema);
}
}
}
return $finalSchemas;
return $this->getSchemaLocator()->locateFromBundles($kernel->getBundles());
}
/*
@ -342,25 +312,9 @@ EOT;
/**
* @return \Symfony\Component\Config\FileLocatorInterface
*/
protected function getFileLocator()
protected function getSchemaLocator()
{
return $this->getContainer()->get('file_locator');
}
/**
* @param \SplFileInfo $schema
* @param BundleInterface $bundle
* @return string
*/
protected function transformToLogicalName(\SplFileInfo $schema, BundleInterface $bundle)
{
$schemaPath = str_replace(
$bundle->getPath(). DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR,
'',
$schema->getRealPath()
);
return sprintf('@%s/Resources/config/%s', $bundle->getName(), $schemaPath);
return $this->getContainer()->get('propel.schema_locator');
}
/**

View file

@ -6,5 +6,13 @@
<parameters>
<parameter key="propel.dbal.default_connection">default</parameter>
<parameter key="propel.schema_locator.class">Propel\PropelBundle\Service\SchemaLocator</parameter>
</parameters>
<services>
<service id="propel.schema_locator" class="%propel.schema_locator.class%">
<argument type="service" id="file_locator" />
</service>
</services>
</container>

75
Service/SchemaLocator.php Normal file
View file

@ -0,0 +1,75 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Service;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
class SchemaLocator
{
protected $fileLocator;
public function __construct(FileLocatorInterface $fileLocator)
{
$this->fileLocator = $fileLocator;
}
public function locateFromBundles(array $bundles)
{
$schemas = array();
foreach ($bundles as $bundle) {
$schemas = array_merge($schemas, $this->locateFromBundle($bundle));
}
return $schemas;
}
/**
* @param \Symfony\Component\HttpKernel\Bundle\BundleInterface
*/
public function locateFromBundle(BundleInterface $bundle)
{
$finalSchemas = array();
if (is_dir($dir = $bundle->getPath().'/Resources/config')) {
$finder = new Finder();
$schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir);
if (iterator_count($schemas)) {
foreach ($schemas as $schema) {
$logicalName = $this->transformToLogicalName($schema, $bundle);
$finalSchema = new \SplFileInfo($this->fileLocator->locate($logicalName));
$finalSchemas[(string) $finalSchema] = array($bundle, $finalSchema);
}
}
}
return $finalSchemas;
}
/**
* @param \SplFileInfo $schema
* @param BundleInterface $bundle
* @return string
*/
protected function transformToLogicalName(\SplFileInfo $schema, BundleInterface $bundle)
{
$schemaPath = str_replace(
$bundle->getPath(). DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR,
'',
$schema->getRealPath()
);
return sprintf('@%s/Resources/config/%s', $bundle->getName(), $schemaPath);
}
}