Skip to content

Commit

Permalink
Make CoreController actions CaaSes
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Jun 9, 2018
1 parent 0a010ba commit 9c7fa4e
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 113 deletions.
75 changes: 8 additions & 67 deletions src/Controller/CoreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace Sonata\AdminBundle\Controller;

use Sonata\AdminBundle\Admin\AdminInterface;
@trigger_error(
'The '.__NAMESPACE__.'\CoreController class is deprecated since version 3.x and will be removed in 4.0.'
.' Use '.__NAMESPACE__.'\SearchAction or '.__NAMESPACE__.'\DashboardAction instead.',
E_USER_DEPRECATED
);

use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Search\SearchHandler;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -31,29 +35,7 @@ class CoreController extends Controller
*/
public function dashboardAction()
{
$blocks = [
'top' => [],
'left' => [],
'center' => [],
'right' => [],
'bottom' => [],
];

foreach ($this->container->getParameter('sonata.admin.configuration.dashboard_blocks') as $block) {
$blocks[$block['position']][] = $block;
}

$parameters = [
'base_template' => $this->getBaseTemplate(),
'admin_pool' => $this->container->get('sonata.admin.pool'),
'blocks' => $blocks,
];

if (!$this->getCurrentRequest()->isXmlHttpRequest()) {
$parameters['breadcrumbs_builder'] = $this->get('sonata.admin.breadcrumbs_builder');
}

return $this->render($this->getTemplateRegistry()->getTemplate('dashboard'), $parameters);
return $this->container->get(DashboardAction::class)($this->getCurrentRequest());
}

/**
Expand All @@ -66,48 +48,7 @@ public function dashboardAction()
*/
public function searchAction(Request $request)
{
if ($request->get('admin') && $request->isXmlHttpRequest()) {
try {
$admin = $this->getAdminPool()->getAdminByAdminCode($request->get('admin'));
} catch (ServiceNotFoundException $e) {
throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
}

if (!$admin instanceof AdminInterface) {
throw new \RuntimeException('The requested service is not an Admin instance');
}

$handler = $this->getSearchHandler();

$results = [];

if ($pager = $handler->search($admin, $request->get('q'), $request->get('page'), $request->get('offset'))) {
foreach ($pager->getResults() as $result) {
$results[] = [
'label' => $admin->toString($result),
'link' => $admin->generateObjectUrl('edit', $result),
'id' => $admin->id($result),
];
}
}

$response = new JsonResponse([
'results' => $results,
'page' => $pager ? (int) $pager->getPage() : false,
'total' => $pager ? (int) $pager->getNbResults() : false,
]);
$response->setPrivate();

return $response;
}

return $this->render($this->getTemplateRegistry()->getTemplate('search'), [
'base_template' => $this->getBaseTemplate(),
'breadcrumbs_builder' => $this->get('sonata.admin.breadcrumbs_builder'),
'admin_pool' => $this->container->get('sonata.admin.pool'),
'query' => $request->get('q'),
'groups' => $this->getAdminPool()->getDashboardGroups(),
]);
return $this->container->get(SearchAction::class)($request);
}

/**
Expand Down
82 changes: 82 additions & 0 deletions src/Controller/DashboardAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Controller;

use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

final class DashboardAction extends Controller
{
/**
* @var array
*/
private $dashboardBlocks;

/**
* @var BreadcrumbsBuilderInterface
*/
private $breadcrumbsBuilder;

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

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

public function __construct(
array $dashboardBlocks,
BreadcrumbsBuilderInterface $breadcrumbsBuilder,
TemplateRegistryInterface $templateRegistry,
Pool $pool
) {
$this->dashboardBlocks = $dashboardBlocks;
$this->breadcrumbsBuilder = $breadcrumbsBuilder;
$this->templateRegistry = $templateRegistry;
$this->pool = $pool;
}

public function __invoke(Request $request)
{
$blocks = [
'top' => [],
'left' => [],
'center' => [],
'right' => [],
'bottom' => [],
];

foreach ($this->dashboardBlocks as $block) {
$blocks[$block['position']][] = $block;
}

$parameters = [
'base_template' => $request->isXmlHttpRequest() ?
$this->templateRegistry->getTemplate('ajax') :
$this->templateRegistry->getTemplate('layout'),
'admin_pool' => $this->pool,
'blocks' => $blocks,
];

if (!$request->isXmlHttpRequest()) {
$parameters['breadcrumbs_builder'] = $this->breadcrumbsBuilder;
}

return $this->render($this->templateRegistry->getTemplate('dashboard'), $parameters);
}
}
116 changes: 116 additions & 0 deletions src/Controller/SearchAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Controller;

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Search\SearchHandler;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

final class SearchAction extends Controller
{
/**
* @var Pool
*/
private $pool;

/**
* @var SearchHandler
*/
private $searchHandler;

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

/**
* @var BreadcrumbsBuilderInterface
*/
private $breadcrumbsBuilder;

public function __construct(
Pool $pool,
SearchHandler $searchHandler,
TemplateRegistryInterface $templateRegistry,
BreadcrumbsBuilderInterface $breadcrumbsBuilder
) {
$this->pool = $pool;
$this->searchHandler = $searchHandler;
$this->templateRegistry = $templateRegistry;
$this->breadcrumbsBuilder = $breadcrumbsBuilder;
}

/**
* The search action first render an empty page, if the query is set, then the template generates
* some ajax request to retrieve results for each admin. The Ajax query returns a JSON response.
*
* @throws \RuntimeException
*
* @return JsonResponse|Response
*/
public function __invoke(Request $request)
{
if ($request->get('admin') && $request->isXmlHttpRequest()) {
try {
$admin = $this->pool->getAdminByAdminCode($request->get('admin'));
} catch (ServiceNotFoundException $e) {
throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e);
}

if (!$admin instanceof AdminInterface) {
throw new \RuntimeException('The requested service is not an Admin instance');
}

$results = [];

if ($pager = $this->searchHandler->search(
$admin,
$request->get('q'),
$request->get('page'),
$request->get('offset')
)) {
foreach ($pager->getResults() as $result) {
$results[] = [
'label' => $admin->toString($result),
'link' => $admin->generateObjectUrl('edit', $result),
'id' => $admin->id($result),
];
}
}

$response = new JsonResponse([
'results' => $results,
'page' => $pager ? (int) $pager->getPage() : false,
'total' => $pager ? (int) $pager->getNbResults() : false,
]);
$response->setPrivate();

return $response;
}

return $this->render($this->templateRegistry->getTemplate('search'), [
'base_template' => $request->isXmlHttpRequest() ?
$this->templateRegistry->getTemplate('ajax') :
$this->templateRegistry->getTemplate('layout'),
'breadcrumbs_builder' => $this->breadcrumbsBuilder,
'admin_pool' => $this->pool,
'query' => $request->get('q'),
'groups' => $this->pool->getDashboardGroups(),
]);
}
}
17 changes: 17 additions & 0 deletions src/Resources/config/controllers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Sonata\AdminBundle\Controller\DashboardAction" public="true">
<argument>%sonata.admin.configuration.dashboard_blocks%</argument>
<argument type="service" id="sonata.admin.breadcrumbs_builder"/>
<argument type="service" id="sonata.admin.global_template_registry"/>
<argument type="service" id="sonata.admin.pool"/>
</service>
<service id="Sonata\AdminBundle\Controller\SearchAction" public="true">
<argument type="service" id="sonata.admin.pool"/>
<argument type="service" id="sonata.admin.search.handler"/>
<argument type="service" id="sonata.admin.global_template_registry"/>
<argument type="service" id="sonata.admin.breadcrumbs_builder"/>
</service>
</services>
</container>
4 changes: 2 additions & 2 deletions src/Resources/config/routing/sonata_admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<default key="permanent">true</default>
</route>
<route id="sonata_admin_dashboard" path="/dashboard">
<default key="_controller">SonataAdminBundle:Core:dashboard</default>
<default key="_controller">Sonata\AdminBundle\Controller\DashboardAction</default>
</route>
<route id="sonata_admin_retrieve_form_element" path="/core/get-form-field-element">
<default key="_controller">sonata.admin.controller.admin:retrieveFormFieldElementAction</default>
Expand All @@ -23,7 +23,7 @@
<default key="_controller">sonata.admin.controller.admin:setObjectFieldValueAction</default>
</route>
<route id="sonata_admin_search" path="/search">
<default key="_controller">SonataAdminBundle:Core:search</default>
<default key="_controller">Sonata\AdminBundle\Controller\SearchAction</default>
</route>
<route id="sonata_admin_retrieve_autocomplete_items" path="/core/get-autocomplete-items">
<default key="_controller">sonata.admin.controller.admin:retrieveAutocompleteItemsAction</default>
Expand Down
Loading

0 comments on commit 9c7fa4e

Please sign in to comment.