diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57f1cb2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ \ No newline at end of file diff --git a/Command/SonataGenerateAdminCommand.php b/Command/SonataGenerateAdminCommand.php deleted file mode 100644 index 985c208..0000000 --- a/Command/SonataGenerateAdminCommand.php +++ /dev/null @@ -1,419 +0,0 @@ - - * @author Simon Cosandey - */ -class SonataGenerateAdminCommand extends GenerateAdminCommand -{ - /** @var string[] */ - private $managerTypes; - - /** - * {@inheritDoc} - */ - public function configure() - { - parent::configure(); - - $this - ->setName('glavweb:sonata:admin:generate') - ; - } - - /** - * {@inheritDoc} - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $modelClass = Validators::validateClass($input->getArgument('model')); - $modelClassBasename = current(array_slice(explode('\\', $modelClass), -1)); - - $bundle = $this->getBundle($input->getOption('bundle') ?: $this->getBundleNameFromClass($modelClass)); - $adminClassBasename = $input->getOption('admin') ?: $modelClassBasename . 'Admin'; - $adminClassBasename = Validators::validateAdminClassBasename($adminClassBasename); - - $managerType = $input->getOption('manager') ?: $this->getDefaultManagerType(); - $modelManager = $this->getModelManager($managerType); - - $skeletonDirectory = __DIR__ . '/../Resources/skeleton/Sonata'; - $adminGenerator = new AdminGenerator($modelManager, $skeletonDirectory); - - $id = $input->getOption('id') ?: $this->getAdminServiceId($bundle->getName(), $adminClassBasename); - - try { - $adminGenerator->generate($bundle, $adminClassBasename, $modelClass, $id); - $output->writeln(sprintf( - '%sThe admin class "%s" has been generated under the file "%s".', - PHP_EOL, - $adminGenerator->getClass(), - realpath($adminGenerator->getFile()) - )); - } catch (\Exception $e) { - $this->writeError($output, $e->getMessage()); - } - - if ($controllerClassBasename = $input->getOption('controller')) { - $controllerClassBasename = Validators::validateControllerClassBasename($controllerClassBasename); - $controllerGenerator = new ControllerGenerator($skeletonDirectory); - - try { - $controllerGenerator->generate($bundle, $controllerClassBasename); - $output->writeln(sprintf( - '%sThe controller class "%s" has been generated under the file "%s".', - PHP_EOL, - $controllerGenerator->getClass(), - realpath($controllerGenerator->getFile()) - )); - } catch (\Exception $e) { - $this->writeError($output, $e->getMessage()); - } - } - - if ($servicesFile = $input->getOption('services')) { - $adminClass = $adminGenerator->getClass(); - $file = sprintf('%s/Resources/config/%s', $bundle->getPath(), $servicesFile); - $servicesManipulator = new ServicesManipulator($file); - - $controllerName = $controllerClassBasename - ? sprintf('%s:%s', $bundle->getName(), substr($controllerClassBasename, 0, -10)) - : 'SonataAdminBundle:CRUD' - ; - - try { - $servicesManipulator->addResource($id, $modelClass, $adminClass, $controllerName, $managerType); - - $output->writeln(sprintf( - '%sThe service "%s" has been appended to the file "%s".', - PHP_EOL, - $id, - realpath($file) - )); - } catch (\Exception $e) { - $this->writeError($output, $e->getMessage()); - } - } - - // Translation file - try { - $translationGenerator = new TranslationGenerator($modelManager, $skeletonDirectory); - $translationGenerator->generate($bundle, $modelClass, $id); - $output->writeln(sprintf( - '%sThe translation file "%s" has been generated.', - PHP_EOL, - realpath($controllerGenerator->getFile()) - )); - - } catch (\Exception $e) { - $this->writeError($output, $e->getMessage()); - } - - - return 0; - } - - /** - * {@inheritDoc} - */ - protected function interact(InputInterface $input, OutputInterface $output) - { - $questionHelper = $this->getQuestionHelper(); - $questionHelper->writeSection($output, 'Welcome to the Sonata admin generator'); - $modelClass = $this->askAndValidate( - $input, - $output, - 'The fully qualified model class', - $input->getArgument('model'), - 'Sonata\AdminBundle\Command\Validators::validateClass' - ); - $modelClassBasename = current(array_slice(explode('\\', $modelClass), -1)); - $bundleName = $this->askAndValidate( - $input, - $output, - 'The bundle name', - $input->getOption('bundle') ?: $this->getBundleNameFromClass($modelClass), - 'Sensio\Bundle\GeneratorBundle\Command\Validators::validateBundleName' - ); - $adminClassBasename = $this->askAndValidate( - $input, - $output, - 'The admin class basename', - $input->getOption('admin') ?: $modelClassBasename . 'Admin', - 'Sonata\AdminBundle\Command\Validators::validateAdminClassBasename' - ); - - if (count($this->getAvailableManagerTypes()) > 1) { - $managerType = $this->askAndValidate( - $input, - $output, - 'The manager type', - $input->getOption('manager') ?: $this->getDefaultManagerType(), - array($this, 'validateManagerType') - ); - $input->setOption('manager', $managerType); - } - - if ($this->askConfirmation($input, $output, 'Do you want to generate a controller', 'no', '?')) { - $controllerClassBasename = $this->askAndValidate( - $input, - $output, - 'The controller class basename', - $input->getOption('controller') ?: $modelClassBasename . 'AdminController', - 'Sonata\AdminBundle\Command\Validators::validateControllerClassBasename' - ); - $input->setOption('controller', $controllerClassBasename); - } - - if ($this->askConfirmation($input, $output, 'Do you want to update the services YAML configuration file', 'yes', '?')) { - $path = $this->getBundle($bundleName)->getPath() . '/Resources/config/'; - $servicesFile = $this->askAndValidate( - $input, - $output, - 'The services YAML configuration file', - is_file($path . 'admin.yml') ? 'admin.yml' : 'services.yml', - 'Sonata\AdminBundle\Command\Validators::validateServicesFile' - ); - $id = $this->askAndValidate( - $input, - $output, - 'The admin service ID', - $this->getAdminServiceId($bundleName, $adminClassBasename), - 'Sonata\AdminBundle\Command\Validators::validateServiceId' - ); - $input->setOption('services', $servicesFile); - $input->setOption('id', $id); - } - - $input->setArgument('model', $modelClass); - $input->setOption('admin', $adminClassBasename); - $input->setOption('bundle', $bundleName); - } - - /** - * @param string $managerType - * - * @return string - * - * @throws \InvalidArgumentException - */ - public function validateManagerType($managerType) - { - $managerTypes = $this->getAvailableManagerTypes(); - - if (!isset($managerTypes[$managerType])) { - throw new \InvalidArgumentException(sprintf( - 'Invalid manager type "%s". Available manager types are "%s".', - $managerType, - implode('", "', $managerTypes) - )); - } - - return $managerType; - } - - /** - * @param string $class - * - * @return string|null - * - * @throws \InvalidArgumentException - */ - private function getBundleNameFromClass($class) - { - $application = $this->getApplication(); - /* @var $application Application */ - - foreach ($application->getKernel()->getBundles() as $bundle) { - if (strpos($class, $bundle->getNamespace() . '\\') === 0) { - return $bundle->getName(); - }; - } - - return null; - } - - /** - * @param string $name - * - * @return BundleInterface - */ - private function getBundle($name) - { - return $this->getKernel()->getBundle($name); - } - - /** - * @param OutputInterface $output - * @param string $message - */ - private function writeError(OutputInterface $output, $message) - { - $output->writeln(sprintf("\n%s", $message)); - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * @param string $questionText - * @param mixed $default - * @param callable $validator - * - * @return mixed - */ - private function askAndValidate(InputInterface $input, OutputInterface $output, $questionText, $default, $validator) - { - $questionHelper = $this->getQuestionHelper(); - - if ($questionHelper instanceof DialogHelper) { - // @todo remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3 - - return $questionHelper->askAndValidate($output, $questionHelper->getQuestion($questionText, $default), $validator, false, $default); - } - - $question = new Question($questionHelper->getQuestion($questionText, $default), $default); - - $question->setValidator($validator); - - return $questionHelper->ask($input, $output, $question); - } - - private function askConfirmation(InputInterface $input, OutputInterface $output, $questionText, $default, $separator) - { - $questionHelper = $this->getQuestionHelper(); - - if ($questionHelper instanceof DialogHelper) { - // @todo remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3 - $question = $questionHelper->getQuestion($questionText, $default, $separator); - - return $questionHelper->askConfirmation($output, $question, ($default === 'no' ? false : true)); - } - - $question = new ConfirmationQuestion($questionHelper->getQuestion( - $questionText, - $default, - $separator - ), ($default === 'no' ? false : true)); - - return $questionHelper->ask($input, $output, $question); - } - - /** - * @return string - * - * @throws \RuntimeException - */ - private function getDefaultManagerType() - { - if (!$managerTypes = $this->getAvailableManagerTypes()) { - throw new \RuntimeException('There are no model managers registered.'); - } - - return current($managerTypes); - } - - /** - * @param string $managerType - * - * @return ModelManagerInterface - */ - private function getModelManager($managerType) - { - return $this->getContainer()->get('sonata.admin.manager.' . $managerType); - } - - /** - * @param string $bundleName - * @param string $adminClassBasename - * - * @return string - */ - private function getAdminServiceId($bundleName, $adminClassBasename) - { - $suffix = substr($adminClassBasename, -5) == 'Admin' ? substr($adminClassBasename, 0, -5) : $adminClassBasename; - $suffix = str_replace('\\', '.', $suffix); - - return Container::underscore($suffix); - } - - /** - * @return string[] - */ - private function getAvailableManagerTypes() - { - $container = $this->getContainer(); - - if (!$container instanceof Container) { - return array(); - } - - if ($this->managerTypes === null) { - $this->managerTypes = array(); - - foreach ($container->getServiceIds() as $id) { - if (strpos($id, 'sonata.admin.manager.') === 0) { - $managerType = substr($id, 21); - $this->managerTypes[$managerType] = $managerType; - } - } - } - - return $this->managerTypes; - } - - /** - * @return KernelInterface - */ - private function getKernel() - { - $application = $this->getApplication(); - /* @var $application Application */ - - return $application->getKernel(); - } - - /** - * @return QuestionHelper|DialogHelper - */ - private function getQuestionHelper() - { - if (class_exists('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')) { - // @todo remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3 - $questionHelper = $this->getHelper('dialog'); - - if (!$questionHelper instanceof DialogHelper) { - $questionHelper = new DialogHelper(); - $this->getHelperSet()->set($questionHelper); - } - } else { - $questionHelper = $this->getHelper('question'); - - if (!$questionHelper instanceof QuestionHelper) { - $questionHelper = new QuestionHelper(); - $this->getHelperSet()->set($questionHelper); - } - - } - - return $questionHelper; - } -} diff --git a/Generator/Sonata/AdminGenerator.php b/Generator/Sonata/AdminGenerator.php deleted file mode 100644 index ff75423..0000000 --- a/Generator/Sonata/AdminGenerator.php +++ /dev/null @@ -1,82 +0,0 @@ - - * @author Simon Cosandey - */ -class AdminGenerator extends Generator -{ - /** @var ModelManagerInterface */ - private $modelManager; - - /** @var string|null */ - private $class; - - /** @var string|null */ - private $file; - - /** - * @param ModelManagerInterface $modelManager - * @param array|string $skeletonDirectories - */ - public function __construct(ModelManagerInterface $modelManager, $skeletonDirectories) - { - $this->modelManager = $modelManager; - $this->setSkeletonDirs($skeletonDirectories); - } - - /** - * @param BundleInterface $bundle - * @param string $adminClassBasename - * @param string $modelClass - * @param $serviceId - */ - public function generate(BundleInterface $bundle, $adminClassBasename, $modelClass, $serviceId) - { - $this->class = sprintf('%s\Admin\%s', $bundle->getNamespace(), $adminClassBasename); - $this->file = sprintf('%s/Admin/%s.php', $bundle->getPath(), str_replace('\\', '/', $adminClassBasename)); - $parts = explode('\\', $this->class); - - if (file_exists($this->file)) { - throw new \RuntimeException(sprintf( - 'Unable to generate the admin class "%s". The file "%s" already exists.', - $this->class, - realpath($this->file) - )); - } - - $normalizedServiceId = str_replace('.', '_', $serviceId); - $baseRoutePattern = str_replace('_', '-', $normalizedServiceId); - $baseRouteName = $normalizedServiceId; - - $this->renderFile('Admin.php.twig', $this->file, array( - 'classBasename' => array_pop($parts), - 'namespace' => implode('\\', $parts), - 'fields' => $this->modelManager->getExportFields($modelClass), - 'baseRoutePattern' => $baseRoutePattern, - 'baseRouteName' => $baseRouteName - )); - } - - /** - * @return string|null - */ - public function getClass() - { - return $this->class; - } - - /** - * @return string|null - */ - public function getFile() - { - return $this->file; - } -} diff --git a/Generator/Sonata/ControllerGenerator.php b/Generator/Sonata/ControllerGenerator.php deleted file mode 100644 index 16e530a..0000000 --- a/Generator/Sonata/ControllerGenerator.php +++ /dev/null @@ -1,72 +0,0 @@ - - * @author Simon Cosandey - */ -class ControllerGenerator extends Generator -{ - /** @var string|null */ - private $class; - - /** @var string|null */ - private $file; - - /** - * @param array|string $skeletonDirectory - */ - public function __construct($skeletonDirectory) - { - $this->setSkeletonDirs($skeletonDirectory); - } - - /** - * @param BundleInterface $bundle - * @param string $controllerClassBasename - * @throws \RuntimeException - */ - public function generate(BundleInterface $bundle, $controllerClassBasename) - { - $this->class = sprintf('%s\Controller\%s', $bundle->getNamespace(), $controllerClassBasename); - $this->file = sprintf( - '%s/Controller/%s.php', - $bundle->getPath(), - str_replace('\\', '/', $controllerClassBasename) - ); - $parts = explode('\\', $this->class); - - if (file_exists($this->file)) { - throw new \RuntimeException(sprintf( - 'Unable to generate the admin controller class "%s". The file "%s" already exists.', - $this->class, - realpath($this->file) - )); - } - - $this->renderFile('AdminController.php.twig', $this->file, array( - 'classBasename' => array_pop($parts), - 'namespace' => implode('\\', $parts) - )); - } - - /** - * @return string|null - */ - public function getClass() - { - return $this->class; - } - - /** - * @return string|null - */ - public function getFile() - { - return $this->file; - } -} diff --git a/Generator/Sonata/TranslationGenerator.php b/Generator/Sonata/TranslationGenerator.php deleted file mode 100644 index 6e4f665..0000000 --- a/Generator/Sonata/TranslationGenerator.php +++ /dev/null @@ -1,79 +0,0 @@ - - * @author Simon Cosandey - */ -class TranslationGenerator extends Generator -{ - /** @var ModelManagerInterface */ - private $modelManager; - - /** @var string|null */ - private $file; - - /** - * @param ModelManagerInterface $modelManager - * @param array|string $skeletonDirectories - */ - public function __construct(ModelManagerInterface $modelManager, $skeletonDirectories) - { - $this->modelManager = $modelManager; - $this->setSkeletonDirs($skeletonDirectories); - } - - /** - * @param BundleInterface $bundle - * @param string $adminClassBasename - * @param string $modelClass - * @throws \RuntimeException - */ - public function generate(BundleInterface $bundle, $modelClass, $serviceId) - { - $normalizedServiceId = str_replace('.', '_', $serviceId); - $this->file = sprintf('%s/Resources/translations/%s.ru.yml', $bundle->getPath(), $normalizedServiceId); - - if (file_exists($this->file)) { - throw new \RuntimeException(sprintf( - 'Unable to generate the admin class "%s". The file "%s" already exists.', - $this->class, - realpath($this->file) - )); - } - - /** @var \Doctrine\ORM\Mapping\ClassMetadata $metadata */ - $metadata = $this->modelManager->getEntityManager($modelClass)->getClassMetadata($modelClass); - - $fields = array(); - $fieldNames = $metadata->getFieldNames(); - foreach ($fieldNames as $fieldName) { - $fieldMapping = $metadata->getFieldMapping($fieldName); - $columnName = $fieldMapping['columnName']; - $translate = isset($fieldMapping['options']['comment']) ? - $fieldMapping['options']['comment'] : - str_replace('_', ' ', ucfirst($columnName)) - ; - - $fields[$columnName] = $translate; - } - - $this->renderFile('translation.yml.twig', $this->file, array( - 'normalizedServiceId' => $normalizedServiceId, - 'fields' => $fields - )); - } - - /** - * @return string|null - */ - public function getFile() - { - return $this->file; - } -} diff --git a/GlavwebCoreBundle.php b/GlavwebCoreBundle.php index ae8bd9f..909810d 100644 --- a/GlavwebCoreBundle.php +++ b/GlavwebCoreBundle.php @@ -4,6 +4,10 @@ use Symfony\Component\HttpKernel\Bundle\Bundle; +/** + * Class GlavwebCoreBundle + * @package Glavweb\CoreBundle + */ class GlavwebCoreBundle extends Bundle { } diff --git a/Manipulator/Sonata/ServicesManipulator.php b/Manipulator/Sonata/ServicesManipulator.php deleted file mode 100644 index 8681b2a..0000000 --- a/Manipulator/Sonata/ServicesManipulator.php +++ /dev/null @@ -1,100 +0,0 @@ - - * @author Simon Cosandey - */ -class ServicesManipulator -{ - /** @var string */ - private $file; - - /** @var string */ - private $template = ' %s: - class: %s - arguments: [~, %s, %s] - tags: - - {name: sonata.admin, manager_type: %s, group: admin, label: %s, label_translator_strategy: "sonata.admin.label.strategy.underscore"} - calls: - - [ setTranslationDomain, ["%s"]] - -'; - - - - /** - * @param string $file - */ - public function __construct($file) - { - $this->file = (string) $file; - } - - /** - * @param string $serviceId - * @param string $modelClass - * @param string $adminClass - * @param string $controllerName - * @param string $managerType - * @throws \RuntimeException - */ - public function addResource($serviceId, $modelClass, $adminClass, $controllerName, $managerType) - { - $code = "services:\n"; - - if (is_file($this->file)) { - $code = rtrim(file_get_contents($this->file)); - $data = (array) Yaml::parse($code); - - if ($code !== '') { - $code .= "\n"; - } - - if (array_key_exists('services', $data)) { - if (array_key_exists($serviceId, (array) $data['services'])) { - throw new \RuntimeException(sprintf( - 'The service "%s" is already defined in the file "%s".', - $serviceId, - realpath($this->file) - )); - } - - if ($data['services'] !== null) { - $code .= "\n"; - } - } else { - $code .= $code === '' ? '' : "\n" . "services:\n"; - } - } - - $normalizedServiceId = str_replace('.', '_', $serviceId); - $label = 'dashboard.label_' . $normalizedServiceId; - $translationDomain = $normalizedServiceId; - - $code .= sprintf( - $this->template, - $serviceId, - $adminClass, - $modelClass, - $controllerName, - $managerType, - $label, - $translationDomain - ); - @mkdir(dirname($this->file), 0777, true); - - if (@file_put_contents($this->file, $code) === false) { - throw new \RuntimeException(sprintf( - 'Unable to append service "%s" to the file "%s". You will have to do it manually.', - $serviceId, - $this->file - )); - }; - } -} diff --git a/Resources/skeleton/Sonata/Admin.php.twig b/Resources/skeleton/Sonata/Admin.php.twig deleted file mode 100644 index efd3b5c..0000000 --- a/Resources/skeleton/Sonata/Admin.php.twig +++ /dev/null @@ -1,79 +0,0 @@ -add('{{ field }}') - {%- endfor %} -{% endset %} - -namespace {{ namespace }}; - -use Sonata\AdminBundle\Admin\Admin; -use Sonata\AdminBundle\Datagrid\DatagridMapper; -use Sonata\AdminBundle\Datagrid\ListMapper; -use Sonata\AdminBundle\Form\FormMapper; -use Sonata\AdminBundle\Show\ShowMapper; - -class {{ classBasename }} extends Admin -{ - /** - * The base route pattern used to generate the routing information - * - * @var string - */ - protected $baseRoutePattern = '{{ baseRoutePattern }}'; - - /** - * The base route name used to generate the routing information - * - * @var string - */ - protected $baseRouteName = '{{ baseRouteName }}'; - - /** - * @param DatagridMapper $datagridMapper - */ - protected function configureDatagridFilters(DatagridMapper $datagridMapper) - { - $datagridMapper - {{- code }} - ; - } - - /** - * @param ListMapper $listMapper - */ - protected function configureListFields(ListMapper $listMapper) - { - $listMapper - {{- code }} - ->add('_action', 'actions', array( - 'actions' => array( - 'show' => array(), - 'edit' => array(), - 'delete' => array(), - ) - )) - ; - } - - /** - * @param FormMapper $formMapper - */ - protected function configureFormFields(FormMapper $formMapper) - { - $formMapper - {{- code }} - ; - } - - /** - * @param ShowMapper $showMapper - */ - protected function configureShowFields(ShowMapper $showMapper) - { - $showMapper - {{- code }} - ; - } -} diff --git a/Resources/skeleton/Sonata/AdminController.php.twig b/Resources/skeleton/Sonata/AdminController.php.twig deleted file mode 100644 index 69db475..0000000 --- a/Resources/skeleton/Sonata/AdminController.php.twig +++ /dev/null @@ -1,10 +0,0 @@ -