-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDefaultCiActionResolver.php
More file actions
100 lines (87 loc) · 2.98 KB
/
DefaultCiActionResolver.php
File metadata and controls
100 lines (87 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/*
* Copyright 2012 Nerijus Arlauskas <nercury@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Nercury\CodeIgniterBundle;
/**
* This is default event listener to resolve CI actions.
* It resolves /{controller}/{function} action
*/
class DefaultCiActionResolver
{
/**
* @var string
*/
protected $defaultLocale;
/**
* @var CiControllerChecker
*/
protected $controllerChecker;
public function __construct(CiControllerChecker $controllerChecker, $defaultLocale)
{
$this->defaultLocale = $defaultLocale;
$this->controllerChecker = $controllerChecker;
}
public function addPossibleRoutes(CiActionResolveEvent $event, &$pathParts, $indexOfFirst, $locale)
{
$controllerPath = '';
for ($i = $indexOfFirst; $i < count($pathParts) && $i < 10; $i++) {
if ($controllerPath == '') {
$controllerPath = $pathParts[$i];
} else {
$controllerPath .= '/'.$pathParts[$i];
}
$next = $i < count($pathParts) - 1 ? $pathParts[$i + 1] : false;
if (!$this->controllerChecker->isControllerExist($controllerPath)) {
continue;
}
$event->addPossibleAction(
$controllerPath,
$next !== false ? $next : 'index',
$locale
);
}
}
/**
* This method collects possible routes for a request.
*
* @param CiActionResolveEvent $event
*/
public function onActionResolveEvent(CiActionResolveEvent $event)
{
$path = $event->getRequest()->getPathInfo();
$part = (string) substr($path, 1);
if ($part === '') {
$part = 'home/';
}
$parts = explode('/', $part);
if (count($parts) === 1) {
$parts[] = '';
}
$indexOfFirst = 0;
if (count($parts) > 1) {
if (false !== strpos($parts[0], '.php')) {
$indexOfFirst++;
}
if (count($parts) > $indexOfFirst + 1) {
$this->addPossibleRoutes($event, $parts, $indexOfFirst, $this->defaultLocale);
// add routes in case first part is a language string, i.e /en/...
if (count($parts) > $indexOfFirst + 2 && strlen($parts[0]) > 1 && strlen($parts[0]) <= 2) {
$this->addPossibleRoutes($event, $parts, $indexOfFirst + 1, $parts[0]);
}
}
}
}
}