| 1 | <?php |
|---|
| 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 | final class Init |
|---|
| 32 | { |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | private $_configs; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | private $_view = null; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | private $_viewSuffix = ''; |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | private $_params = array(); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | private $_dirs = array(); |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | private $_modules = array(); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | private $_routes = null; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | public function __construct($appPath = null) |
|---|
| 96 | { |
|---|
| 97 | if (is_null($appPath) === true) { |
|---|
| 98 | $appPath = dirname(__FILE__); |
|---|
| 99 | } |
|---|
| 100 | $appPath = rtrim($appPath, '/\\') . DIRECTORY_SEPARATOR; |
|---|
| 101 | $this->_setParam('appPath', $appPath); |
|---|
| 102 | |
|---|
| 103 | require_once 'Zend/Loader.php'; |
|---|
| 104 | Zend_Loader::registerAutoload(); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | public function load() |
|---|
| 114 | { |
|---|
| 115 | $this->_loadCoreConfig() |
|---|
| 116 | ->_loadTemplateEngine() |
|---|
| 117 | ->_setViewRender() |
|---|
| 118 | ->_loadLayoutSetting() |
|---|
| 119 | ->_loadPaths() |
|---|
| 120 | ->_loadDbAdapter(); |
|---|
| 121 | |
|---|
| 122 | return $this; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | private function _loadDbAdapter() |
|---|
| 132 | { |
|---|
| 133 | $file = $this->_getParam('appPath') . 'configs' . DIRECTORY_SEPARATOR . 'database.ini'; |
|---|
| 134 | $ini = $this->_loadConfig($file, $this->_configs['global']['env_mode'], false); |
|---|
| 135 | $db = Zend_Db::factory($ini->database->adapter, $ini->database->params->toArray()); |
|---|
| 136 | $db->setFetchMode(Zend_Db::FETCH_ASSOC); |
|---|
| 137 | Zend_Db_Table_Abstract::setDefaultAdapter($db); |
|---|
| 138 | |
|---|
| 139 | return $this; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | private function _loadTemplateEngine() |
|---|
| 150 | { |
|---|
| 151 | |
|---|
| 152 | $engine = $this->_configs['template']['engine']; |
|---|
| 153 | if (!isset($engine)) { |
|---|
| 154 | throw new Zend_Config_Exception('Template engine does not set.'); |
|---|
| 155 | } |
|---|
| 156 | $rootDir = $this->_getParam('rootDir'); |
|---|
| 157 | $viewIniFile = $this->_getParam('appPath') . 'configs' . DIRECTORY_SEPARATOR . 'view.ini'; |
|---|
| 158 | $config = $this->_loadConfig($viewIniFile, $engine); |
|---|
| 159 | |
|---|
| 160 | $className = 'Phwittr_View_Adapter_' . $engine; |
|---|
| 161 | $reflection = new ReflectionClass($className); |
|---|
| 162 | $args = array($config, $rootDir); |
|---|
| 163 | $class = $reflection->newInstanceArgs($args); |
|---|
| 164 | $this->_view = $class->getView(); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | if (isset($this->_configs['template']['helper']['path']) |
|---|
| 168 | && isset($this->_configs['template']['helper']['prefix'])) { |
|---|
| 169 | |
|---|
| 170 | $path = $rootDir . $this->_configs['template']['helper']['path']; |
|---|
| 171 | $prefix = $this->_configs['template']['helper']['prefix']; |
|---|
| 172 | |
|---|
| 173 | $this->_view->addHelperPath($path, $prefix); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | return $this; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | private function _loadLayoutSetting() |
|---|
| 186 | { |
|---|
| 187 | |
|---|
| 188 | $rootDir = $this->_getParam('rootDir'); |
|---|
| 189 | if (is_array($this->_configs['layout']) |
|---|
| 190 | && $this->_configs['layout']['layoutPath']) { |
|---|
| 191 | |
|---|
| 192 | $layoutArray = $this->_configs['layout']['layoutPath']; |
|---|
| 193 | $pathArray = pathinfo($layoutArray); |
|---|
| 194 | $layoutName = $pathArray['filename']; |
|---|
| 195 | $suffix = $pathArray['extension']; |
|---|
| 196 | $layoutPath = $rootDir . $pathArray['dirname']; |
|---|
| 197 | |
|---|
| 198 | if (isset($this->_configs['layout']['contentKey'])) { |
|---|
| 199 | $contentKey = $this->_configs['layout']['contentKey']; |
|---|
| 200 | } else { |
|---|
| 201 | $contentKey = 'content'; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | $options = array( |
|---|
| 205 | 'layout' => $layoutName, |
|---|
| 206 | 'layoutPath' => $layoutPath, |
|---|
| 207 | 'contentKey' => $contentKey |
|---|
| 208 | ); |
|---|
| 209 | |
|---|
| 210 | $layout = new Zend_Layout($options, true); |
|---|
| 211 | } |
|---|
| 212 | return $this; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | private function _setViewRender() |
|---|
| 223 | { |
|---|
| 224 | |
|---|
| 225 | if ($this->_view instanceof Zend_View) { |
|---|
| 226 | return $this; |
|---|
| 227 | } |
|---|
| 228 | if ($this->_view == null) { |
|---|
| 229 | throw new Zend_Exception('View object is null.'); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | $viewSuffix = 'php'; |
|---|
| 234 | if (isset($this->_configs['template']['suffix'])) { |
|---|
| 235 | $viewSuffix = $this->_configs['template']['suffix']; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | $this->_viewSuffix = $viewSuffix; |
|---|
| 240 | |
|---|
| 241 | $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer(); |
|---|
| 242 | $viewRenderer->setView($this->_view) |
|---|
| 243 | ->setViewSuffix($viewSuffix); |
|---|
| 244 | Zend_Controller_Action_HelperBroker::addHelper($viewRenderer); |
|---|
| 245 | |
|---|
| 246 | return $this; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | private function _getParam($key = null) |
|---|
| 257 | { |
|---|
| 258 | if ($key === null) { |
|---|
| 259 | return $this->_params; |
|---|
| 260 | } |
|---|
| 261 | if (!array_key_exists($key, $this->_params)) { |
|---|
| 262 | return null; |
|---|
| 263 | } |
|---|
| 264 | return $this->_params[$key]; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | private function _setParam($key, $value) |
|---|
| 276 | { |
|---|
| 277 | $this->_params[$key] = $value; |
|---|
| 278 | return $this; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | private function _loadConfig($file, $section = null, $toArray = true) |
|---|
| 292 | { |
|---|
| 293 | if (!file_exists($file)) { |
|---|
| 294 | throw new Zend_Config_Exception('Could not load ini file.'); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | $config = new Zend_Config_Ini($file, $section); |
|---|
| 298 | if ($toArray === true) { |
|---|
| 299 | return $config->toArray(); |
|---|
| 300 | } |
|---|
| 301 | return $config; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | private function _loadCoreConfig() |
|---|
| 312 | { |
|---|
| 313 | $appPath = $this->_getParam('appPath'); |
|---|
| 314 | $file = $appPath . 'configs' . DIRECTORY_SEPARATOR . 'core.ini'; |
|---|
| 315 | if (!file_exists($file)) { |
|---|
| 316 | throw new Zend_Config_Exception('Could not load configs.ini.'); |
|---|
| 317 | } |
|---|
| 318 | $this->_configs = $this->_loadConfig($file); |
|---|
| 319 | |
|---|
| 320 | $mode = 'production'; |
|---|
| 321 | if (isset($this->_configs['global']['env_mode'])) { |
|---|
| 322 | $mode = $this->_configs['global']['env_mode']; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | $rootDir = dirname(dirname(__FILE__)); |
|---|
| 326 | if (isset($this->_configs['global']['root_dir'])) { |
|---|
| 327 | $rootDir = $this->_configs['global']['root_dir']; |
|---|
| 328 | } |
|---|
| 329 | $rootDir = rtrim($rootDir, '\//') . DIRECTORY_SEPARATOR; |
|---|
| 330 | |
|---|
| 331 | $moduleDir = $appPath . 'modules'; |
|---|
| 332 | if (isset($this->_configs['global']['modules_dir'])) { |
|---|
| 333 | $moduleDir = $this->_configs['global']['modules_dir']; |
|---|
| 334 | } |
|---|
| 335 | $moduleDir = rtrim($moduleDir, '\//') . DIRECTORY_SEPARATOR; |
|---|
| 336 | |
|---|
| 337 | $cacheDir = $rootDir . 'var' . DIRECTORY_SEPARATOR . 'cache'; |
|---|
| 338 | if (isset($this->_configs['global']['cache_dir'])) { |
|---|
| 339 | $cacheDir = $this->_configs['global']['cache_dir']; |
|---|
| 340 | } |
|---|
| 341 | $cacheDir = rtrim($cacheDir, '\//') . DIRECTORY_SEPARATOR; |
|---|
| 342 | |
|---|
| 343 | $logDir = $rootDir . 'var' . DIRECTORY_SEPARATOR . 'log'; |
|---|
| 344 | if (isset($this->_configs['global']['log_dir'])) { |
|---|
| 345 | $logDir = $this->_configs['global']['log_dir']; |
|---|
| 346 | } |
|---|
| 347 | $logDir = rtrim($logDir, '/\\') . DIRECTORY_SEPARATOR; |
|---|
| 348 | $this->_setParam('rootDir', $rootDir); |
|---|
| 349 | $this->_setParam('cacheDir', $cacheDir); |
|---|
| 350 | $this->_setParam('moduleDir', $moduleDir); |
|---|
| 351 | $this->_setParam('logDir', $logDir); |
|---|
| 352 | $this->_setParam('envMode', $mode); |
|---|
| 353 | |
|---|
| 354 | return $this; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | private function _loadPaths() |
|---|
| 365 | { |
|---|
| 366 | $appPath = $this->_getParam('appPath'); |
|---|
| 367 | $file = $appPath . 'configs' . DIRECTORY_SEPARATOR . 'setting.ini'; |
|---|
| 368 | if (!file_exists($file)) { |
|---|
| 369 | throw new Zend_Controller_Exception('Could not load setting.ini'); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | $modelPaths = array(); |
|---|
| 374 | $layoutPaths = array(); |
|---|
| 375 | $servicePaths = array(); |
|---|
| 376 | $pathObj = $this->_loadConfig($file, null, false); |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | if (is_object($pathObj) && $pathObj->get('routes') !== null) { |
|---|
| 380 | $this->_routes = $pathObj->get('routes'); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | $paths = $pathObj->toArray(); |
|---|
| 384 | |
|---|
| 385 | if (is_array($paths)) { |
|---|
| 386 | $rootDir = $this->_getParam('rootDir'); |
|---|
| 387 | $moduleDir = $this->_getParam('moduleDir'); |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | foreach ($paths as $key => $val) { |
|---|
| 391 | $this->_modules = array_merge($this->_modules, array_keys($val)); |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | $this->_modules = array_unique($this->_modules); |
|---|
| 396 | foreach ($this->_modules as $key => $val) { |
|---|
| 397 | if (isset($paths['layouts'][$val])) { |
|---|
| 398 | if (is_array($paths['layouts'][$val]['path'])) { |
|---|
| 399 | foreach ($paths['layouts'][$val]['path'] as $layoutKey => $layoutVal) { |
|---|
| 400 | $layoutPaths[$val][$layoutKey] = $rootDir . $layoutVal; |
|---|
| 401 | } |
|---|
| 402 | } else { |
|---|
| 403 | $layoutPaths[$val] = $rootDir . $paths['layouts'][$val]['path']; |
|---|
| 404 | } |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | if (isset($paths['models'][$val])) { |
|---|
| 408 | $modelPaths[$val] = $rootDir . $paths['models'][$val]['path']; |
|---|
| 409 | } |
|---|
| 410 | |
|---|
| 411 | if (isset($paths['services'][$val])) { |
|---|
| 412 | $servicePaths[$val] = $rootDir . $paths['services'][$val]['path']; |
|---|
| 413 | } |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | $this->_setParam('models', $modelPaths); |
|---|
| 417 | $this->_setParam('layouts', $layoutPaths); |
|---|
| 418 | $this->_setParam('services', $servicePaths); |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | return $this; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | private function _getPlugins() |
|---|
| 431 | { |
|---|
| 432 | if (!isset($this->_configs['plugin']['directory'])) { |
|---|
| 433 | return null; |
|---|
| 434 | } |
|---|
| 435 | if (!isset($this->_configs['plugin']['classPrefix'])) { |
|---|
| 436 | return null; |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | $path = rtrim($this->_configs['plugin']['directory'], '/\\') . DIRECTORY_SEPARATOR; |
|---|
| 440 | $path = $this->_getParam('rootDir') . $path; |
|---|
| 441 | $prefix = $this->_configs['plugin']['classPrefix'] . '_'; |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); |
|---|
| 446 | $objArray = array(); |
|---|
| 447 | foreach ($iterator as $val) { |
|---|
| 448 | if ($val->isFile()) { |
|---|
| 449 | $pathArray = pathinfo($val->getPathname()); |
|---|
| 450 | if ($pathArray['extension'] !== 'php') { |
|---|
| 451 | continue; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | require_once $val->getPathname(); |
|---|
| 456 | if (basename($val->getPath()) === basename($path)) { |
|---|
| 457 | $className = $prefix . $pathArray['filename']; |
|---|
| 458 | } else { |
|---|
| 459 | $className = $prefix . basename($val->getPath()) . '_' . $pathArray['filename']; |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | $classObj = new $className; |
|---|
| 463 | if ($classObj instanceof Zend_Controller_Plugin_Abstract) { |
|---|
| 464 | $objArray[] = $classObj; |
|---|
| 465 | } |
|---|
| 466 | } |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | return $objArray; |
|---|
| 470 | } |
|---|
| 471 | |
|---|
| 472 | |
|---|
| 473 | |
|---|
| 474 | |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | private function _initRoute(Zend_Controller_Router_Rewrite $router, $defaultModule) |
|---|
| 481 | { |
|---|
| 482 | if (!$router instanceof Zend_Controller_Router_Rewrite) { |
|---|
| 483 | throw new Zend_Exception('Invaild arguments.'); |
|---|
| 484 | } |
|---|
| 485 | if ($defaultModule === '') { |
|---|
| 486 | throw new Zend_Exception('Invaild arguments.'); |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | $router->removeDefaultRoutes(); |
|---|
| 491 | if (is_array($this->_modules)) { |
|---|
| 492 | $moduleName = ''; |
|---|
| 493 | foreach ($this->_modules as $key => $val) { |
|---|
| 494 | if ($val !== $defaultModule) { |
|---|
| 495 | $moduleName = $val . '/'; |
|---|
| 496 | } |
|---|
| 497 | $route = new Zend_Controller_Router_Route( |
|---|
| 498 | $moduleName . ':controller/:action/*', |
|---|
| 499 | array( |
|---|
| 500 | 'module' => $val, |
|---|
| 501 | 'controller' => 'index', |
|---|
| 502 | 'action' => 'index' |
|---|
| 503 | ) |
|---|
| 504 | ); |
|---|
| 505 | $router->addRoute($val, $route); |
|---|
| 506 | } |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | if ($this->_routes !== null) { |
|---|
| 511 | $router->addConfig($this->_routes); |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | return $this; |
|---|
| 515 | } |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | public function run() |
|---|
| 524 | { |
|---|
| 525 | try { |
|---|
| 526 | $front = Zend_Controller_Front::getInstance(); |
|---|
| 527 | $front->setParam('config', $this->_getParam()); |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | |
|---|
| 536 | $front->registerPlugin(new Phwittr_Controller_Plugin_Login()); |
|---|
| 537 | |
|---|
| 538 | $defaultModule = 'default'; |
|---|
| 539 | if (isset($this->_configs['global']['default_module'])) { |
|---|
| 540 | $defaultModule = $this->_configs['global']['default_module']; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | |
|---|
| 544 | $router = $front->getRouter(); |
|---|
| 545 | $this->_initRoute($router, $defaultModule); |
|---|
| 546 | |
|---|
| 547 | $front->setDefaultModule($defaultModule) |
|---|
| 548 | ->addModuleDirectory($this->_getParam('moduleDir')) |
|---|
| 549 | ->dispatch(); |
|---|
| 550 | |
|---|
| 551 | } catch (Zend_Exception $e) { |
|---|
| 552 | } |
|---|
| 553 | } |
|---|
| 554 | } |
|---|