Skip to content

Commit

Permalink
Merge afcf9d3 into 5e2a7e4
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoBakx committed Feb 27, 2018
2 parents 5e2a7e4 + afcf9d3 commit d234bf6
Show file tree
Hide file tree
Showing 23 changed files with 541 additions and 178 deletions.
54 changes: 39 additions & 15 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,11 @@ abstract class AbstractAdmin implements AdminInterface, DomainObjectInterface, A
*/
protected $accessMapping = [];

/**
* @var TemplateRegistryInterface
*/
private $templateRegistry;

/**
* The class name managed by the admin class.
*
Expand Down Expand Up @@ -1124,33 +1129,52 @@ public function generateMenuUrl($name, array $parameters = [], $absolute = Routi
return $this->routeGenerator->generateMenuUrl($this, $name, $parameters, $absolute);
}

public function setTemplateRegistry(TemplateRegistryInterface $templateRegistry)
{
$this->templateRegistry = $templateRegistry;
}

/**
* @deprecated Use TemplateRegistry services instead
*
* @param array $templates
*/
public function setTemplates(array $templates)
{
$this->templates = $templates;
$this->templateRegistry->setTemplates($templates);
}

/**
* @deprecated Use TemplateRegistry services instead
*
* @param string $name
* @param string $template
*/
public function setTemplate($name, $template)
{
$this->templates[$name] = $template;
$this->templateRegistry->setTemplate($name, $template);
}

/**
* @deprecated Use TemplateRegistry services instead
*
* @return array
*/
public function getTemplates()
{
return $this->templates;
return $this->templateRegistry->getTemplates();
}

/**
* @deprecated Use TemplateRegistry services instead
*
* @param string $name
*
* @return null|string
*/
public function getTemplate($name)
{
if (isset($this->templates[$name])) {
return $this->templates[$name];
}
return $this->templateRegistry->getTemplate($name);
}

public function getNewInstance()
Expand Down Expand Up @@ -2493,7 +2517,7 @@ public function configureActionButtons($action, $object = null)
&& $this->hasRoute('create')
) {
$list['create'] = [
'template' => $this->getTemplate('button_create'),
'template' => $this->templateRegistry->getTemplate('button_create'),
];
}

Expand All @@ -2502,7 +2526,7 @@ public function configureActionButtons($action, $object = null)
&& $this->hasRoute('edit')
) {
$list['edit'] = [
'template' => $this->getTemplate('button_edit'),
'template' => $this->templateRegistry->getTemplate('button_edit'),
];
}

Expand All @@ -2511,7 +2535,7 @@ public function configureActionButtons($action, $object = null)
&& $this->hasRoute('history')
) {
$list['history'] = [
'template' => $this->getTemplate('button_history'),
'template' => $this->templateRegistry->getTemplate('button_history'),
];
}

Expand All @@ -2521,7 +2545,7 @@ public function configureActionButtons($action, $object = null)
&& $this->hasRoute('acl')
) {
$list['acl'] = [
'template' => $this->getTemplate('button_acl'),
'template' => $this->templateRegistry->getTemplate('button_acl'),
];
}

Expand All @@ -2531,7 +2555,7 @@ public function configureActionButtons($action, $object = null)
&& $this->hasRoute('show')
) {
$list['show'] = [
'template' => $this->getTemplate('button_show'),
'template' => $this->templateRegistry->getTemplate('button_show'),
];
}

Expand All @@ -2540,7 +2564,7 @@ public function configureActionButtons($action, $object = null)
&& $this->hasRoute('list')
) {
$list['list'] = [
'template' => $this->getTemplate('button_list'),
'template' => $this->templateRegistry->getTemplate('button_list'),
];
}

Expand Down Expand Up @@ -2580,7 +2604,7 @@ public function getDashboardActions()
$actions['create'] = [
'label' => 'link_add',
'translation_domain' => 'SonataAdminBundle',
'template' => $this->getTemplate('action_create'),
'template' => $this->templateRegistry->getTemplate('action_create'),
'url' => $this->generateUrl('create'),
'icon' => 'plus-circle',
];
Expand Down Expand Up @@ -2780,7 +2804,7 @@ protected function buildList()
);

$fieldDescription->setAdmin($this);
$fieldDescription->setTemplate($this->getTemplate('batch'));
$fieldDescription->setTemplate($this->templateRegistry->getTemplate('batch'));

$mapper->add($fieldDescription, 'batch');
}
Expand All @@ -2804,7 +2828,7 @@ protected function buildList()
);

$fieldDescription->setAdmin($this);
$fieldDescription->setTemplate($this->getTemplate('select'));
$fieldDescription->setTemplate($this->templateRegistry->getTemplate('select'));

$mapper->add($fieldDescription, 'select');
}
Expand Down
24 changes: 17 additions & 7 deletions src/Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class Pool
protected $adminClasses = [];

/**
* @var string[]
* @var TemplateRegistryInterface
*/
protected $templates = [];
protected $templateRegistry;

/**
* @var array
Expand Down Expand Up @@ -315,29 +315,39 @@ public function getAdminClasses()
return $this->adminClasses;
}

public function setTemplateRegistry(TemplateRegistryInterface $templateRegistry)
{
$this->templateRegistry = $templateRegistry;
}

/**
* @deprecated Use TemplateRegistry instead
*/
public function setTemplates(array $templates)
{
$this->templates = $templates;
$this->templateRegistry->setTemplates($templates);
}

/**
* @deprecated Use TemplateRegistry instead
*
* @return array
*/
public function getTemplates()
{
return $this->templates;
return $this->templateRegistry->getTemplates();
}

/**
* @deprecated Use TemplateRegistry instead
*
* @param string $name
*
* @return null|string
*/
public function getTemplate($name)
{
if (isset($this->templates[$name])) {
return $this->templates[$name];
}
return $this->templateRegistry->getTemplate($name);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions src/Admin/TemplateRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Admin;

final class TemplateRegistry implements TemplateRegistryInterface
{
private $templates = [];

/**
* TemplateRegistry constructor.
*
* @param array $templates
*/
public function __construct(array $templates = [])
{
$this->templates = $templates;
}

public function getTemplates()
{
return $this->templates;
}

public function setTemplates(array $templates)
{
$this->templates = $templates;
}

public function getTemplate($name)
{
if (isset($this->templates[$name])) {
return $this->templates[$name];
}
}

public function setTemplate($name, $template)
{
$this->templates[$name] = $template;
}
}
35 changes: 35 additions & 0 deletions src/Admin/TemplateRegistryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\Admin;

interface TemplateRegistryInterface
{
/**
* @return array
*/
public function getTemplates();

public function setTemplates(array $templates);

/**
* @param string $name
*
* @return string
*/
public function getTemplate($name);

/**
* @param string $name
* @param string $template
*/
public function setTemplate($name, $template);
}
21 changes: 18 additions & 3 deletions src/Block/AdminListBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Sonata\AdminBundle\Block;

use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Admin\TemplateRegistry;
use Sonata\AdminBundle\Admin\TemplateRegistryInterface;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
Expand All @@ -23,16 +25,29 @@
*/
class AdminListBlockService extends AbstractBlockService
{
/**
* @var Pool
*/
protected $pool;

/**
* @var TemplateRegistryInterface
*/
private $templateRegistry;

/**
* @param string $name
*/
public function __construct($name, EngineInterface $templating, Pool $pool)
{
public function __construct(
$name,
EngineInterface $templating,
Pool $pool,
TemplateRegistryInterface $templateRegistry = null
) {
parent::__construct($name, $templating);

$this->pool = $pool;
$this->templateRegistry = $templateRegistry ?: new TemplateRegistry();
}

public function execute(BlockContextInterface $blockContext, Response $response = null)
Expand All @@ -48,7 +63,7 @@ public function execute(BlockContextInterface $blockContext, Response $response
}
}

return $this->renderPrivateResponse($this->pool->getTemplate('list_block'), [
return $this->renderPrivateResponse($this->templateRegistry->getTemplate('list_block'), [
'block' => $blockContext->getBlock(),
'settings' => $settings,
'admin_pool' => $this->pool,
Expand Down

0 comments on commit d234bf6

Please sign in to comment.