|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
namespace Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter; |
|
|
4 |
|
|
|
5 |
use Symfony\Component\HttpFoundation\Request; |
|
|
6 |
|
|
|
7 |
/* |
|
|
8 |
* This file is part of the Symfony framework. |
|
|
9 |
* |
|
|
10 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
11 |
* |
|
|
12 |
* This source file is subject to the MIT license that is bundled |
|
|
13 |
* with this source code in the file LICENSE. |
|
|
14 |
*/ |
|
|
15 |
|
|
|
16 |
/** |
|
|
17 |
* Managers converters. |
|
|
18 |
* |
|
|
19 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
20 |
* @author Henrik Bjornskov <henrik@bjrnskov.dk> |
|
|
21 |
*/ |
|
|
22 |
class ParamConverterManager |
|
|
23 |
{ |
|
|
24 |
/** |
|
|
25 |
* @var array |
|
|
26 |
*/ |
|
|
27 |
protected $converters = array(); |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* Applies all converters to the passed configurations and stops when a |
|
|
31 |
* converter is applied it will move on to the next configuration and so on. |
|
|
32 |
* |
|
|
33 |
* @param Request $request |
|
|
34 |
* @param array|object $configurations |
|
|
35 |
*/ |
|
|
36 |
public function apply(Request $request, $configurations) |
|
|
37 |
{ |
|
|
38 |
if (is_object($configurations)) { |
|
|
39 |
$configurations = array($configurations); |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
foreach ($configurations as $configuration) { |
|
|
43 |
foreach ($this->all() as $converter) { |
|
|
44 |
if ($converter->supports($configuration)) { |
|
|
45 |
if ($request->attributes->has($configuration->getName())) { |
|
|
46 |
continue 2; |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
$converter->apply($request, $configuration); |
|
|
50 |
} |
|
|
51 |
} |
|
|
52 |
} |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
/** |
|
|
56 |
* Adds a parameter converter. |
|
|
57 |
* |
|
|
58 |
* @param ParamConverterInterface $converter A ParamConverterInterface instance |
|
|
59 |
* @param integer $priority The priority (between -10 and 10) |
|
|
60 |
*/ |
|
|
61 |
public function add(ParamConverterInterface $converter, $priority = 0) |
|
|
62 |
{ |
|
|
63 |
if (!isset($this->converters[$priority])) { |
|
|
64 |
$this->converters[$priority] = array(); |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
$this->converters[$priority][] = $converter; |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
/** |
|
|
71 |
* Returns all registered param converters. |
|
|
72 |
* |
|
|
73 |
* @return array An array of param converters |
|
|
74 |
*/ |
|
|
75 |
public function all() |
|
|
76 |
{ |
|
|
77 |
krsort($this->converters); |
|
|
78 |
|
|
|
79 |
$converters = array(); |
|
|
80 |
foreach ($this->converters as $all) { |
|
|
81 |
$converters = array_merge($converters, $all); |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
return $converters; |
|
|
85 |
} |
|
|
86 |
} |