Last active
August 29, 2015 14:25
-
-
Save shengjie/ba401f72c6a13b33cdcd to your computer and use it in GitHub Desktop.
generate xml repository service definition for all entities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author: shengjie | |
* @date: 16/07/15 16:10 | |
* @file: GenerateCommand.php | |
* @Licence: MIT | |
*/ | |
namespace AppBundle\Command; | |
use Doctrine\ORM\Mapping\ClassMetadata; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class RepositoryGeneratorCommand extends ContainerAwareCommand | |
{ | |
/** | |
* Configures the current command. | |
*/ | |
protected function configure() | |
{ | |
$this->setName("app:generate:repository_configuration"); | |
} | |
protected function getServiceNameFromClassName($className) | |
{ | |
$parts = explode('\\', $className); | |
$shortClassName = end($parts); | |
$underscored = strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($shortClassName))); | |
$serviceName = "repository." . $underscored; | |
return $serviceName; | |
} | |
protected function getTagsFromClassName($class) | |
{ | |
$tags = []; | |
return implode('', array_map(function ($name) { | |
return '<tag name="' . $name . '"/>"'; | |
}, $tags)); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$em = $this->getContainer()->get('doctrine.orm.default_entity_manager'); | |
/** @var ClassMetadata[] $metaDatas */ | |
$metaDatas = $em->getMetadataFactory()->getAllMetadata(); | |
$skip = 0; | |
$lines = ['<?xml version="1.0" ?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<services>']; | |
foreach ($metaDatas as $metadata) { | |
if ($metadata->isMappedSuperclass) continue; | |
$repositoryClassName = $metadata->customRepositoryClassName; | |
if ($repositoryClassName === null) { | |
$configuration = $em->getConfiguration(); | |
$repositoryClassName = $configuration->getDefaultRepositoryClassName(); | |
} | |
$lines[] = sprintf( | |
"\t\t" . '<service id="%s" class="%s" factory-service="doctrine.orm.default_entity_manager" factory-method="getRepository"><argument>%s</argument>%s</service>', | |
$this->getServiceNameFromClassName($metadata->name), | |
ltrim($repositoryClassName, '\\'), | |
$metadata->name, | |
$this->getTagsFromClassName($metadata->name) | |
); | |
} | |
$lines[] = '</services> | |
</container>'; | |
file_put_contents(__DIR__ . "/../Resources/config/services.repository.xml", implode("\n", $lines)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment