1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OrbitaleCmsBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Rock Ancelet <[email protected]> |
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 Orbitale\Bundle\CmsBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Orbitale\Bundle\CmsBundle\Entity\Category; |
15
|
|
|
use Orbitale\Bundle\CmsBundle\Entity\Page; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
17
|
|
|
|
18
|
|
|
abstract class AbstractCmsController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Slugs HAVE TO be ordered exactly as in the request. |
22
|
|
|
* This method will check that, in $elements, we have the same keys as in $slugs, |
23
|
|
|
* and that the hierarchy is correct. |
24
|
|
|
* This also prevents things like /children/parent to work, |
25
|
|
|
* as it should be /parent/children. |
26
|
|
|
* |
27
|
|
|
* @param array $slugs |
28
|
|
|
* @param Page[]|Category[] $elements |
29
|
|
|
* |
30
|
|
|
* @return Category|Page |
31
|
|
|
*/ |
32
|
|
|
protected function getFinalTreeElement(array $slugs, array $elements) |
33
|
|
|
{ |
34
|
|
|
// Will check that slugs and elements match |
35
|
|
|
$slugsElements = array_keys($elements); |
36
|
|
|
$sortedSlugs = $slugs; |
37
|
|
|
sort($sortedSlugs); |
38
|
|
|
sort($slugsElements); |
39
|
|
|
|
40
|
|
|
if ($sortedSlugs !== $slugsElements || !count($slugs) || count($slugs) !== count($elements)) { |
41
|
|
|
throw $this->createNotFoundException(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @var Page|Category $element */ |
45
|
|
|
$element = null; |
46
|
|
|
/** @var Page|Category $previousElement */ |
47
|
|
|
$previousElement = null; |
48
|
|
|
|
49
|
|
|
foreach ($slugs as $slug) { |
50
|
|
|
$element = isset($elements[$slug]) ? $elements[$slug] : null; |
51
|
|
|
$match = false; |
52
|
|
|
if ($element) { |
53
|
|
|
// Only for the first iteration |
54
|
|
|
$match = $previousElement |
55
|
|
|
? $element->getParent() && $previousElement->getSlug() === $element->getParent()->getSlug() |
56
|
|
|
: true; |
57
|
|
|
|
58
|
|
|
$previousElement = $element; |
59
|
|
|
} |
60
|
|
|
if (!$match) { |
61
|
|
|
throw $this->createNotFoundException((new \ReflectionClass($element))->getShortName().' hierarchy not found.'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $element; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|