public function testIsReadable() { $this->assertTrue(Zend::isReadable(__FILE__)); $this->assertFalse(Zend::isReadable(__FILE__ . '.foobaar')); }
/** * If $performDispatch is FALSE, this method will check if a controller * file exists. This still doesn't necessarily mean that it can be dispatched * in the stricted sense, as file may not contain the controller class or the * controller may reject the action. * * If $performDispatch is TRUE, then this method will actually * instantiate the controller and call its action. Calling the action * is done by passing a Zend_Controller_Dispatcher_Token to the controller's constructor. * * @param Zend_Controller_Dispatcher_Token $action * @param boolean $performDispatch * @return boolean|Zend_Controller_Dispatcher_Token */ protected function _dispatch(Zend_Controller_Dispatcher_Token $action, $performDispatch) { if ($this->_directory === null) { throw new Zend_Controller_Dispatcher_Exception('Controller directory never set. Use setControllerDirectory() first.'); } $className = $this->formatControllerName($action->getControllerName()); /** * If $performDispatch is FALSE, only determine if the controller file * can be accessed. */ if (!$performDispatch) { return Zend::isReadable($this->_directory . DIRECTORY_SEPARATOR . $className . '.php'); } Zend::loadClass($className, $this->_directory); $controller = new $className(); if (!$controller instanceof Zend_Controller_Action) { throw new Zend_Controller_Dispatcher_Exception("Controller \"{$className}\" is not an instance of Zend_Controller_Action."); } /** * Dispatch * * Call the action of the Zend_Controller_Action. It will return either null or a * new Zend_Controller_Dispatcher_Token object. If a Zend_Controller_Dispatcher_Token object is returned, this will be returned * back to ZFrontController, which will call $this again to forward to * another action. */ $nextAction = $controller->run($this, $action); // Destroy the page controller instance $controller = null; // Return either null (finished) or a Zend_Controller_Dispatcher_Token object (forward to another action). return $nextAction; }
/** * Get controller name * * Try request first; if not found, try pulling from request parameter; * if still not found, fallback to default * * @param Zend_Controller_Request_Abstract $request * @param array $directories * @return string|false Returns class name on success */ protected function _getController($request, $directories = null) { if (null === $directories) { $directories = $this->getControllerDirectory(); } if (empty($directories)) { throw Zend::exception('Zend_Controller_Dispatcher_Exception', 'Controller directory never set. Use setControllerDirectory() first'); } $controllerName = $request->getControllerName(); if (empty($controllerName)) { $controllerName = $this->getDefaultController(); $request->setControllerName($controllerName); } $className = $this->formatControllerName($controllerName); /** * Determine if controller is dispatchable * * Checks to see if a module name is present in the request; if so, * checks for class file existing in module directory. Otherwise, loops through * directories in FIFO order to find it. */ $dispatchable = false; $module = (string) $request->getParam('module', false); if ($module && isset($directories[$module])) { $dispatchable = Zend::isReadable($directories[$module] . DIRECTORY_SEPARATOR . $className . '.php'); if ($dispatchable) { $this->_curDirectory = $directories[$module]; } } else { foreach ($directories as $directory) { $dispatchable = Zend::isReadable($directory . DIRECTORY_SEPARATOR . $className . '.php'); if ($dispatchable) { $this->_curDirectory = $directory; break; } } } return $dispatchable ? $className : false; }
/** * If $performDispatch is FALSE, this method will check if a controller * file exists. This still doesn't necessarily mean that it can be dispatched * in the stricted sense, as file may not contain the controller class or the * controller may reject the action. * * If $performDispatch is TRUE, then this method will actually * instantiate the controller and call its action. Calling the action * is done by passing a Zend_Controller_Dispatcher_Token to the controller's constructor. * * @param Zend_Controller_Request_Abstract $request * @param boolean $performDispatch * @return void */ protected function _dispatch(Zend_Controller_Request_Abstract $request, $performDispatch = true) { /** * Controller directory check */ if ($this->_directory === null) { throw new Zend_Controller_Dispatcher_Exception('Controller directory never set. Use setControllerDirectory() first'); } /** * Get controller name * * Try request first; if not found, try pulling from request parameter; * if still not found, fallback to default */ $controllerName = $request->getControllerName(); if (empty($controllerName)) { $controllerName = $this->getDefaultController(); } $className = $this->formatControllerName($controllerName); /** * Determine if controller is dispatchable */ $dispatchable = Zend::isReadable($this->_directory . DIRECTORY_SEPARATOR . $className . '.php'); /** * If $performDispatch is FALSE, only determine if the controller file * can be accessed. */ if (!$performDispatch) { return $dispatchable; } /** * If not dispatchable, get the default controller; if this is already * the default controller, throw an exception */ if (!$dispatchable) { if ($controllerName == $this->getDefaultController()) { throw new Zend_Controller_Dispatcher_Exception('Default controller class not defined'); } $className = $this->formatControllerName($this->getDefaultController()); } /** * Load the controller class file */ Zend::loadClass($className, $this->_directory); /** * Perform reflection on the class and verify it's a controller */ $reflection = new ReflectionClass($className); if (!$reflection->isSubclassOf(new ReflectionClass('Zend_Controller_Action'))) { throw new Zend_Controller_Dispatcher_Exception("Controller \"{$className}\" is not an instance of Zend_Controller_Action"); } /** * Get any instance arguments and instantiate a controller object */ $argv = $this->getParams(); /** * Prepend response object to arguments */ array_unshift($argv, $this->getResponse()); /** * Prepend request object to arguments */ array_unshift($argv, $request); /** * Instantiate controller with arguments */ $controller = $reflection->newInstanceArgs($argv); /** * Determine the action name * * First attempt to retrieve from request; then from request params * using action key; default to default action */ $action = $request->getActionName(); if (empty($action)) { $action = $this->getDefaultAction(); } $action = $this->formatActionName($action); $invokeArgs = array(); /** * If method does not exist, default to __call() */ if (!$reflection->hasMethod($action)) { $invokeArgs = array($action, array()); $action = '__call'; } $method = $reflection->getMethod($action); /** * Dispatch the method call */ $request->setDispatched(true); $controller->preDispatch(); if ($request->isDispatched()) { // preDispatch() didn't change the action, so we can continue $method->invokeArgs($controller, $invokeArgs); $controller->postDispatch(); } // Destroy the page controller instance and reflection objects $controller = null; $reflection = null; $method = null; }
/** * Loads a helper or filter class. * * @param string $type The class type ('helper' or 'filter'). * @param string $name The base name. * @param string The full class name. */ private function _loadClass($type, $name) { // check to see if name => class mapping exists for helper/filter $classLoaded = '_' . $type . 'Loaded'; $classAccess = '_set' . ucfirst($type) . 'Class'; if (isset($this->{$classLoaded}[$name])) { echo "Already loaded {$name} per {$classLoaded}\n", var_export($this->{$classLoaded}, 1), "\n"; return $this->{$classLoaded}[$name]; } // only look for "$Name.php" $file = ucfirst($name) . '.php'; // do LIFO search for helper foreach ($this->_path[$type] as $info) { $dir = $info['dir']; $prefix = $info['prefix']; $class = $prefix . ucfirst($name); if (class_exists($class, false)) { $this->{$classAccess}($name, $class); return $class; } elseif (Zend::isReadable($dir . $file)) { include_once $dir . $file; if (class_exists($class, false)) { $this->{$classAccess}($name, $class); return $class; } } } require_once 'Zend/View/Exception.php'; throw new Zend_View_Exception("{$type} '{$name}' not found in path"); }
/** * Retrieve default controller class * * Determines whether the default controller to use lies within the * requested module, or if the global default should be used. * * By default, will only use the module default unless that controller does * not exist; if this is the case, it falls back to the default controller * in the default module. * * @param Zend_Controller_Request_Abstract $request * @return string */ public function getDefaultControllerClass(Zend_Controller_Request_Abstract $request) { $controller = $this->getDefaultControllerName(); $default = $this->formatControllerName($controller); $request->setControllerName($controller)->setActionName(null); $module = $request->getModuleName(); $controllerDirs = $this->getControllerDirectory(); $this->_curModule = 'default'; $this->_curDirectory = $controllerDirs['default']; if ($this->isValidModule($module)) { $moduleDir = $controllerDirs[$module]; $fileSpec = $moduleDir . DIRECTORY_SEPARATOR . $this->classToFilename($default); if (Zend::isReadable($fileSpec)) { $this->_curModule = $this->formatModuleName($module); $this->_curDirectory = $moduleDir; } } return $default; }