1 : <?php
2 :
3 : /**
4 : * Dictionaries of Old French and Latin
5 : *
6 : * PHP 5
7 : *
8 : * @category Application
9 : * @package DicFro
10 : * @author Michel Corne <mcorne@yahoo.com>
11 : * @copyright 2008-2010 Michel Corne
12 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
13 : * @link http://www.micmap.org/dicfro
14 : */
15 :
16 : require_once 'Common/String.php';
17 : require_once 'Engine/View.php';
18 :
19 : /**
20 : * Front controller
21 : *
22 : * @category Application
23 : * @package DicFro
24 : * @author Michel Corne <mcorne@yahoo.com>
25 : * @copyright 2008-2010 Michel Corne
26 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
27 : */
28 :
29 : class Engine_Front
30 : {
31 : /**
32 : * Name of the controller action
33 : * @var string
34 : */
35 : public $action;
36 :
37 : /**
38 : * Action parameters
39 : * @var string
40 : */
41 : public $actionParams = array();
42 :
43 : /**
44 : * Configuration directives
45 : * @var array
46 : */
47 : public $config;
48 :
49 : /**
50 : * List of controller classes
51 : * @var array
52 : */
53 : protected $controllerClasses = array(
54 : 'interface' => 'Controller_Interface',
55 : );
56 :
57 : /**
58 : * Name of the controller
59 : * @var string
60 : */
61 : public $controllerName;
62 :
63 : /**
64 : * Input parameters
65 : * @var array
66 : */
67 : public $params;
68 :
69 : /**
70 : * View object
71 : * @var object
72 : */
73 : public $view;
74 :
75 : /**
76 : * Constructor
77 : *
78 : * @param string $config the configuration directives
79 : * @param object $view the view
80 : * @return void
81 : */
82 : public function __construct($config, Engine_View $view)
83 : {
84 36 : $this->config = $config;
85 36 : $this->view = $view;
86 36 : }
87 :
88 : /**
89 : * Executes the controller action
90 : *
91 : * @param object $view the view
92 : * @return void
93 : */
94 : public function callController()
95 : {
96 : // instantiate the controller
97 2 : $class = $this->controllerClasses[$this->controllerName];
98 2 : $file = str_replace('_', '/', $class) . '.php';
99 2 : require_once $file;
100 2 : $controller = new $class($this);
101 :
102 : // calls the controller action
103 2 : $controller->init();
104 2 : $method = $this->action . 'Action';
105 2 : $controller->$method();
106 2 : $controller->finish();
107 2 : }
108 :
109 : /**
110 : * Matches the route to a controller name, action and function name
111 : *
112 : * @return true
113 : */
114 : public function matchRoute()
115 : {
116 : // format: [/base-url][/environment][/action][/param1]...
117 : // ex. "dicfro/search/vandaele/xyz" or "dicfro/beta/search/vandaele/xyz"
118 : // note: not capturing controller name
119 2 : $baseUrl = empty($this->config['base-url'])?
120 1 : '' :
121 2 : "/{$this->config['base-url']}";
122 2 : $routePattern = "~^$baseUrl(?:/([^/?]+))?(?:/([^/?]+))?(?:/([^/?]+))?(?:/([^/?]+))?(?:/([^/?]+))?(?:/([^/?]+))?~";
123 :
124 2 : if (preg_match($routePattern, $_SERVER['REQUEST_URI'], $this->actionParams)) {
125 2 : array_shift($this->actionParams);
126 2 : $this->removeEnvironment();
127 2 : $this->setAction();
128 2 : }
129 :
130 2 : $this->controllerName = 'interface';
131 :
132 2 : return true;
133 : }
134 :
135 : /**
136 : * Removes the environement from the parameters
137 : *
138 : * @return void
139 : */
140 : public function removeEnvironment()
141 : {
142 3 : isset($this->actionParams[0]) and
143 2 : $this->actionParams[0] == $this->config['environment'] and
144 2 : array_shift($this->actionParams);
145 3 : }
146 :
147 : /**
148 : * Runs the front controller
149 : *
150 : * @return void
151 : */
152 : public function run()
153 : {
154 1 : $this->matchRoute() and
155 1 : $this->setParams();
156 1 : $this->callController();
157 1 : }
158 :
159 : /**
160 : * Sets the action
161 : *
162 : * @return void
163 : */
164 : public function setAction()
165 : {
166 3 : $action = array_shift($this->actionParams);
167 3 : $string = new Common_String;
168 3 : $this->action = $string->dash2CamelCase($action);
169 3 : }
170 :
171 : /**
172 : * Sets URL parameters
173 : *
174 : * @return void
175 : */
176 : public function setParams()
177 : {
178 2 : $this->params = empty($_GET)?
179 2 : array() :
180 1 : $_GET;
181 2 : }
|