Skip to content

[FrameworkBundle] Split abstract Controller class into traits #16863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
394 changes: 1 addition & 393 deletions src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

Large diffs are not rendered by default.

141 changes: 141 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Controller;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Bundle\FrameworkBundle\Form\FormHelperTrait;
use Symfony\Bundle\FrameworkBundle\Kernel\KernelHelperTrait;
use Symfony\Bundle\FrameworkBundle\Routing\RouterHelperTrait;
use Symfony\Bundle\FrameworkBundle\Security\SecurityHelperTrait;
use Symfony\Bundle\FrameworkBundle\Serializer\SerializerHelperTrait;
use Symfony\Bundle\FrameworkBundle\Templating\RenderHelperTrait;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* ControllerTrait is a simple implementation of a Controller.
*
* It provides methods to common features needed in controllers.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Alexander M. Turek <me@derrabus.de>
*/
trait ControllerTrait
{
use ContainerAwareTrait;
use KernelHelperTrait;
use RouterHelperTrait;
use FormHelperTrait;
use RenderHelperTrait;
use SerializerHelperTrait;
use SecurityHelperTrait;

/**
* Returns a RedirectResponse to the given URL.
*
* @param string $url The URL to redirect to
* @param int $status The status code to use for the Response
*
* @return RedirectResponse
*/
protected function redirect($url, $status = 302)
{
return new RedirectResponse($url, $status);
}

/**
* Adds a flash message to the current session for type.
*
* @param string $type The type
* @param string $message The message
*
* @throws \LogicException
*/
protected function addFlash($type, $message)
{
if (!$this->container->has('session')) {
throw new \LogicException('You can not use the addFlash method if sessions are disabled.');
}

$this->container->get('session')->getFlashBag()->add($type, $message);
}

/**
* Returns a NotFoundHttpException.
*
* This will result in a 404 response code. Usage example:
*
* throw $this->createNotFoundException('Page not found!');
*
* @param string $message A message
* @param \Exception|null $previous The previous exception
*
* @return NotFoundHttpException
*/
protected function createNotFoundException($message = 'Not Found', \Exception $previous = null)
{
return new NotFoundHttpException($message, $previous);
}

/**
* Shortcut to return the Doctrine Registry service.
*
* @return Registry
*
* @throws \LogicException If DoctrineBundle is not available
*/
protected function getDoctrine()
{
if (!$this->container->has('doctrine')) {
throw new \LogicException('The DoctrineBundle is not registered in your application.');
}

return $this->container->get('doctrine');
}

/**
* Returns true if the service id is defined.
*
* @param string $id The service id
*
* @return bool true if the service id is defined, false otherwise
*/
protected function has($id)
{
return $this->container->has($id);
}

/**
* Gets a container service by its id.
*
* @param string $id The service id
*
* @return object The service
*/
protected function get($id)
{
return $this->container->get($id);
}

/**
* Gets a container configuration parameter by its name.
*
* @param string $name The parameter name
*
* @return mixed
*/
protected function getParameter($name)
{
return $this->container->getParameter($name);
}
}
21 changes: 21 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Exception;

/**
* Base LogicException for the FrameworkBundle.
*
* @author Alexander M. Turek <me@derrabus.de>
*/
class LogicException extends \LogicException
{
}
75 changes: 75 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Form/FormHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Form;

use Symfony\Bundle\FrameworkBundle\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormFactoryInterface;

/**
* Form integration for controller classes.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Alexander M. Turek <me@derrabus.de>
*/
trait FormHelperTrait
{
/**
* @var FormFactoryInterface
*/
protected $formFactory;

/**
* @return FormFactoryInterface
*/
protected function getFormFactory()
{
if ($this->formFactory === null) {
if (!isset($this->container)) {
throw new LogicException('Unable to load the form factory. Please either set the $formFactory property or make'.__CLASS__.' container-aware.');
}

$this->formFactory = $this->container->get('form.factory');
}

return $this->formFactory;
}

/**
* Creates and returns a Form instance from the type of the form.
*
* @param string $type The fully qualified class name of the form type
* @param mixed $data The initial data for the form
* @param array $options Options for the form
*
* @return Form
*/
protected function createForm($type, $data = null, array $options = array())
{
return $this->getFormFactory()->create($type, $data, $options);
}

/**
* Creates and returns a form builder instance.
*
* @param mixed $data The initial data for the form
* @param array $options Options for the form
*
* @return FormBuilder
*/
protected function createFormBuilder($data = null, array $options = array())
{
return $this->getFormFactory()->createBuilder(FormType::class, $data, $options);
}
}
85 changes: 85 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Kernel/KernelHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Kernel;

use Symfony\Bundle\FrameworkBundle\Exception\LogicException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* HttpKernel integration for controller classes.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Alexander M. Turek <me@derrabus.de>
*/
trait KernelHelperTrait
{
/**
* @var RequestStack
*/
protected $requestStack;

/**
* @var HttpKernelInterface
*/
protected $httpKernel;

/**
* @return RequestStack
*/
protected function getRequestStack()
{
if ($this->requestStack === null) {
if (!isset($this->container)) {
throw new LogicException('Unable to retrieve the request stack. Please either set the $requestStack property or make'.__CLASS__.' container-aware.');
}

$this->requestStack = $this->container->get('request_stack');
}

return $this->requestStack;
}

/**
* @return HttpKernelInterface
*/
protected function getHttpKernel()
{
if ($this->httpKernel === null) {
if (!isset($this->container)) {
throw new LogicException('Unable to retrieve the HTTP kernel. Please either set the $httpKernel property or make'.__CLASS__.' container-aware.');
}

$this->httpKernel = $this->container->get('http_kernel');
}

return $this->httpKernel;
}

/**
* Forwards the request to another controller.
*
* @param string $controller The controller name (a string like BlogBundle:Post:index)
* @param array $path An array of path parameters
* @param array $query An array of query parameters
*
* @return Response A Response instance
*/
protected function forward($controller, array $path = array(), array $query = array())
{
$path['_controller'] = $controller;
$subRequest = $this->getRequestStack()->getCurrentRequest()->duplicate($query, null, $path);

return $this->getHttpKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}
}
Loading