Skip to content

Commit ff7cfe8

Browse files
committed
Transform abstract Controller class into a trait.
1 parent 45e26ca commit ff7cfe8

File tree

3 files changed

+406
-383
lines changed

3 files changed

+406
-383
lines changed

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

Lines changed: 5 additions & 372 deletions
Original file line numberDiff line numberDiff line change
@@ -12,386 +12,19 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Controller;
1313

1414
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16-
use Symfony\Component\HttpFoundation\Request;
17-
use Symfony\Component\HttpFoundation\Response;
18-
use Symfony\Component\HttpFoundation\RedirectResponse;
19-
use Symfony\Component\HttpFoundation\StreamedResponse;
20-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21-
use Symfony\Component\HttpKernel\HttpKernelInterface;
22-
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
23-
use Symfony\Component\Security\Csrf\CsrfToken;
24-
use Symfony\Component\Form\Extension\Core\Type\FormType;
25-
use Symfony\Component\Form\FormTypeInterface;
26-
use Symfony\Component\Form\Form;
27-
use Symfony\Component\Form\FormBuilder;
28-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
29-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
30-
use Doctrine\Bundle\DoctrineBundle\Registry;
15+
16+
@trigger_error('The '.Controller::class.' class is deprecated since version 3.1 and will be removed in 4.0. Use the '.ControllerTrait::class.' trait instead.', E_USER_DEPRECATED);
3117

3218
/**
3319
* Controller is a simple implementation of a Controller.
3420
*
3521
* It provides methods to common features needed in controllers.
3622
*
3723
* @author Fabien Potencier <fabien@symfony.com>
24+
*
25+
* @deprecated since version 3.1, to be removed in 4.0. Use the ControllerTrait instead.
3826
*/
3927
abstract class Controller implements ContainerAwareInterface
4028
{
41-
use ContainerAwareTrait;
42-
43-
/**
44-
* Generates a URL from the given parameters.
45-
*
46-
* @param string $route The name of the route
47-
* @param mixed $parameters An array of parameters
48-
* @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
49-
*
50-
* @return string The generated URL
51-
*
52-
* @see UrlGeneratorInterface
53-
*/
54-
protected function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
55-
{
56-
return $this->container->get('router')->generate($route, $parameters, $referenceType);
57-
}
58-
59-
/**
60-
* Forwards the request to another controller.
61-
*
62-
* @param string $controller The controller name (a string like BlogBundle:Post:index)
63-
* @param array $path An array of path parameters
64-
* @param array $query An array of query parameters
65-
*
66-
* @return Response A Response instance
67-
*/
68-
protected function forward($controller, array $path = array(), array $query = array())
69-
{
70-
$path['_controller'] = $controller;
71-
$subRequest = $this->container->get('request_stack')->getCurrentRequest()->duplicate($query, null, $path);
72-
73-
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
74-
}
75-
76-
/**
77-
* Returns a RedirectResponse to the given URL.
78-
*
79-
* @param string $url The URL to redirect to
80-
* @param int $status The status code to use for the Response
81-
*
82-
* @return RedirectResponse
83-
*/
84-
protected function redirect($url, $status = 302)
85-
{
86-
return new RedirectResponse($url, $status);
87-
}
88-
89-
/**
90-
* Returns a RedirectResponse to the given route with the given parameters.
91-
*
92-
* @param string $route The name of the route
93-
* @param array $parameters An array of parameters
94-
* @param int $status The status code to use for the Response
95-
*
96-
* @return RedirectResponse
97-
*/
98-
protected function redirectToRoute($route, array $parameters = array(), $status = 302)
99-
{
100-
return $this->redirect($this->generateUrl($route, $parameters), $status);
101-
}
102-
103-
/**
104-
* Adds a flash message to the current session for type.
105-
*
106-
* @param string $type The type
107-
* @param string $message The message
108-
*
109-
* @throws \LogicException
110-
*/
111-
protected function addFlash($type, $message)
112-
{
113-
if (!$this->container->has('session')) {
114-
throw new \LogicException('You can not use the addFlash method if sessions are disabled.');
115-
}
116-
117-
$this->container->get('session')->getFlashBag()->add($type, $message);
118-
}
119-
120-
/**
121-
* Checks if the attributes are granted against the current authentication token and optionally supplied object.
122-
*
123-
* @param mixed $attributes The attributes
124-
* @param mixed $object The object
125-
*
126-
* @return bool
127-
*
128-
* @throws \LogicException
129-
*/
130-
protected function isGranted($attributes, $object = null)
131-
{
132-
if (!$this->container->has('security.authorization_checker')) {
133-
throw new \LogicException('The SecurityBundle is not registered in your application.');
134-
}
135-
136-
return $this->container->get('security.authorization_checker')->isGranted($attributes, $object);
137-
}
138-
139-
/**
140-
* Throws an exception unless the attributes are granted against the current authentication token and optionally
141-
* supplied object.
142-
*
143-
* @param mixed $attributes The attributes
144-
* @param mixed $object The object
145-
* @param string $message The message passed to the exception
146-
*
147-
* @throws AccessDeniedException
148-
*/
149-
protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
150-
{
151-
if (!$this->isGranted($attributes, $object)) {
152-
throw $this->createAccessDeniedException($message);
153-
}
154-
}
155-
156-
/**
157-
* Returns a rendered view.
158-
*
159-
* @param string $view The view name
160-
* @param array $parameters An array of parameters to pass to the view
161-
*
162-
* @return string The rendered view
163-
*/
164-
protected function renderView($view, array $parameters = array())
165-
{
166-
if ($this->container->has('templating')) {
167-
return $this->container->get('templating')->render($view, $parameters);
168-
}
169-
170-
if (!$this->container->has('twig')) {
171-
throw new \LogicException('You can not use the "renderView" method if the Templating Component or the Twig Bundle are not available.');
172-
}
173-
174-
return $this->container->get('twig')->render($view, $parameters);
175-
}
176-
177-
/**
178-
* Renders a view.
179-
*
180-
* @param string $view The view name
181-
* @param array $parameters An array of parameters to pass to the view
182-
* @param Response $response A response instance
183-
*
184-
* @return Response A Response instance
185-
*/
186-
protected function render($view, array $parameters = array(), Response $response = null)
187-
{
188-
if ($this->container->has('templating')) {
189-
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
190-
}
191-
192-
if (!$this->container->has('twig')) {
193-
throw new \LogicException('You can not use the "render" method if the Templating Component or the Twig Bundle are not available.');
194-
}
195-
196-
if (null === $response) {
197-
$response = new Response();
198-
}
199-
200-
$response->setContent($this->container->get('twig')->render($view, $parameters));
201-
202-
return $response;
203-
}
204-
205-
/**
206-
* Streams a view.
207-
*
208-
* @param string $view The view name
209-
* @param array $parameters An array of parameters to pass to the view
210-
* @param StreamedResponse $response A response instance
211-
*
212-
* @return StreamedResponse A StreamedResponse instance
213-
*/
214-
protected function stream($view, array $parameters = array(), StreamedResponse $response = null)
215-
{
216-
if ($this->container->has('templating')) {
217-
$templating = $this->container->get('templating');
218-
219-
$callback = function () use ($templating, $view, $parameters) {
220-
$templating->stream($view, $parameters);
221-
};
222-
} elseif ($this->container->has('twig')) {
223-
$twig = $this->container->get('twig');
224-
225-
$callback = function () use ($twig, $view, $parameters) {
226-
$twig->display($view, $parameters);
227-
};
228-
} else {
229-
throw new \LogicException('You can not use the "stream" method if the Templating Component or the Twig Bundle are not available.');
230-
}
231-
232-
if (null === $response) {
233-
return new StreamedResponse($callback);
234-
}
235-
236-
$response->setCallback($callback);
237-
238-
return $response;
239-
}
240-
241-
/**
242-
* Returns a NotFoundHttpException.
243-
*
244-
* This will result in a 404 response code. Usage example:
245-
*
246-
* throw $this->createNotFoundException('Page not found!');
247-
*
248-
* @param string $message A message
249-
* @param \Exception|null $previous The previous exception
250-
*
251-
* @return NotFoundHttpException
252-
*/
253-
protected function createNotFoundException($message = 'Not Found', \Exception $previous = null)
254-
{
255-
return new NotFoundHttpException($message, $previous);
256-
}
257-
258-
/**
259-
* Returns an AccessDeniedException.
260-
*
261-
* This will result in a 403 response code. Usage example:
262-
*
263-
* throw $this->createAccessDeniedException('Unable to access this page!');
264-
*
265-
* @param string $message A message
266-
* @param \Exception|null $previous The previous exception
267-
*
268-
* @return AccessDeniedException
269-
*/
270-
protected function createAccessDeniedException($message = 'Access Denied.', \Exception $previous = null)
271-
{
272-
return new AccessDeniedException($message, $previous);
273-
}
274-
275-
/**
276-
* Creates and returns a Form instance from the type of the form.
277-
*
278-
* @param string|FormTypeInterface $type The built type of the form
279-
* @param mixed $data The initial data for the form
280-
* @param array $options Options for the form
281-
*
282-
* @return Form
283-
*/
284-
protected function createForm($type, $data = null, array $options = array())
285-
{
286-
return $this->container->get('form.factory')->create($type, $data, $options);
287-
}
288-
289-
/**
290-
* Creates and returns a form builder instance.
291-
*
292-
* @param mixed $data The initial data for the form
293-
* @param array $options Options for the form
294-
*
295-
* @return FormBuilder
296-
*/
297-
protected function createFormBuilder($data = null, array $options = array())
298-
{
299-
return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
300-
}
301-
302-
/**
303-
* Shortcut to return the Doctrine Registry service.
304-
*
305-
* @return Registry
306-
*
307-
* @throws \LogicException If DoctrineBundle is not available
308-
*/
309-
protected function getDoctrine()
310-
{
311-
if (!$this->container->has('doctrine')) {
312-
throw new \LogicException('The DoctrineBundle is not registered in your application.');
313-
}
314-
315-
return $this->container->get('doctrine');
316-
}
317-
318-
/**
319-
* Get a user from the Security Token Storage.
320-
*
321-
* @return mixed
322-
*
323-
* @throws \LogicException If SecurityBundle is not available
324-
*
325-
* @see TokenInterface::getUser()
326-
*/
327-
protected function getUser()
328-
{
329-
if (!$this->container->has('security.token_storage')) {
330-
throw new \LogicException('The SecurityBundle is not registered in your application.');
331-
}
332-
333-
if (null === $token = $this->container->get('security.token_storage')->getToken()) {
334-
return;
335-
}
336-
337-
if (!is_object($user = $token->getUser())) {
338-
// e.g. anonymous authentication
339-
return;
340-
}
341-
342-
return $user;
343-
}
344-
345-
/**
346-
* Returns true if the service id is defined.
347-
*
348-
* @param string $id The service id
349-
*
350-
* @return bool true if the service id is defined, false otherwise
351-
*/
352-
protected function has($id)
353-
{
354-
return $this->container->has($id);
355-
}
356-
357-
/**
358-
* Gets a container service by its id.
359-
*
360-
* @param string $id The service id
361-
*
362-
* @return object The service
363-
*/
364-
protected function get($id)
365-
{
366-
return $this->container->get($id);
367-
}
368-
369-
/**
370-
* Gets a container configuration parameter by its name.
371-
*
372-
* @param string $name The parameter name
373-
*
374-
* @return mixed
375-
*/
376-
protected function getParameter($name)
377-
{
378-
return $this->container->getParameter($name);
379-
}
380-
381-
/**
382-
* Checks the validity of a CSRF token.
383-
*
384-
* @param string $id The id used when generating the token
385-
* @param string $token The actual token sent with the request that should be validated
386-
*
387-
* @return bool
388-
*/
389-
protected function isCsrfTokenValid($id, $token)
390-
{
391-
if (!$this->container->has('security.csrf.token_manager')) {
392-
throw new \LogicException('CSRF protection is not enabled in your application.');
393-
}
394-
395-
return $this->container->get('security.csrf.token_manager')->isTokenValid(new CsrfToken($id, $token));
396-
}
29+
use ControllerTrait;
39730
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy