Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/3.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Jun 20, 2019
2 parents e6c9d77 + b035052 commit cc6bd7b
Show file tree
Hide file tree
Showing 59 changed files with 812 additions and 498 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -33,6 +33,7 @@
"symfony/config": "^2.8 || ^3.2 || ^4.0",
"symfony/console": "^2.8 || ^3.2 || ^4.0",
"symfony/dependency-injection": "^2.8 || ^3.2 || ^4.0",
"symfony/doctrine-bridge": "^2.8 || ^3.2 || ^4.0",
"symfony/event-dispatcher": "^2.8 || ^3.2 || ^4.0",
"symfony/expression-language": "^2.8 || ^3.2 || ^4.0",
"symfony/form": "^2.8.18 || ^3.2.5 || ^4.0",
Expand All @@ -52,7 +53,7 @@
"symfony/twig-bundle": "^2.8 || ^3.2 || ^4.0",
"symfony/validator": "^2.8 || ^3.2 || ^4.0",
"twig/extensions": "^1.5",
"twig/twig": "^1.34 || ^2.0"
"twig/twig": "^2.9"
},
"conflict": {
"jms/di-extra-bundle": "<1.9",
Expand Down
37 changes: 29 additions & 8 deletions docs/reference/architecture.rst
Expand Up @@ -196,9 +196,21 @@ which stores instances of ``FieldDescriptionInterface``. Picking up on our previ
'class' => User::class
])

// "privateNotes" field will be rendered only if the authenticated
// user is granted with the "ROLE_ADMIN_MODERATOR" role
->add('privateNotes', null, [], [
'role' => 'ROLE_ADMIN_MODERATOR'
])

// if no type is specified, SonataAdminBundle tries to guess it
->add('body')

// conditionally add "status" field if the subject already exists
// `ifFalse()` is also available to build this kind of condition
->ifTrue($this->hasSubject())
->add('status')
->ifEnd()

// ...
;
}
Expand All @@ -209,6 +221,9 @@ which stores instances of ``FieldDescriptionInterface``. Picking up on our previ
$datagridMapper
->add('title')
->add('author')
->add('privateNotes', null, [], null, null, [
'role' => 'ROLE_ADMIN_MODERATOR'
])
;
}

Expand All @@ -219,6 +234,9 @@ which stores instances of ``FieldDescriptionInterface``. Picking up on our previ
->addIdentifier('title')
->add('slug')
->add('author')
->add('privateNotes', null, [
'role' => 'ROLE_ADMIN_MODERATOR'
])
;
}

Expand All @@ -230,21 +248,24 @@ which stores instances of ``FieldDescriptionInterface``. Picking up on our previ
->add('title')
->add('slug')
->add('author')
->add('privateNotes', null, [
'role' => 'ROLE_ADMIN_MODERATOR'
])
;
}
}

Internally, the provided ``Admin`` class will use these three functions to create three
``FieldDescriptionCollection`` instances:

* ``$formFieldDescriptions``, containing three ``FieldDescriptionInterface`` instances
for title, author and body
* ``$filterFieldDescriptions``, containing two ``FieldDescriptionInterface`` instances
for title and author
* ``$listFieldDescriptions``, containing three ``FieldDescriptionInterface`` instances
for title, slug and author
* ``$showFieldDescriptions``, containing four ``FieldDescriptionInterface`` instances
for id, title, slug and author
* ``$formFieldDescriptions``, containing four (and conditionally five) ``FieldDescriptionInterface``
instances for title, author, body and privateNotes (and status, if the condition is met)
* ``$filterFieldDescriptions``, containing three ``FieldDescriptionInterface`` instances
for title, author and privateNotes
* ``$listFieldDescriptions``, containing four ``FieldDescriptionInterface`` instances
for title, slug, author and privateNotes
* ``$showFieldDescriptions``, containing five ``FieldDescriptionInterface`` instances
for id, title, slug, author and privateNotes

The actual ``FieldDescription`` implementation is provided by the storage abstraction
bundle that you choose during the installation process, based on the
Expand Down
36 changes: 28 additions & 8 deletions src/Command/CreateClassCacheCommand.php
Expand Up @@ -13,8 +13,8 @@

namespace Sonata\AdminBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\ClassLoader\ClassCollectionLoader;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -30,19 +30,39 @@
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class CreateClassCacheCommand extends ContainerAwareCommand
class CreateClassCacheCommand extends Command
{
/**
* {@inheritdoc}
*/
protected static $defaultName = 'cache:create-cache-class';

/**
* @var string
*/
private $cacheDir;

/**
* @var bool
*/
private $debug;

public function __construct(string $cacheDir, bool $debug)
{
$this->cacheDir = $cacheDir;
$this->debug = $debug;

parent::__construct();
}

public function configure(): void
{
$this->setName('cache:create-cache-class');
$this->setDescription('Generate the classes.php files');
}

public function execute(InputInterface $input, OutputInterface $output): void
{
$kernel = $this->getContainer()->get('kernel');

$classmap = $kernel->getCacheDir().'/classes.map';
$classmap = $this->cacheDir.'/classes.map';

if (!is_file($classmap)) {
throw new \RuntimeException(sprintf('The file %s does not exist', $classmap));
Expand All @@ -54,9 +74,9 @@ public function execute(InputInterface $input, OutputInterface $output): void
$output->write('<info>Writing cache file ...</info>');
ClassCollectionLoader::load(
include($classmap),
$kernel->getCacheDir(),
$this->cacheDir,
$name,
$kernel->isDebug(),
$this->debug,
false,
$extension
);
Expand Down
39 changes: 29 additions & 10 deletions src/Command/ExplainAdminCommand.php
Expand Up @@ -13,32 +13,51 @@

namespace Sonata\AdminBundle\Command;

use Sonata\AdminBundle\Admin\AdminInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Sonata\AdminBundle\Admin\Pool;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;

/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class ExplainAdminCommand extends ContainerAwareCommand
class ExplainAdminCommand extends Command
{
/**
* {@inheritdoc}
*/
protected static $defaultName = 'sonata:admin:explain';

/**
* @var Pool
*/
private $pool;

/**
* @var MetadataFactoryInterface
*/
private $validator;

public function __construct(Pool $pool, MetadataFactoryInterface $validator)
{
$this->pool = $pool;
$this->validator = $validator;

parent::__construct();
}

public function configure(): void
{
$this->setName('sonata:admin:explain');
$this->setDescription('Explain an admin service');

$this->addArgument('admin', InputArgument::REQUIRED, 'The admin service id');
}

public function execute(InputInterface $input, OutputInterface $output): void
{
$admin = $this->getContainer()->get($input->getArgument('admin'));

if (!$admin instanceof AdminInterface) {
throw new \RuntimeException(sprintf('Service "%s" is not an admin class', $input->getArgument('admin')));
}
$admin = $this->pool->getInstance($input->getArgument('admin'));

$output->writeln('<comment>AdminBundle Information</comment>');
$output->writeln(sprintf('<info>% -20s</info> : %s', 'id', $admin->getCode()));
Expand Down Expand Up @@ -99,7 +118,7 @@ public function execute(InputInterface $input, OutputInterface $output): void
));
}

$metadata = $this->getContainer()->get('validator')->getMetadataFor($admin->getClass());
$metadata = $this->validator->getMetadataFor($admin->getClass());

$output->writeln('');
$output->writeln('<comment>Validation Framework</comment> - http://symfony.com/doc/3.0/book/validation.html');
Expand Down
53 changes: 31 additions & 22 deletions src/Command/GenerateAdminCommand.php
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\AdminBundle\Command;

use Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Controller\CRUDController;
use Sonata\AdminBundle\Generator\AdminGenerator;
use Sonata\AdminBundle\Generator\ControllerGenerator;
Expand All @@ -35,14 +36,33 @@
class GenerateAdminCommand extends QuestionableCommand
{
/**
* @var string[]
* {@inheritdoc}
*/
private $managerTypes;
protected static $defaultName = 'sonata:admin:generate';

/**
* @var Pool
*/
private $pool;

/**
* An array of model managers indexed by their service ids.
*
* @var ModelManagerInterface[]
*/
private $managerTypes = [];

public function __construct(Pool $pool, array $managerTypes)
{
$this->pool = $pool;
$this->managerTypes = $managerTypes;

parent::__construct();
}

public function configure(): void
{
$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')
Expand Down Expand Up @@ -74,7 +94,7 @@ public function validateManagerType($managerType)
throw new \InvalidArgumentException(sprintf(
'Invalid manager type "%s". Available manager types are "%s".',
$managerType,
implode('", "', $managerTypes)
implode('", "', array_keys($managerTypes))
));
}

Expand Down Expand Up @@ -259,12 +279,12 @@ private function getDefaultManagerType(): string
throw new \RuntimeException('There are no model managers registered.');
}

return current($managerTypes);
return current(array_keys($managerTypes));
}

private function getModelManager(string $managerType): ModelManagerInterface
{
$modelManager = $this->getContainer()->get('sonata.admin.manager.'.$managerType);
$modelManager = $this->getAvailableManagerTypes()[$managerType];
\assert($modelManager instanceof ModelManagerInterface);

return $modelManager;
Expand All @@ -288,24 +308,13 @@ private function getAdminServiceId(string $bundleName, string $adminClassBasenam
*/
private function getAvailableManagerTypes(): array
{
$container = $this->getContainer();

if (!$container instanceof Container) {
return [];
}

if (null === $this->managerTypes) {
$this->managerTypes = [];

foreach ($container->getServiceIds() as $id) {
if (0 === strpos($id, 'sonata.admin.manager.')) {
$managerType = substr($id, 21);
$this->managerTypes[$managerType] = $managerType;
}
}
$managerTypes = [];
foreach ($this->managerTypes as $id => $manager) {
$managerType = substr($id, 21);
$managerTypes[$managerType] = $manager;
}

return $this->managerTypes;
return $managerTypes;
}

private function getKernel(): KernelInterface
Expand Down

0 comments on commit cc6bd7b

Please sign in to comment.