This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\EasyExtendsBundle\Generator;
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
use Symfony\Component\Console\Output\OutputInterface;
class BundleGenerator implements GeneratorInterface
{
/**
* @var string
*/
protected $bundleTemplate;
public function __construct()
{
$this->bundleTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/bundle/bundle.mustache');
}
/**
* {@inheritdoc}
*/
public function generate(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$this->generateBundleDirectory($output, $bundleMetadata);
$this->generateBundleFile($output, $bundleMetadata);
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
protected function generateBundleDirectory(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$directories = array(
'',
'Resources/config/serializer',
'Resources/config/doctrine',
'Resources/config/routing',
'Resources/views',
'Command',
'DependencyInjection',
'Entity',
'Document',
'PHPCR',
'Controller',
);
foreach ($directories as $directory) {
$dir = sprintf('%s/%s', $bundleMetadata->getExtendedDirectory(), $directory);
if (!is_dir($dir)) {
$output->writeln(sprintf(' > generating bundle directory <comment>%s</comment>', $dir));
mkdir($dir, 0755, true);
}
}
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
protected function generateBundleFile(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$application = explode('\\', $bundleMetadata->getExtendedNamespace())[0];
$file = sprintf('%s/%s%s.php', $bundleMetadata->getExtendedDirectory(), $application, $bundleMetadata->getName());
if (is_file($file)) {
return;
}
$output->writeln(sprintf(' > generating bundle file <comment>%s</comment>', $file));
$string = Mustache::replace($this->getBundleTemplate(), array(
'application' => $application,
'bundle' => $bundleMetadata->getName(),
'namespace' => $bundleMetadata->getExtendedNamespace(),
));
file_put_contents($file, $string);
}
/**
* @return string
*/
protected function getBundleTemplate()
{
return $this->bundleTemplate;
}
}

View File

@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\EasyExtendsBundle\Generator;
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
use Symfony\Component\Console\Output\OutputInterface;
interface GeneratorInterface
{
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
public function generate(OutputInterface $output, BundleMetadata $bundleMetadata);
}

View File

@@ -0,0 +1,41 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\EasyExtendsBundle\Generator;
class Mustache
{
/**
* @param string $string
* @param array $parameters
*
* @return mixed
*/
public static function replace($string, array $parameters)
{
$replacer = function ($match) use ($parameters) {
return isset($parameters[$match[1]]) ? $parameters[$match[1]] : $match[0];
};
return preg_replace_callback('/{{\s*(.+?)\s*}}/', $replacer, $string);
}
/**
* @param string $file
* @param array $parameters
*
* @return mixed
*/
public static function replaceFromFile($file, array $parameters)
{
return self::replace(file_get_contents($file), $parameters);
}
}

View File

@@ -0,0 +1,172 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\EasyExtendsBundle\Generator;
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
use Symfony\Component\Console\Output\OutputInterface;
class OdmGenerator implements GeneratorInterface
{
/**
* @var string
*/
protected $documentTemplate;
/**
* @var string
*/
protected $documentRepositoryTemplate;
public function __construct()
{
$this->documentTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/odm/document.mustache');
$this->documentRepositoryTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/odm/repository.mustache');
}
/**
* {@inheritdoc}
*/
public function generate(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$this->generateMappingDocumentFiles($output, $bundleMetadata);
$this->generateDocumentFiles($output, $bundleMetadata);
$this->generateDocumentRepositoryFiles($output, $bundleMetadata);
}
/**
* @return string
*/
public function getDocumentTemplate()
{
return $this->documentTemplate;
}
/**
* @return string
*/
public function getDocumentRepositoryTemplate()
{
return $this->documentRepositoryTemplate;
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
protected function generateMappingDocumentFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$output->writeln(' - Copy document files');
$files = $bundleMetadata->getOdmMetadata()->getDocumentMappingFiles();
foreach ($files as $file) {
// copy mapping definition
$fileName = substr($file->getFileName(), 0, strrpos($file->getFileName(), '.'));
$dest_file = sprintf('%s/%s', $bundleMetadata->getOdmMetadata()->getExtendedMappingDocumentDirectory(), $fileName);
$src_file = sprintf('%s/%s.skeleton', $bundleMetadata->getOdmMetadata()->getMappingDocumentDirectory(), $fileName);
if (is_file($dest_file)) {
$output->writeln(sprintf(' ~ <info>%s</info>', $fileName));
} else {
$output->writeln(sprintf(' + <info>%s</info>', $fileName));
$mappingEntityTemplate = file_get_contents($src_file);
$string = Mustache::replace($mappingEntityTemplate, array(
'namespace' => $bundleMetadata->getExtendedNamespace(),
));
file_put_contents($dest_file, $string);
}
}
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
protected function generateDocumentFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$output->writeln(' - Generating document files');
$names = $bundleMetadata->getOdmMetadata()->getDocumentNames();
foreach ($names as $name) {
$extendedName = $name;
$dest_file = sprintf('%s/%s.php', $bundleMetadata->getOdmMetadata()->getExtendedDocumentDirectory(), $name);
$src_file = sprintf('%s/%s.php', $bundleMetadata->getOdmMetadata()->getDocumentDirectory(), $extendedName);
if (!is_file($src_file)) {
$extendedName = 'Base'.$name;
$src_file = sprintf('%s/%s.php', $bundleMetadata->getOdmMetadata()->getDocumentDirectory(), $extendedName);
if (!is_file($src_file)) {
$output->writeln(sprintf(' ! <info>%s</info>', $extendedName));
continue;
}
}
if (is_file($dest_file)) {
$output->writeln(sprintf(' ~ <info>%s</info>', $name));
} else {
$output->writeln(sprintf(' + <info>%s</info>', $name));
$string = Mustache::replace($this->getDocumentTemplate(), array(
'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
'name' => $name != $extendedName ? $extendedName : $name,
'class' => $name,
'extended_name' => $name == $extendedName ? 'Base'.$name : $extendedName,
'namespace' => $bundleMetadata->getNamespace(),
));
file_put_contents($dest_file, $string);
}
}
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
protected function generateDocumentRepositoryFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$output->writeln(' - Generating document repository files');
$names = $bundleMetadata->getOdmMetadata()->getDocumentNames();
foreach ($names as $name) {
$dest_file = sprintf('%s/%sRepository.php', $bundleMetadata->getOdmMetadata()->getExtendedDocumentDirectory(), $name);
$src_file = sprintf('%s/Base%sRepository.php', $bundleMetadata->getOdmMetadata()->getDocumentDirectory(), $name);
if (!is_file($src_file)) {
$output->writeln(sprintf(' ! <info>%sRepository</info>', $name));
continue;
}
if (is_file($dest_file)) {
$output->writeln(sprintf(' ~ <info>%sRepository</info>', $name));
} else {
$output->writeln(sprintf(' + <info>%sRepository</info>', $name));
$string = Mustache::replace($this->getDocumentRepositoryTemplate(), array(
'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
'name' => $name,
'namespace' => $bundleMetadata->getNamespace(),
));
file_put_contents($dest_file, $string);
}
}
}
}

View File

@@ -0,0 +1,172 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\EasyExtendsBundle\Generator;
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
use Symfony\Component\Console\Output\OutputInterface;
class OrmGenerator implements GeneratorInterface
{
/**
* @var string
*/
protected $entityTemplate;
/**
* @var string
*/
protected $entityRepositoryTemplate;
public function __construct()
{
$this->entityTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/orm/entity.mustache');
$this->entityRepositoryTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/orm/repository.mustache');
}
/**
* {@inheritdoc}
*/
public function generate(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$this->generateMappingEntityFiles($output, $bundleMetadata);
$this->generateEntityFiles($output, $bundleMetadata);
$this->generateEntityRepositoryFiles($output, $bundleMetadata);
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
public function generateMappingEntityFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$output->writeln(' - Copy entity files');
$files = $bundleMetadata->getOrmMetadata()->getEntityMappingFiles();
foreach ($files as $file) {
// copy mapping definition
$fileName = substr($file->getFileName(), 0, strrpos($file->getFileName(), '.'));
$dest_file = sprintf('%s/%s', $bundleMetadata->getOrmMetadata()->getExtendedMappingEntityDirectory(), $fileName);
$src_file = sprintf('%s/%s', $bundleMetadata->getOrmMetadata()->getMappingEntityDirectory(), $file->getFileName());
if (is_file($dest_file)) {
$output->writeln(sprintf(' ~ <info>%s</info>', $fileName));
} else {
$output->writeln(sprintf(' + <info>%s</info>', $fileName));
$mappingEntityTemplate = file_get_contents($src_file);
$string = Mustache::replace($mappingEntityTemplate, array(
'namespace' => $bundleMetadata->getExtendedNamespace(),
));
file_put_contents($dest_file, $string);
}
}
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
public function generateEntityFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$output->writeln(' - Generating entity files');
$names = $bundleMetadata->getOrmMetadata()->getEntityNames();
foreach ($names as $name) {
$extendedName = $name;
$dest_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getExtendedEntityDirectory(), $name);
$src_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $extendedName);
if (!is_file($src_file)) {
$extendedName = 'Base'.$name;
$src_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $extendedName);
if (!is_file($src_file)) {
$output->writeln(sprintf(' ! <info>%s</info>', $extendedName));
continue;
}
}
if (is_file($dest_file)) {
$output->writeln(sprintf(' ~ <info>%s</info>', $name));
} else {
$output->writeln(sprintf(' + <info>%s</info>', $name));
$string = Mustache::replace($this->getEntityTemplate(), array(
'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
'name' => $name != $extendedName ? $extendedName : $name,
'class' => $name,
'extended_name' => $name == $extendedName ? 'Base'.$name : $extendedName,
'namespace' => $bundleMetadata->getNamespace(),
));
file_put_contents($dest_file, $string);
}
}
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
public function generateEntityRepositoryFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$output->writeln(' - Generating entity repository files');
$names = $bundleMetadata->getOrmMetadata()->getEntityNames();
foreach ($names as $name) {
$dest_file = sprintf('%s/%sRepository.php', $bundleMetadata->getOrmMetadata()->getExtendedEntityDirectory(), $name);
$src_file = sprintf('%s/Base%sRepository.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $name);
if (!is_file($src_file)) {
$output->writeln(sprintf(' ! <info>%sRepository</info>', $name));
continue;
}
if (is_file($dest_file)) {
$output->writeln(sprintf(' ~ <info>%sRepository</info>', $name));
} else {
$output->writeln(sprintf(' + <info>%sRepository</info>', $name));
$string = Mustache::replace($this->getEntityRepositoryTemplate(), array(
'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
'name' => $name,
'namespace' => $bundleMetadata->getNamespace(),
));
file_put_contents($dest_file, $string);
}
}
}
/**
* @return string
*/
public function getEntityTemplate()
{
return $this->entityTemplate;
}
/**
* @return string
*/
public function getEntityRepositoryTemplate()
{
return $this->entityRepositoryTemplate;
}
}

View File

@@ -0,0 +1,172 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\EasyExtendsBundle\Generator;
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
use Symfony\Component\Console\Output\OutputInterface;
class PHPCRGenerator implements GeneratorInterface
{
/**
* @var string
*/
protected $DocumentTemplate;
/**
* @var string
*/
protected $DocumentRepositoryTemplate;
public function __construct()
{
$this->DocumentTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/phpcr/document.mustache');
$this->DocumentRepositoryTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/phpcr/repository.mustache');
}
/**
* {@inheritdoc}
*/
public function generate(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$this->generateMappingDocumentFiles($output, $bundleMetadata);
$this->generateDocumentFiles($output, $bundleMetadata);
$this->generateDocumentRepositoryFiles($output, $bundleMetadata);
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
public function generateMappingDocumentFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$output->writeln(' - Copy Document files');
$files = $bundleMetadata->getPhpcrMetadata()->getDocumentMappingFiles();
foreach ($files as $file) {
// copy mapping definition
$fileName = substr($file->getFileName(), 0, strrpos($file->getFileName(), '.'));
$dest_file = sprintf('%s/%s', $bundleMetadata->getPhpcrMetadata()->getExtendedMappingDocumentDirectory(), $fileName);
$src_file = sprintf('%s/%s', $bundleMetadata->getPhpcrMetadata()->getMappingDocumentDirectory(), $file->getFileName());
if (is_file($dest_file)) {
$output->writeln(sprintf(' ~ <info>%s</info>', $fileName));
} else {
$output->writeln(sprintf(' + <info>%s</info>', $fileName));
$mappingEntityTemplate = file_get_contents($src_file);
$string = Mustache::replace($mappingEntityTemplate, array(
'namespace' => $bundleMetadata->getExtendedNamespace(),
));
file_put_contents($dest_file, $string);
}
}
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
public function generateDocumentFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$output->writeln(' - Generating Document files');
$names = $bundleMetadata->getPhpcrMetadata()->getDocumentNames();
foreach ($names as $name) {
$extendedName = $name;
$dest_file = sprintf('%s/%s.php', $bundleMetadata->getPhpcrMetadata()->getExtendedDocumentDirectory(), $name);
$src_file = sprintf('%s/%s.php', $bundleMetadata->getPhpcrMetadata()->getDocumentDirectory(), $extendedName);
if (!is_file($src_file)) {
$extendedName = 'Base'.$name;
$src_file = sprintf('%s/%s.php', $bundleMetadata->getPhpcrMetadata()->getDocumentDirectory(), $extendedName);
if (!is_file($src_file)) {
$output->writeln(sprintf(' ! <info>%s</info>', $extendedName));
continue;
}
}
if (is_file($dest_file)) {
$output->writeln(sprintf(' ~ <info>%s</info>', $name));
} else {
$output->writeln(sprintf(' + <info>%s</info>', $name));
$string = Mustache::replace($this->getDocumentTemplate(), array(
'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
'name' => $name != $extendedName ? $extendedName : $name,
'class' => $name,
'extended_name' => $name == $extendedName ? 'Base'.$name : $extendedName,
'namespace' => $bundleMetadata->getNamespace(),
));
file_put_contents($dest_file, $string);
}
}
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
public function generateDocumentRepositoryFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$output->writeln(' - Generating Document repository files');
$names = $bundleMetadata->getPhpcrMetadata()->getDocumentNames();
foreach ($names as $name) {
$dest_file = sprintf('%s/%sRepository.php', $bundleMetadata->getPhpcrMetadata()->getExtendedDocumentDirectory(), $name);
$src_file = sprintf('%s/Base%sRepository.php', $bundleMetadata->getPhpcrMetadata()->getDocumentDirectory(), $name);
if (!is_file($src_file)) {
$output->writeln(sprintf(' ! <info>%sRepository</info>', $name));
continue;
}
if (is_file($dest_file)) {
$output->writeln(sprintf(' ~ <info>%sRepository</info>', $name));
} else {
$output->writeln(sprintf(' + <info>%sRepository</info>', $name));
$string = Mustache::replace($this->getDocumentRepositoryTemplate(), array(
'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
'name' => $name,
'namespace' => $bundleMetadata->getNamespace(),
));
file_put_contents($dest_file, $string);
}
}
}
/**
* @return string
*/
public function getDocumentTemplate()
{
return $this->DocumentTemplate;
}
/**
* @return string
*/
public function getDocumentRepositoryTemplate()
{
return $this->DocumentRepositoryTemplate;
}
}

View File

@@ -0,0 +1,125 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\EasyExtendsBundle\Generator;
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
use Symfony\Component\Console\Output\OutputInterface;
class SerializerGenerator implements GeneratorInterface
{
/**
* @var string
*/
protected $entitySerializerTemplate;
/**
* @var string
*/
protected $documentSerializerTemplate;
public function __construct()
{
$this->entitySerializerTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/serializer/entity.mustache');
$this->documentSerializerTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/serializer/document.mustache');
}
/**
* {@inheritdoc}
*/
public function generate(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$this->generateOrmSerializer($output, $bundleMetadata);
$this->generateOdmSerializer($output, $bundleMetadata);
$this->generatePhpcrSerializer($output, $bundleMetadata);
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
protected function generateOrmSerializer(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$names = $bundleMetadata->getOrmMetadata()->getEntityNames();
if (is_array($names) && count($names) > 0) {
$output->writeln(' - Generating ORM serializer files');
foreach ($names as $name) {
$destFile = sprintf('%s/Entity.%s.xml', $bundleMetadata->getOrmMetadata()->getExtendedSerializerDirectory(), $name);
$this->writeSerializerFile($output, $bundleMetadata, $this->entitySerializerTemplate, $destFile, $name);
}
}
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
protected function generateOdmSerializer(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$names = $bundleMetadata->getOdmMetadata()->getDocumentNames();
if (is_array($names) && count($names) > 0) {
$output->writeln(' - Generating ODM serializer files');
foreach ($names as $name) {
$destFile = sprintf('%s/Document.%s.xml', $bundleMetadata->getOdmMetadata()->getExtendedSerializerDirectory(), $name);
$this->writeSerializerFile($output, $bundleMetadata, $this->documentSerializerTemplate, $destFile, $name);
}
}
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
*/
protected function generatePhpcrSerializer(OutputInterface $output, BundleMetadata $bundleMetadata)
{
$names = $bundleMetadata->getPhpcrMetadata()->getDocumentNames();
if (is_array($names) && count($names) > 0) {
$output->writeln(' - Generating PHPCR serializer files');
foreach ($names as $name) {
$destFile = sprintf('%s/Document.%s.xml', $bundleMetadata->getPhpcrMetadata()->getExtendedSerializerDirectory(), $name);
$this->writeSerializerFile($output, $bundleMetadata, $this->documentSerializerTemplate, $destFile, $name);
}
}
}
/**
* @param OutputInterface $output
* @param BundleMetadata $bundleMetadata
* @param string $template
* @param string $destFile
* @param string $name
*/
protected function writeSerializerFile(OutputInterface $output, BundleMetadata $bundleMetadata, $template, $destFile, $name)
{
if (is_file($destFile)) {
$output->writeln(sprintf(' ~ <info>%s</info>', $name));
} else {
$output->writeln(sprintf(' + <info>%s</info>', $name));
$string = Mustache::replace($template, array(
'name' => $name,
'namespace' => $bundleMetadata->getExtendedNamespace(),
'root_name' => strtolower(preg_replace('/[A-Z]/', '_\\0', $name)),
));
file_put_contents($destFile, $string);
}
}
}