Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fd1566a

Browse filesBrowse files
committed
Transform abstract Controller class into traits.
1 parent 06eb52c commit fd1566a
Copy full SHA for fd1566a

17 files changed

+1810
-954
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
+1-393Lines changed: 1 addition & 393 deletions
Large diffs are not rendered by default.
+141Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Controller;
13+
14+
use Doctrine\Bundle\DoctrineBundle\Registry;
15+
use Symfony\Bundle\FrameworkBundle\Form\FormHelperTrait;
16+
use Symfony\Bundle\FrameworkBundle\Kernel\KernelHelperTrait;
17+
use Symfony\Bundle\FrameworkBundle\Routing\RouterHelperTrait;
18+
use Symfony\Bundle\FrameworkBundle\Security\SecurityHelperTrait;
19+
use Symfony\Bundle\FrameworkBundle\Serializer\SerializerHelperTrait;
20+
use Symfony\Bundle\FrameworkBundle\Templating\RenderHelperTrait;
21+
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
22+
use Symfony\Component\HttpFoundation\RedirectResponse;
23+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
24+
25+
/**
26+
* ControllerTrait is a simple implementation of a Controller.
27+
*
28+
* It provides methods to common features needed in controllers.
29+
*
30+
* @author Fabien Potencier <fabien@symfony.com>
31+
* @author Alexander M. Turek <me@derrabus.de>
32+
*/
33+
trait ControllerTrait
34+
{
35+
use ContainerAwareTrait;
36+
use KernelHelperTrait;
37+
use RouterHelperTrait;
38+
use FormHelperTrait;
39+
use RenderHelperTrait;
40+
use SerializerHelperTrait;
41+
use SecurityHelperTrait;
42+
43+
/**
44+
* Returns a RedirectResponse to the given URL.
45+
*
46+
* @param string $url The URL to redirect to
47+
* @param int $status The status code to use for the Response
48+
*
49+
* @return RedirectResponse
50+
*/
51+
protected function redirect($url, $status = 302)
52+
{
53+
return new RedirectResponse($url, $status);
54+
}
55+
56+
/**
57+
* Adds a flash message to the current session for type.
58+
*
59+
* @param string $type The type
60+
* @param string $message The message
61+
*
62+
* @throws \LogicException
63+
*/
64+
protected function addFlash($type, $message)
65+
{
66+
if (!$this->container->has('session')) {
67+
throw new \LogicException('You can not use the addFlash method if sessions are disabled.');
68+
}
69+
70+
$this->container->get('session')->getFlashBag()->add($type, $message);
71+
}
72+
73+
/**
74+
* Returns a NotFoundHttpException.
75+
*
76+
* This will result in a 404 response code. Usage example:
77+
*
78+
* throw $this->createNotFoundException('Page not found!');
79+
*
80+
* @param string $message A message
81+
* @param \Exception|null $previous The previous exception
82+
*
83+
* @return NotFoundHttpException
84+
*/
85+
protected function createNotFoundException($message = 'Not Found', \Exception $previous = null)
86+
{
87+
return new NotFoundHttpException($message, $previous);
88+
}
89+
90+
/**
91+
* Shortcut to return the Doctrine Registry service.
92+
*
93+
* @return Registry
94+
*
95+
* @throws \LogicException If DoctrineBundle is not available
96+
*/
97+
protected function getDoctrine()
98+
{
99+
if (!$this->container->has('doctrine')) {
100+
throw new \LogicException('The DoctrineBundle is not registered in your application.');
101+
}
102+
103+
return $this->container->get('doctrine');
104+
}
105+
106+
/**
107+
* Returns true if the service id is defined.
108+
*
109+
* @param string $id The service id
110+
*
111+
* @return bool true if the service id is defined, false otherwise
112+
*/
113+
protected function has($id)
114+
{
115+
return $this->container->has($id);
116+
}
117+
118+
/**
119+
* Gets a container service by its id.
120+
*
121+
* @param string $id The service id
122+
*
123+
* @return object The service
124+
*/
125+
protected function get($id)
126+
{
127+
return $this->container->get($id);
128+
}
129+
130+
/**
131+
* Gets a container configuration parameter by its name.
132+
*
133+
* @param string $name The parameter name
134+
*
135+
* @return mixed
136+
*/
137+
protected function getParameter($name)
138+
{
139+
return $this->container->getParameter($name);
140+
}
141+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Exception;
13+
14+
/**
15+
* Base LogicException for the FrameworkBundle.
16+
*
17+
* @author Alexander M. Turek <me@derrabus.de>
18+
*/
19+
class LogicException extends \LogicException
20+
{
21+
}
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Form;
13+
14+
use Symfony\Bundle\FrameworkBundle\Exception\LogicException;
15+
use Symfony\Component\Form\Extension\Core\Type\FormType;
16+
use Symfony\Component\Form\Form;
17+
use Symfony\Component\Form\FormBuilder;
18+
use Symfony\Component\Form\FormFactoryInterface;
19+
20+
/**
21+
* Form integration for controller classes.
22+
*
23+
* @author Fabien Potencier <fabien@symfony.com>
24+
* @author Alexander M. Turek <me@derrabus.de>
25+
*/
26+
trait FormHelperTrait
27+
{
28+
/**
29+
* @var FormFactoryInterface
30+
*/
31+
protected $formFactory;
32+
33+
/**
34+
* @return FormFactoryInterface
35+
*/
36+
protected function getFormFactory()
37+
{
38+
if ($this->formFactory === null) {
39+
if (!isset($this->container)) {
40+
throw new LogicException('Unable to load the form factory. Please either set the $formFactory property or make'.__CLASS__.' container-aware.');
41+
}
42+
43+
$this->formFactory = $this->container->get('form.factory');
44+
}
45+
46+
return $this->formFactory;
47+
}
48+
49+
/**
50+
* Creates and returns a Form instance from the type of the form.
51+
*
52+
* @param string $type The fully qualified class name of the form type
53+
* @param mixed $data The initial data for the form
54+
* @param array $options Options for the form
55+
*
56+
* @return Form
57+
*/
58+
protected function createForm($type, $data = null, array $options = array())
59+
{
60+
return $this->getFormFactory()->create($type, $data, $options);
61+
}
62+
63+
/**
64+
* Creates and returns a form builder instance.
65+
*
66+
* @param mixed $data The initial data for the form
67+
* @param array $options Options for the form
68+
*
69+
* @return FormBuilder
70+
*/
71+
protected function createFormBuilder($data = null, array $options = array())
72+
{
73+
return $this->getFormFactory()->createBuilder(FormType::class, $data, $options);
74+
}
75+
}
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Kernel;
13+
14+
use Symfony\Bundle\FrameworkBundle\Exception\LogicException;
15+
use Symfony\Component\HttpFoundation\RequestStack;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
18+
19+
/**
20+
* HttpKernel integration for controller classes.
21+
*
22+
* @author Fabien Potencier <fabien@symfony.com>
23+
* @author Alexander M. Turek <me@derrabus.de>
24+
*/
25+
trait KernelHelperTrait
26+
{
27+
/**
28+
* @var RequestStack
29+
*/
30+
protected $requestStack;
31+
32+
/**
33+
* @var HttpKernelInterface
34+
*/
35+
protected $httpKernel;
36+
37+
/**
38+
* @return RequestStack
39+
*/
40+
protected function getRequestStack()
41+
{
42+
if ($this->requestStack === null) {
43+
if (!isset($this->container)) {
44+
throw new LogicException('Unable to retrieve the request stack. Please either set the $requestStack property or make'.__CLASS__.' container-aware.');
45+
}
46+
47+
$this->requestStack = $this->container->get('request_stack');
48+
}
49+
50+
return $this->requestStack;
51+
}
52+
53+
/**
54+
* @return HttpKernelInterface
55+
*/
56+
protected function getHttpKernel()
57+
{
58+
if ($this->httpKernel === null) {
59+
if (!isset($this->container)) {
60+
throw new LogicException('Unable to retrieve the HTTP kernel. Please either set the $httpKernel property or make'.__CLASS__.' container-aware.');
61+
}
62+
63+
$this->httpKernel = $this->container->get('http_kernel');
64+
}
65+
66+
return $this->httpKernel;
67+
}
68+
69+
/**
70+
* Forwards the request to another controller.
71+
*
72+
* @param string $controller The controller name (a string like BlogBundle:Post:index)
73+
* @param array $path An array of path parameters
74+
* @param array $query An array of query parameters
75+
*
76+
* @return Response A Response instance
77+
*/
78+
protected function forward($controller, array $path = array(), array $query = array())
79+
{
80+
$path['_controller'] = $controller;
81+
$subRequest = $this->getRequestStack()->getCurrentRequest()->duplicate($query, null, $path);
82+
83+
return $this->getHttpKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
84+
}
85+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.