/** * render|update|create model model ... * @param InputInterface $input * @param OutputInterface $output * @return int|null|void * @throws \tf_exception */ protected function execute(InputInterface $input, OutputInterface $output) { //require('../_tests/loader.php'); $core = $this->core(); $this->prepare_db($input->getOption('database')); $action = $input->getArgument('action'); if (empty($action)) { throw new \tf_exception("empty action\n"); } $ids = $input->getArgument('models'); if (empty($ids)) { throw new \tf_exception("empty ID\n"); } if ($ids[0] == '*') { $ids = Helpers\ModelEnumerator::find(); } /* * $action = @$_SERVER['argv'][2] ? : 'render'; if (in_array('--', $_SERVER['argv'])) { } */ $output->writeln('action: ' . $action); /** @var collection_generator */ $generator = \abs_collection::get_generator(); foreach ($ids as $mid) { $_mid = explode('.', trim($mid)); if (count($_mid) == 1) { array_unshift($_mid, 'core'); $container = core::selfie(); } else { $container = core::module($_mid[0]); } if (!$container) { $output->writeln('skip ' . $mid); continue; } $method = 'get_' . $_mid[1] . '_handle'; // allow model with params: get_model_handle | model('model') $obj = \functions::is_callable(array($container, $method)) ? $container->{$method}() : $container->model($_mid[1]); $generator->append_object($obj); } $output->writeln($generator->render_table_structure(), OutputInterface::OUTPUT_RAW); if ($action == 'update') { $generator->update_table_structure(); } if ($action == 'create') { $generator->update_table_structure(true); } }
function render_virtual($method, $type) { if ($method instanceof Closure) { return $method($this, $type); } $method = 'virtual_' . $method; if (!functions::is_callable(array($this, $method))) { throw new collection_exception('Virtual method not callable : ' . get_class($this) . '.' . $method); } return call_user_func(array($this, $method), $type); }
/** * Run method on all children * * $users->invoke('dump'); * $users->invoke(function($item){$item->dump();}); * * @param mixed $method * @param mixed $params * @return abs_collection self */ function invoke($method, $params = null) { $_method = $method; if ($this->count()) { foreach ($this as $item) { if (is_string($_method)) { $method = array($item, $_method); if (functions::is_callable($method)) { call_user_func_array($method, $params); } else { if ($this->_invoke_exception) { throw new Collection_Exception(__METHOD__ . ' not callable: ' . get_class($method[0]) . ':' . $method[1]); } } } else { // closure pass selfie first param if ($_method instanceof Closure) { $params = array($item, $params); call_user_func_array($method, $params); // $method(...$params) } else { if ($this->_invoke_exception) { throw new Collection_Exception(__METHOD__ . ' closure not callable'); } } } } } return $this; }
/** * Entry point for app * * @return bool false - not found * @throws router_exception */ function dispatch($url = false) { if ($this->cfg('options.log_requests')) { $this->logger->log('request', $url); } $request = $url ? $url : urldecode($_SERVER['REQUEST_URI']); self::event('dispatch_before', array('url' => &$request)); $skip_site_check = false; if (preg_match('@users/(login|logout)/\\z@', $request)) { $this->set_cfg_var('in_login', true); $skip_site_check = true; } // returns HTTP_HOST if main_domain not set $domain = $_SERVER['HTTP_HOST']; //$this->get_main_domain(); $subdomain = $_SERVER['HTTP_HOST'] != $domain ? substr($_SERVER['HTTP_HOST'], 0, -1 + -1 * strlen($domain)) : false; if ('www' === $subdomain) { $subdomain = false; } $this->set_cfg_var('subdomain', $subdomain); $cname = false; // check for cname if ($domain != substr($_SERVER['HTTP_HOST'], -1 * ($sl = strlen($domain)), $sl)) { $cname = preg_replace('/^.*\\.([^\\.]+\\.[\\w]+)$/', '$1', $_SERVER['HTTP_HOST']); $this->set_cfg_var('cname', $cname); } // @todo fix tails or errors if (false !== ($qpos = strpos($request, '?'))) { $request = substr($request, 0, $qpos); } $dispatcher = self::$modules->get_main(); if (!$dispatcher) { throw new module_exception('Main module not set'); } $this->append_base_url($this->router->get_protocol()); $this->append_base_url($this->cfg('main_domain'), true); if ($dispatcher && functions::is_callable(array($dispatcher, 'predispatch'))) { $dispatcher->predispatch($domain, $request); } /** * /domain/module+alias/ * check path against * - modules list * - alias list */ $request_array = explode('/', $request); // remove empty parts array_splice($request_array, 0, 1); if (empty($request_array[count($request_array) - 1])) { array_splice($request_array, -1, 1); } $request_count = count($request_array); // Output filters if ($request_count >= 2 && $request_array[$request_count - 2] == 'outfilter') { $filter = $request_array[$request_count - 1]; if (!$this->set_output_filter($filter)) { throw new router_exception('Invalid filter - ' . $filter, router_exception::NOT_FOUND); } array_splice($request_array, -2); // @todo slice $request } $router_module = null; if ($this->in_index()) { // override frontpage layout if ($template = $this->cfg('site.frontpage.template')) { $this->renderer->set_page_template($template); } } $root = isset($request_array[0]) ? array_shift($request_array) : false; if (!empty($root) && !preg_match('/^[\\w_\\.\\-\\%а-я[:space:]]+$/ui', $root)) { throw new router_exception('Root route error'); } if (empty($root)) { // use default module as router self::$modules->get_main()->set_is_router(); } else { // core module calls if ('core' == $root) { $this->set_is_router(true); $router_module = $this; } else { if (self::$modules->is_registered($root)) { self::$modules->set_router($root); } else { /** @var core_module */ $pmod = self::$modules->get_by_alias($root); if ($pmod) { $pmod->set_is_router(true); } else { // default, inject root element array_unshift($request_array, $root); self::$modules->get_main()->set_is_router(); // no module found, route to default router (with main flag) } } } } // nothing routed? $router_module = $router_module ? $router_module : self::$modules->get_router(); if (!$router_module) { throw new router_exception('No router module found'); } $router = $router_module->get_router(); self::event('route_before', $router); if ($router) { self::dprint('[ROUTER] using ' . $router_module->get_name()); $result = $router->route($request_array); if (!$result) { throw new router_exception('Requested page doesnt exists'); } /*deprecated*/ /* self::lib('renderer')->set_main_template( $router_module->get_controller()->get_template() ); */ } else { throw new router_exception('No router available for ' . ($router_module ? $router_module->get_name() : ''), 0); } }
/** * Call section * @param $section * @param $route * @param $params */ function run_section($section, $route, $params) { $method = "section_{$section}"; if (functions::is_callable(array($this, $method))) { $this->{$method}($route, $params); } }