/** * Set listened target * @param ZDispatcher $a_target */ function setTarget(ZDispatcher &$a_target) { if ($this->m_target) { unset($this->m_target); } $this->m_target =& $a_target; $this->m_target->addProcessor(array(&$this, 'eventProcessor')); $this->m_prefix = Zoombi::config('plugin.action_prefix', ''); $this->m_suffix = Zoombi::config('plugin.action_suffix', 'Action'); }
public function __construct(ZObject &$parent = null, $a_name = null) { parent::__construct($parent, $a_name); $this->m_config = new ZConfig(); $this->m_flag = array(); $this->setLoader(new ZLoader($this)); $this->setLanguage(new ZLanguage($this)); $this->setRegistry(new ZRegistry()); $this->setMode(ZApplication::MODE_NORMAL); $this->setDispatcher(Zoombi::getDispatcher($this)); $this->setPluginManager(new ZPluginManager($this)); }
public function __call($a_name, $a_args) { if (method_exists($this->query, $a_name)) { $r = call_user_func_array(array(&$this->query, $a_name), $a_args); if ($r instanceof Zoombi_SqlBuilder) { return $this; } return $r; } if (Zoombi::getApplication()->isModeDebug()) { $this->triggerError('Bad model method: ' . $a_name); } }
public function &__get($a_property) { switch ($a_property) { default: break; case 'application': return Zoombi::getApplication(); case 'database': return Zoombi::getApplication()->getDatabase(); case 'router': return $this->getModule()->getRouter(); case 'session': return Zoombi::getApplication()->getSession(); case 'request': return ZRequest::getInstance(); case 'response': return ZResponse::getInstance(); case 'module': return $this->getModule(); case 'route': return $this->getModule()->getRoute(); case 'registry': return $this->getModule()->getRegistry(); case 'config': return $this->getModule()->getConfig(); case 'language': return $this->getModule()->getLanguage(); case 'load': return $this->getModule()->getLoader(); case 'acl': return $this->getModule()->getAcl(); } try { return $this->getProperty($a_property); } catch (ZException $e) { if ($e->getCode() == ZException::EXC_NO_PROPERTY) { return Zoombi::$null; } } return Zoombi::$null; }
/** * Log message * @param string $a_message * @param string $a_prefix * @return ZLog */ public function log($a_message, $a_prefix = null) { if ($this->m_path === null) { $this->m_path = Zoombi::config('path.log'); } $filename = 'log.txt'; if ($a_prefix) { $filename = $a_prefix . '-' . $filename; } $filepath = Zoombi::getApplication()->fromApplicationBaseDir($filename); $message = (string) $a_message; $header = ''; if (!file_exists($filepath)) { $head = array(); $head[] = "# Software:\tZoombi PHP Framework"; $head[] = "# File name:\t" . $filename; $head[] = "# Version:\t1.0"; $head[] = "# File creation date:\t" . date("d.m.Y"); $head[] = "# File creation time:\t" . date("H:i:s"); $head[] = "#"; $header = implode("\n", $head) . "\n"; } $fp = @fopen($filepath, 'a'); if (!$fp) { return false; } $msg = array(); $msg[] = "[" . date("d.m.Y") . "]"; $msg[] = "[" . date("H:i:s") . "]"; $msg[] = "[" . $_SERVER['REMOTE_ADDR'] . "]"; $msg[] = '- ' . $message; $message = implode(" ", $msg) . "\n"; flock($fp, LOCK_EX); fwrite($fp, $header . $message); flock($fp, LOCK_UN); fclose($fp); }
/** * * @return ZDispatcher */ public function &getDispatcher() { $d = $this->m_dispatcher ? $this->m_dispatcher : Zoombi::getDispatcher(); return $d; }
/** * Get plugin * @return ZPlugin */ public final function &getPlugin($a_plugin) { switch (gettype($a_plugin)) { case 'int': case 'string': if (isset($this->m_plugins[$a_plugin])) { return $this->m_plugins[$a_plugin]; } break; case 'object': if ($a_plugin instanceof ZPlugin) { foreach ($this->m_plugins as $k => $plg) { if ($plg === $a_plugin) { return $this->m_plugins[$k]; } } } break; } return Zoombi::null(); }
function put($a_key, $a_data, $a_expire = self::EXPIRE_MINUTE) { if (Zoombi_Folder::notexist($this->m_dir)) { if (!mkdir($this->m_dir)) { Zoombi::getApplication()->triggerError('Failed when create directory'); return; } } $key = (string) $a_key; $cache = $this->m_dir . $key . '.cache'; $info = $this->m_dir . $key . '.info'; $time = time(); $age = intval($a_expire); if (file_put_contents($cache, $a_data) === false) { Zoombi::getApplication()->triggerError('Cache file "' . $cache . '", not written'); return; } if (file_put_contents($info, $time . ':' . $age) === false) { Zoombi::getApplication()->triggerError('Cache info file "' . $info . '", not written'); return; } return array($time, $age); }
/** * Load model from file * @param string $a_model * @return ZModel|null */ public static final function &load($a_model) { return Zoombi::model($a_model); }
/** * Render view * @param ZView $a_view * @param array $a_data * @param bool $a_return * @return midex */ private function renderView(ZView &$a_view, $a_return = false) { $view = $a_view->getView(); if (file_exists($view)) { return $a_view->display($a_return); } $oldload = $this->getModule()->getLoader(); if (substr($view, 0, 1) == Zoombi::SS) { $view = substr($view, 1); $this->getModule()->setLoader(Zoombi::getApplication()->getLoader()); } $a_view->setThis($this); if (!file_exists($view)) { try { $a_view->setView($this->getModule()->getLoader()->view($view)); } catch (ZViewException $e) { $this->getModule()->setLoader($oldload); $this->triggerError($e); return; } } $o = $a_view->display($a_return); $this->getModule()->setLoader($oldload); return $o; }
/** * Execute MVC stack * @param $a_contoller * @param $a_view * @param $a_model */ public static final function mvc($a_contoller, $a_view = null, $a_model = null) { $c = Zoombi::controller($a_contoller); if ($c) { if ($a_model) { $c->model = Zoombi::model($a_model); } if ($a_view) { } $c->render($view); } }
/** * Display current view * @param bool $a_return * @return mixed */ public function display($a_return = false) { if (!file_exists($this->m_view) or !is_file($this->m_view)) { $this->m_view = Zoombi::getApplication()->getLoader()->view($this->m_view); } return $this->renderFile($this->m_view, null, $a_return); }
/** * Request controller action * @param string $a_action * @param array $a_params * @return mixed */ public function requestAction($a_action, array $a_params = array()) { if (!$this->hasAction($a_action)) { throw new ZControllerException("Controller '{$this->getName()}' has no action '{$a_action}'", ZControllerException::EXC_ACTION); } $action_name = Zoombi::config('controller.action_prefix') . (string) $a_action . Zoombi::config('controller.action_suffix', 'Action'); if (method_exists($this, $action_name)) { return call_user_func_array(array(&$this, $action_name), $a_params); } else { if (!isset($this->m_actions[$a_action])) { return; } $action =& $this->m_actions[$a_action]; if (!$action) { throw new ZActionException('Action error', ZLoader::EXC_EMPTY); } $action->setController($this); return call_user_func_array(array(&$action, 'run'), $a_params); } }
/** * Route path * @param string $a_path * @return ZModule */ public final function &route($a_path, $a_args = array()) { Zoombi::getApplication()->m_route_deep++; switch (gettype($a_path)) { case 'string': if (substr($a_path, 0, 1) == Zoombi::SS) { Zoombi::getApplication()->route(substr($a_path, 1), $a_args); $this->m_output = Zoombi::getApplication()->outputGet(); $this->m_return = Zoombi::getApplication()->getReturn(); return $this; } break; case 'object': if (!$a_path instanceof ZRoute) { return $this; } break; default: return $this; } $this->setArgs($a_args); try { $path = $this->routePath($this->getRouter()->rewrite($a_path), $route, true); } catch (Exception $e) { //return $this->triggerError('Invalid route path: ' . $a_path, 91); return $this->triggerError($e); } /*if( $path->isInvalid() ) return $this->triggerError('Invalid route path: ' . $path, 91);*/ $this->setRoute($route); $path->controller->requestAction($path->action, $route->getParams()); $this->m_output = $path->controller->getOutput(); $this->m_return = $path->controller->getReturn(); return $this; }
public function getDebug() { $d = new ZDummyController(Zoombi::getApplication(), '__dummy_ctl__'); $o = $d->render(Zoombi::fromFrameworkDir('Views/view_route_debug.php'), array('route' => &$this), true); unset($d); return $o; }
/** * Set application instance * @param ZApplication $a_application */ public static final function setApplication(ZApplication &$a_application) { Zoombi::getInstance()->m_application =& $a_application; return Zoombi::getInstance(); }
/** * Route path * @param string $a_path * @return ZRoute */ public function route($a_path) { $route = (string) $a_path; $rt = new ZRoute($route); if ($rt->module) { $cp = Zoombi::getApplication()->fromApplicationBaseDir(Zoombi::config('path.module', 'module') . Zoombi::DS . $rt->module); if (!file_exists($cp)) { $route = 'default' . Zoombi::US . $rt; } } $this->setRequest($route); $this->setRedirect(clone $this->getRequest()); for ($i = 0; $i < 10; $i++) { $tmp = $this->_parse((string) $this->getRedirect()); if ($tmp) { $this->setRedirect($tmp); } } }
public function __get($a_property) { switch ($a_property) { default: break; case 'application': return Zoombi::getApplication(); case 'database': return Zoombi::getApplication()->getDatabase(); case 'router': return Zoombi::getApplication()->getRouter(); case 'module': if ($this instanceof ZModule) { return $this; } else { return $this->getParent(); } case 'registry': if ($this instanceof ZModule) { return $this->getRegistry(); } else { return $this->getParent()->getRegistry(); } break; case 'language': if ($this instanceof ZModule) { return $this->getLanguage(); } else { return $this->getParent()->getLanguage(); } break; case 'load': if ($this instanceof ZModule) { return $this->getLoader(); } else { return $this->getParent()->getLoader(); } break; case 'name': return $this->getName(); } return $this->getProperty($a_property); }
/** * Class autoloader * @param string $a_class Class name */ public final function autoload($a_class) { $this->emit(new ZEvent($this, 'onAutoload', $a_class)); if ($a_class[0] == 'Z') { $filename = strtolower(substr($a_class, 1, strlen($a_class) - 1)); $base = Zoombi::getFrameworkDir(); foreach (scandir($base) as $dir) { if ($dir == '.' or $dir == '..') { continue; } $path = $base . DIRECTORY_SEPARATOR . $dir; if (is_dir($path) == false) { continue; } $filepath = $path . DIRECTORY_SEPARATOR . $filename . '.php'; if (is_file($filepath)) { require_once $filepath; $this->emit(new ZEvent($this, 'onAutoloadSuccess', $a_class)); return; } } } $this->emit(new ZEvent($this, 'onAutoloadFailed', $a_class)); }
public function __get($a_property) { switch ($a_property) { default: break; case 'application': return Zoombi::getApplication(); case 'database': return Zoombi::getApplication()->getDatabase(); case 'registry': return Zoombi::getApplication()->getRegistry(); case 'language': return Zoombi::getApplication()->getLanguage(); case 'router': return Zoombi::getApplication()->getRouter(); case 'load': return ZLoader::getInstance(); case 'name': return $this->getName(); } return $this->getProperty($a_property); }
/** * Print html formated error message * @param int|arrray $a_code * @param string $a_message * @param int $a_line * @param string $a_file * @param array $a_backtrace * @return null */ public function showError($a_code, $a_message = null, $a_line = null, $a_file = null, $a_backtrace = null) { if (is_array($a_code) && func_num_args() == 1) { return $this->showError(isset($a_code['code']) ? $a_code['code'] : null, isset($a_code['message']) ? $a_code['message'] : null, isset($a_code['line']) ? $a_code['line'] : null, isset($a_code['file']) ? $a_code['file'] : null, isset($a_code['backtrace']) ? $a_code['backtrace'] : null); } else { if ($a_code instanceof Exception) { return $this->showError($a_code->getCode(), $a_code->getMessage(), $a_code->getLine(), $a_code->getFile(), $a_code->getTrace()); } else { if ($a_code instanceof ZError) { return $this->showError($a_code->getCode(), $a_code->getMessage(), 0, 0, $a_code->getTrace()); } } } $c = new ZDummyController($this, 'DummyController'); $c->render(Zoombi::fromFrameworkDir('Views' . Zoombi::DS . 'view_error.php'), array('code' => $a_code, 'message' => $a_message, 'line' => $a_line, 'file' => $a_file, 'backtrace' => $a_backtrace)); unset($c); }
/** * Route path * @param string $a_path * @return Zoombi_Module */ public final function &route($a_path, $a_args = array()) { if (substr($a_path, 0, 1) == Zoombi::SS) { return Zoombi::getApplication()->route(substr($a_path, 1)); } if (substr($a_path, 0, 3) == '../' . Zoombi::SS) { $parent = $this->getModule(); if (!$parent) { $parent = Zoombi::getApplication(); } return $parent->route(substr($a_path, 3)); } $rewrite = $this->getRouter()->rewrite($a_path); $path = new Zoombi_Route($rewrite); $epath = new Zoombi_RoutePath(); if ($path->getSegment(0) == $this->getName()) { $seg = $path->getSegment(1); if ($this->getLoader()->hasController($seg) or $this->getLoader()->hasModule($path->getSegment($seg))) { $path->pop_start(); } } $epath->parents[] = $this; $m = $this; do { $s = $path->getSegment(0); //$m = $m->getLoader()->module($s, false); if ($m->hasModule($s)) { $m = $m->getLoader()->module($s, false); $epath->parents[] = $m; $path->pop_start(); } else { break; } } while ($m); $mod = $epath->module; if (!$mod) { $this->triggerError(new Zoombi_Exception('Application router can\'t find module "' . $path->getSegment(0) . '" -> ' . $rewrite, Zoombi_Exception_Controller::EXC_LOAD)); } $sc = $path->getSegment(0); $path->pop_start(); if (!$mod->hasController($sc)) { return $this->triggerError(new Zoombi_Exception('Application router can\'t find controller "' . $sc . '" in module "' . $mod->getName() . '" -> ' . $rewrite, Zoombi_Exception_Controller::EXC_LOAD)); } $ctl = $mod->getLoader()->controller($sc, false); $epath->controller = $ctl; $sa = $path->getSegment(0); $path->pop_start(); if (!$epath->controller->hasAction($sa)) { return $this->triggerError(new Zoombi_Exception('Application router can\'t find action "' . $sa . '" in controller "' . $sc . '" of module "' . $mod->getName() . '" -> ' . $rewrite, Zoombi_Exception_Controller::EXC_ACTION)); } $epath->action = $sa; Zoombi_Request::getInstance()->setExecPath($epath); Zoombi_Request::getInstance()->setRoutePath($epath); $old_args = $this->getArgs(); $old_route = $this->getRoute(); $old_current = $this->getRouter()->getForward(); $nr = new Zoombi_Route(implode(Zoombi::SS, array_merge($epath->toArray(), $path->getSegments())) . $path->queryString()); $this->setArgs($a_args); $this->setRoute($nr); $this->getRouter()->setForward($nr); $epath->controller->requestAction($epath->action, $path->getSegments()); foreach ($epath->parents as $mod) { if ($mod and $mod instanceof Zoombi_Module) { $mod->setReturn($epath->controller->getReturn()); $mod->setOutput($epath->controller->getOutput()); } } $this->setArgs($old_args); $this->setRoute($old_route); $this->getRouter()->setForward($old_current); return $this; }
public final function &factory($a_name) { $n = 'Zoombi_' . ucfirst($a_name); $c = Zoombi::null(); try { $c = new $n(); } catch (Exception $e) { Zoombi::getApplication()->triggerError($e); } return $c; }
/** * Add ZModel instance * @param string|array $a_model * @return ZController */ function &addModel($a_model) { switch (gettype($a_model)) { case 'string': if ($this->hasModel($a_model)) { break; } $m = null; if (substr($a_model, 0, 1) == Zoombi::SS) { $a_model = substr($a_model, 1); $m = Zoombi::getApplication()->getLoader()->model($a_model); } else { $m = $this->getModule()->getLoader()->model($a_model); } if ($m && $m instanceof ZModel) { $this->setModel($m->getName(), $m); } break; case 'array': foreach ($a_model as $model) { $this->addModel($model); } break; } return $this; }
function put($a_key, $a_data) { $f = new Zoombi_File(); if (Zoombi_Folder::notexist($this->m_dir)) { if (!mkdir($this->m_dir)) { Zoombi::getApplication()->triggerError('Failed when create directory'); return; } } $time = time(); $key = (string) $a_key; $cache = $this->m_dir . $key . '.cache'; $info = $this->m_dir . $key . '.info'; if (file_put_contents($cache, $a_data) === false) { Zoombi::getApplication()->triggerError('Cache file "' . $cache . '", not written'); return; } if (file_put_contents($info, $time) === false) { Zoombi::getApplication()->triggerError('Cache info file "' . $info . '", not written'); return; } }
/** * Load model * @param string|Zoombi_Model $a_model * @return Zoombi_Model */ function loadModel($a_model) { if ($a_model instanceof Zoombi_Model) { return $a_model; } if (!is_string($a_model)) { $this->triggerError("Controller '{$this->getName()}': has wrong model type", Zoombi_Exception_Controller::EXC_MODEL); return; } $name = Zoombi_String::normalize($a_model); if (empty($name)) { $this->triggerError("Controller '{$this->getName()}': has try load empty model", Zoombi_Exception_Controller::EXC_MODEL); return; } if (is_numeric($name)) { $this->triggerError("Controller '{$this->getName()}': has try load model with numeric name", Zoombi_Exception_Controller::EXC_MODEL); return; } $m = null; try { if (substr($name, 0, 1) == Zoombi::SS) { $m = Zoombi::getApplication()->getLoader()->model(substr($name, 1), true); } else { $m = $this->getModule()->getLoader()->model($name, true); } if ($m instanceof Zoombi_Model) { $this->setModel($m->getName(), $m); } else { if ($this->getModule()->isMode(Zoombi_Module::MODE_DEBUG)) { $this->triggerError('Model: ' . $name . 'not loaded'); } } } catch (Zoombi_Exception_Model $e) { if ($this->getModule()->isMode(Zoombi_Module::MODE_DEBUG)) { $this->triggerError($e); } } return $m; }
public static final function route() { return Zoombi::getApplication()->getRouter()->getCurrent(); }
public function _shutdown() { if (Zoombi::ack($this->getConfig()->get('showtrace', false))) { Zoombi_Debug::printTraces(); } if (Zoombi::ack($this->getConfig()->get('showerror', false))) { $this->showErrors(); } }
public function &__get($a_value) { if ($a_value == 'module') { if (count($this->parents) > 0) { return $this->parents[count($this->parents) - 1]; } } return Zoombi::null(); }
private static function &_get_global_routes() { if (!self::$_global_routes) { self::$_global_routes = new Zoombi_HttpRouter(); } return self::$_global_routes; }