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,61 @@
<?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\AdminBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\ClassLoader\ClassCollectionLoader;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class CreateClassCacheCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
public function configure()
{
$this->setName('cache:create-cache-class');
$this->setDescription('Generate the classes.php files');
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$kernel = $this->getContainer()->get('kernel');
$classmap = $kernel->getCacheDir().'/classes.map';
if (!is_file($classmap)) {
throw new \RuntimeException(sprintf('The file %s does not exist', $classmap));
}
$name = 'classes';
$extension = '.php';
$output->write('<info>Writing cache file ...</info>');
ClassCollectionLoader::load(
include($classmap),
$kernel->getCacheDir(),
$name,
$kernel->isDebug(),
false,
$extension
);
$output->writeln(' done!');
}
}

View File

@@ -0,0 +1,162 @@
<?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\AdminBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class ExplainAdminCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
public function configure()
{
$this->setName('sonata:admin:explain');
$this->setDescription('Explain an admin service');
$this->addArgument('admin', InputArgument::REQUIRED, 'The admin service id');
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$admin = $this->getContainer()->get($input->getArgument('admin'));
if (!$admin instanceof \Sonata\AdminBundle\Admin\AdminInterface) {
throw new \RuntimeException(sprintf('Service "%s" is not an admin class', $input->getArgument('admin')));
}
$output->writeln('<comment>AdminBundle Information</comment>');
$output->writeln(sprintf('<info>% -20s</info> : %s', 'id', $admin->getCode()));
$output->writeln(sprintf('<info>% -20s</info> : %s', 'Admin', get_class($admin)));
$output->writeln(sprintf('<info>% -20s</info> : %s', 'Model', $admin->getClass()));
$output->writeln(sprintf('<info>% -20s</info> : %s', 'Controller', $admin->getBaseControllerName()));
$output->writeln(sprintf('<info>% -20s</info> : %s', 'Model Manager', get_class($admin->getModelManager())));
$output->writeln(sprintf('<info>% -20s</info> : %s', 'Form Builder', get_class($admin->getFormBuilder())));
$output->writeln(sprintf('<info>% -20s</info> : %s', 'Datagrid Builder', get_class($admin->getDatagridBuilder())));
$output->writeln(sprintf('<info>% -20s</info> : %s', 'List Builder', get_class($admin->getListBuilder())));
if ($admin->isChild()) {
$output->writeln(sprintf('<info>% -15s</info> : %s', 'Parent', $admin->getParent()->getCode()));
}
$output->writeln('');
$output->writeln('<info>Routes</info>');
foreach ($admin->getRoutes()->getElements() as $route) {
$output->writeln(sprintf(' - % -25s %s', $route->getDefault('_sonata_name'), $route->getPath()));
}
$output->writeln('');
$output->writeln('<info>Datagrid Columns</info>');
foreach ($admin->getListFieldDescriptions() as $name => $fieldDescription) {
$output->writeln(sprintf(
' - % -25s % -15s % -15s',
$name,
$fieldDescription->getType(),
$fieldDescription->getTemplate()
));
}
$output->writeln('');
$output->writeln('<info>Datagrid Filters</info>');
foreach ($admin->getFilterFieldDescriptions() as $name => $fieldDescription) {
$output->writeln(sprintf(
' - % -25s % -15s % -15s',
$name,
$fieldDescription->getType(),
$fieldDescription->getTemplate()
));
}
$output->writeln('');
$output->writeln('<info>Form theme(s)</info>');
foreach ($admin->getFormTheme() as $template) {
$output->writeln(sprintf(' - %s', $template));
}
$output->writeln('');
$output->writeln('<info>Form Fields</info>');
foreach ($admin->getFormFieldDescriptions() as $name => $fieldDescription) {
$output->writeln(sprintf(
' - % -25s % -15s % -15s',
$name,
$fieldDescription->getType(),
$fieldDescription->getTemplate()
));
}
$metadata = false;
if ($this->getContainer()->has('validator.validator_factory')) {
$factory = $this->getContainer()->get('validator.validator_factory');
if (method_exists($factory, 'getMetadataFor')) {
$metadata = $factory->getMetadataFor($admin->getClass());
}
}
// NEXT_MAJOR: remove method check in next major release
if (!$metadata) {
$metadata = $this->getContainer()->get('validator')->getMetadataFor($admin->getClass());
}
$output->writeln('');
$output->writeln('<comment>Validation Framework</comment> - http://symfony.com/doc/3.0/book/validation.html');
$output->writeln('<info>Properties constraints</info>');
if (count($metadata->properties) == 0) {
$output->writeln(' <error>no property constraints defined !!</error>');
} else {
foreach ($metadata->properties as $name => $property) {
$output->writeln(sprintf(' - %s', $name));
foreach ($property->getConstraints() as $constraint) {
$output->writeln(sprintf(
' % -70s %s',
get_class($constraint),
implode('|', $constraint->groups)
));
}
}
}
$output->writeln('');
$output->writeln('<info>Getters constraints</info>');
if (count($metadata->getters) == 0) {
$output->writeln(' <error>no getter constraints defined !!</error>');
} else {
foreach ($metadata->getters as $name => $property) {
$output->writeln(sprintf(' - %s', $name));
foreach ($property->getConstraints() as $constraint) {
$output->writeln(sprintf(
' % -70s %s',
get_class($constraint),
implode('|', $constraint->groups)
));
}
}
}
$output->writeln('');
$output->writeln('<info>done!</info>');
}
}

View File

@@ -0,0 +1,353 @@
<?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\AdminBundle\Command;
use Sonata\AdminBundle\Generator\AdminGenerator;
use Sonata\AdminBundle\Generator\ControllerGenerator;
use Sonata\AdminBundle\Manipulator\ServicesManipulator;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* @author Marek Stipek <mario.dweller@seznam.cz>
* @author Simon Cosandey <simon.cosandey@simseo.ch>
*/
class GenerateAdminCommand extends QuestionableCommand
{
/**
* @var string[]
*/
private $managerTypes;
/**
* {@inheritdoc}
*/
public function configure()
{
$this
->setName('sonata:admin:generate')
->setDescription('Generates an admin class based on the given model class')
->addArgument('model', InputArgument::REQUIRED, 'The fully qualified model class')
->addOption('bundle', 'b', InputOption::VALUE_OPTIONAL, 'The bundle name')
->addOption('admin', 'a', InputOption::VALUE_OPTIONAL, 'The admin class basename')
->addOption('controller', 'c', InputOption::VALUE_OPTIONAL, 'The controller class basename')
->addOption('manager', 'm', InputOption::VALUE_OPTIONAL, 'The model manager type')
->addOption('services', 'y', InputOption::VALUE_OPTIONAL, 'The services YAML file', 'services.yml')
->addOption('id', 'i', InputOption::VALUE_OPTIONAL, 'The admin service ID')
;
}
/**
* {@inheritdoc}
*/
public function isEnabled()
{
return class_exists('Sensio\\Bundle\\GeneratorBundle\\SensioGeneratorBundle');
}
/**
* @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;
}
/**
* {@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';
$adminGenerator = new AdminGenerator($modelManager, $skeletonDirectory);
try {
$adminGenerator->generate($bundle, $adminClassBasename, $modelClass);
$output->writeln(sprintf(
'%sThe admin class "<info>%s</info>" has been generated under the file "<info>%s</info>".',
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 "<info>%s</info>" has been generated under the file "<info>%s</info>".',
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 {
$id = $input->getOption('id') ?: $this->getAdminServiceId($bundle->getName(), $adminClassBasename);
$servicesManipulator->addResource($id, $modelClass, $adminClass, $controllerName, $managerType);
$output->writeln(sprintf(
'%sThe service "<info>%s</info>" has been appended to the file <info>"%s</info>".',
PHP_EOL,
$id,
realpath($file)
));
} 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(),
[$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);
} else {
$input->setOption('services', false);
}
$input->setArgument('model', $modelClass);
$input->setOption('admin', $adminClassBasename);
$input->setOption('bundle', $bundleName);
}
/**
* @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;
}
/**
* @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<error>%s</error>", $message));
}
/**
* @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)
{
$prefix = substr($bundleName, -6) == 'Bundle' ? substr($bundleName, 0, -6) : $bundleName;
$suffix = substr($adminClassBasename, -5) == 'Admin' ? substr($adminClassBasename, 0, -5) : $adminClassBasename;
$suffix = str_replace('\\', '.', $suffix);
return Container::underscore(sprintf(
'%s.admin.%s',
$prefix,
$suffix
));
}
/**
* @return string[]
*/
private function getAvailableManagerTypes()
{
$container = $this->getContainer();
if (!$container instanceof Container) {
return [];
}
if ($this->managerTypes === null) {
$this->managerTypes = [];
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()
{
/* @var $application Application */
$application = $this->getApplication();
return $application->getKernel();
}
}

View File

@@ -0,0 +1,132 @@
<?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\AdminBundle\Command;
use Sonata\AdminBundle\Util\ObjectAclManipulatorInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class GenerateObjectAclCommand extends QuestionableCommand
{
/**
* @var string
*/
protected $userEntityClass = '';
/**
* {@inheritdoc}
*/
public function configure()
{
$this
->setName('sonata:admin:generate-object-acl')
->setDescription('Install ACL for the objects of the Admin Classes.')
->addOption('object_owner', null, InputOption::VALUE_OPTIONAL, 'If set, the task will set the object owner for each admin.')
->addOption('user_entity', null, InputOption::VALUE_OPTIONAL, 'Shortcut notation like <comment>AcmeDemoBundle:User</comment>. If not set, it will be asked the first time an object owner is set.')
->addOption('step', null, InputOption::VALUE_NONE, 'If set, the task will ask for each admin if the ACLs need to be generated and what object owner to set, if any.')
;
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Welcome to the AdminBundle object ACL generator');
$output->writeln([
'',
'This command helps you to generate ACL entities for the objects handled by the AdminBundle.',
'',
'If the step option is used, you will be asked if you want to generate the object ACL entities for each Admin.',
'You must use the shortcut notation like <comment>AcmeDemoBundle:User</comment> if you want to set an object owner.',
'',
]);
if ($input->getOption('user_entity')) {
try {
$this->getUserEntityClass($input, $output);
} catch (\Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
return;
}
}
foreach ($this->getContainer()->get('sonata.admin.pool')->getAdminServiceIds() as $id) {
try {
$admin = $this->getContainer()->get($id);
} catch (\Exception $e) {
$output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>');
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
continue;
}
if ($input->getOption('step') && !$this->askConfirmation($input, $output, sprintf("<question>Generate ACLs for the object instances handled by \"%s\"?</question>\n", $id), 'no', '?')) {
continue;
}
$securityIdentity = null;
if ($input->getOption('step') && $this->askConfirmation($input, $output, "<question>Set an object owner?</question>\n", 'no', '?')) {
$username = $this->askAndValidate($input, $output, 'Please enter the username: ', '', 'Sonata\AdminBundle\Command\Validators::validateUsername');
$securityIdentity = new UserSecurityIdentity($username, $this->getUserEntityClass($input, $output));
}
if (!$input->getOption('step') && $input->getOption('object_owner')) {
$securityIdentity = new UserSecurityIdentity($input->getOption('object_owner'), $this->getUserEntityClass($input, $output));
}
$manipulatorId = sprintf('sonata.admin.manipulator.acl.object.%s', $admin->getManagerType());
if (!$this->getContainer()->has($manipulatorId)) {
$output->writeln('Admin class is using a manager type that has no manipulator implemented : <info>ignoring</info>');
continue;
}
$manipulator = $this->getContainer()->get($manipulatorId);
if (!$manipulator instanceof ObjectAclManipulatorInterface) {
$output->writeln(sprintf('The interface "ObjectAclManipulatorInterface" is not implemented for %s: <info>ignoring</info>', get_class($manipulator)));
continue;
}
$manipulator->batchConfigureAcls($output, $admin, $securityIdentity);
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return string
*/
protected function getUserEntityClass(InputInterface $input, OutputInterface $output)
{
if ($this->userEntityClass === '') {
if ($input->getOption('user_entity')) {
list($userBundle, $userEntity) = Validators::validateEntityName($input->getOption('user_entity'));
$this->userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle).'\\'.$userEntity;
} else {
list($userBundle, $userEntity) = $this->askAndValidate($input, $output, 'Please enter the User Entity shortcut name: ', '', 'Sonata\AdminBundle\Command\Validators::validateEntityName');
// Entity exists?
$this->userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle).'\\'.$userEntity;
}
}
return $this->userEntityClass;
}
}

View File

@@ -0,0 +1,48 @@
<?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\AdminBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class ListAdminCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
public function configure()
{
$this->setName('sonata:admin:list');
$this->setDescription('List all admin services available');
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$pool = $this->getContainer()->get('sonata.admin.pool');
$output->writeln('<info>Admin services:</info>');
foreach ($pool->getAdminServiceIds() as $id) {
$instance = $this->getContainer()->get($id);
$output->writeln(sprintf(' <info>%-40s</info> %-60s',
$id,
$instance->getClass()
));
}
}
}

View File

@@ -0,0 +1,108 @@
<?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\AdminBundle\Command;
use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
abstract class QuestionableCommand extends ContainerAwareCommand
{
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param string $questionText
* @param mixed $default
* @param callable $validator
*
* @return mixed
*/
final protected function askAndValidate(InputInterface $input, OutputInterface $output, $questionText, $default, $validator)
{
$questionHelper = $this->getQuestionHelper();
// NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
if ($questionHelper instanceof DialogHelper) {
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);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param string $questionText
* @param string $default
* @param string $separator
*
* @return string
*/
final protected function askConfirmation(InputInterface $input, OutputInterface $output, $questionText, $default, $separator)
{
$questionHelper = $this->getQuestionHelper();
// NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
if ($questionHelper instanceof DialogHelper) {
$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 QuestionHelper|DialogHelper
*/
final protected function getQuestionHelper()
{
// NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
if (class_exists('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')) {
$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;
}
}

View File

@@ -0,0 +1,62 @@
<?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\AdminBundle\Command;
use Sonata\AdminBundle\Util\AdminAclManipulatorInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class SetupAclCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
public function configure()
{
$this->setName('sonata:admin:setup-acl');
$this->setDescription('Install ACL for Admin Classes');
}
/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Starting ACL AdminBundle configuration');
foreach ($this->getContainer()->get('sonata.admin.pool')->getAdminServiceIds() as $id) {
try {
$admin = $this->getContainer()->get($id);
} catch (\Exception $e) {
$output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>');
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
continue;
}
$manipulator = $this->getContainer()->get('sonata.admin.manipulator.acl.admin');
if (!$manipulator instanceof AdminAclManipulatorInterface) {
$output->writeln(sprintf(
'The interface "AdminAclManipulatorInterface" is not implemented for %s: <info>ignoring</info>',
get_class($manipulator)
));
continue;
}
$manipulator->configureAcls($output, $admin);
}
}
}

View File

@@ -0,0 +1,166 @@
<?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\AdminBundle\Command;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class Validators
{
/**
* @static
*
* @param string $username
*
* @return mixed
*
* @throws \InvalidArgumentException
*/
public static function validateUsername($username)
{
if (is_null($username)) {
throw new \InvalidArgumentException('The username must be set');
}
return $username;
}
/**
* @static
*
* @param string $shortcut
*
* @return array
*
* @throws \InvalidArgumentException
*/
public static function validateEntityName($shortcut)
{
$entity = str_replace('/', '\\', $shortcut);
if (false === $pos = strpos($entity, ':')) {
throw new \InvalidArgumentException(sprintf(
'The entity name must contain a ":" (colon sign) '
.'("%s" given, expecting something like AcmeBlogBundle:Post)',
$entity
));
}
return [substr($entity, 0, $pos), substr($entity, $pos + 1)];
}
/**
* @static
*
* @param string $class
*
* @return string
*
* @throws \InvalidArgumentException
*/
public static function validateClass($class)
{
$class = str_replace('/', '\\', $class);
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('The class "%s" does not exist.', $class));
}
return $class;
}
/**
* @static
*
* @param string $adminClassBasename
*
* @return string
*
* @throws \InvalidArgumentException
*/
public static function validateAdminClassBasename($adminClassBasename)
{
$adminClassBasename = str_replace('/', '\\', $adminClassBasename);
if (false !== strpos($adminClassBasename, ':')) {
throw new \InvalidArgumentException(sprintf(
'The admin class name must not contain a ":" (colon sign) '
.'("%s" given, expecting something like PostAdmin")',
$adminClassBasename
));
}
return $adminClassBasename;
}
/**
* @static
*
* @param string $controllerClassBasename
*
* @return string
*
* @throws \InvalidArgumentException
*/
public static function validateControllerClassBasename($controllerClassBasename)
{
$controllerClassBasename = str_replace('/', '\\', $controllerClassBasename);
if (false !== strpos($controllerClassBasename, ':')) {
throw new \InvalidArgumentException(sprintf(
'The controller class name must not contain a ":" (colon sign) ("%s" given, '
.'expecting something like PostAdminController")',
$controllerClassBasename
));
}
if (substr($controllerClassBasename, -10) != 'Controller') {
throw new \InvalidArgumentException('The controller class name must end with "Controller".');
}
return $controllerClassBasename;
}
/**
* @static
*
* @param string $servicesFile
*
* @return string
*/
public static function validateServicesFile($servicesFile)
{
return trim($servicesFile, '/');
}
/**
* @static
*
* @param string $serviceId
*
* @return string
*
* @throws \InvalidArgumentException
*/
public static function validateServiceId($serviceId)
{
if (preg_match('/[^A-Za-z\._0-9]/', $serviceId, $matches)) {
throw new \InvalidArgumentException(sprintf(
'Service ID "%s" contains invalid character "%s".',
$serviceId,
$matches[0]
));
}
return $serviceId;
}
}