/** * Return lang by name * * @param string $name * @param mixed $default Default value that will be returned if lang is not found * @return string */ function lang($name, $default = null) { if (is_null($default)) { $default = "Missing lang: {$name}"; } return $this->langs->get($name, $default); }
/** * Getting required arguments with given parameters * * @param \ReflectionParameter[] $required Arguments * @param array $params Parameters * *@return array */ private function _resolveArgs($required, $params = array()) { $args = array(); foreach ($required as $param) { $name = $param->getName(); $type = $param->getClass(); if (isset($type)) { $type = $type->getName(); } if (isset($params[$name])) { $args[] = $params[$name]; } elseif (is_string($type) && isset($params[$type])) { $args[] = $params[$type]; } else { $content = $this->_container->get($name); if (isset($content)) { $args[] = $content; } elseif (is_string($type)) { $args[] = $this->_container->get($type); } else { $args[] = null; } } } return $args; }
public function warmUp($cacheDir) { // we need the directory no matter the hydrator cache generation strategy. $hydratorCacheDir = $this->container->getParameter('doctrine_mongodb.odm.hydrator_dir'); if (!file_exists($hydratorCacheDir)) { if (false === @mkdir($hydratorCacheDir, 0777, true)) { throw new \RuntimeException(sprintf('Unable to create the Doctrine Hydrator directory (%s)', dirname($hydratorCacheDir))); } } else { if (!is_writable($hydratorCacheDir)) { throw new \RuntimeException(sprintf('Doctrine Hydrator directory (%s) is not writable for the current system user.', $hydratorCacheDir)); } } // if hydrators are autogenerated we don't need to generate them in the cache warmer. if ($this->container->getParameter('doctrine_mongodb.odm.auto_generate_hydrator_classes') === true) { return; } /* @var $registry \Doctrine\Common\Persistence\ManagerRegistry */ $registry = $this->container->get('doctrine_mongodb'); foreach ($registry->getManagers() as $dm) { /* @var $dm \Doctrine\ODM\MongoDB\DocumentManager */ $classes = $dm->getMetadataFactory()->getAllMetadata(); $dm->getHydratorFactory()->generateHydratorClasses($classes); } }
/** * @param string $componentKey * * @return object|null */ public function get($componentKey) { if (array_key_exists($componentKey, $this->components)) { return $this->components[$componentKey]; } if ($this->parent !== null) { return $this->parent->get($componentKey); } return null; }
/** * * @param Container $container * @param boolean $value * @return string */ public static function showStatusOrder($container, $value) { if ($value == 1) { $html = "<span class=\"label label-sm label-info\">" . $container->get('translator')->trans('Completed') . "</span>"; } elseif ($value == 0) { $html = "<span class=\"label label-sm label-danger\">" . $container->get('translator')->trans('Closed') . "</span>"; } else { $html = "<span class=\"label label-sm label-warning\">" . $container->get('translator')->trans('Pending') . "</span>"; } return $html; }
/** * @todo Implement testGetServices(). */ public function testGetDefsAndServices() { $this->container->set('foo', new \StdClass()); $this->container->set('bar', new \StdClass()); $this->container->set('baz', new \StdClass()); $expect = ['foo', 'bar', 'baz']; $actual = $this->container->getDefs(); $this->assertSame($expect, $actual); $service = $this->container->get('bar'); $expect = ['bar']; $actual = $this->container->getServices(); $this->assertSame($expect, $actual); }
/** * Constructor * * @param Container $container The container object * @param Output $output The console output */ public function __construct($container, $output) { $this->container = $container; $this->converter = new CaseConverter(); $this->registry = $container->get('doctrine'); $this->filesystem = $container->get('filesystem'); $this->output = $output; $parameters = $container->getParameterBag()->all(); foreach ($parameters as $k => $v) { $pos = strpos($k, '.'); $alias = substr($k, 0, $pos); $parameter = substr($k, $pos + 1); $this->parameters[$alias][$parameter] = $v; } }
/** * Register Slim's default services. * * @param Container $container A DI container implementing ArrayAccess and container-interop. * * @return void */ public function register($container) { $container['errorHandler'] = function ($container) { return new Error($container->get('settings')['displayErrorDetails']); }; $container['phpErrorHandler'] = function ($container) { return new PhpError($container->get('settings')['displayErrorDetails']); }; $container['notFoundHandler'] = function ($container) { return new NotFound($container->get('settings')['displayErrorDetails']); }; $container['notAllowedHandler'] = function ($container) { return new NotAllowed($container->get('settings')['displayErrorDetails']); }; }
/** * * Tests that a service is of the expected class. * * @param string $name The service name. * * @param string $class The expected class. * * @return null * * @dataProvider provideGet * */ public function testGet($name, $class) { if (!$name) { $this->markTestSkipped('No service name passed for testGet().'); } $this->assertInstanceOf($class, $this->di->get($name)); }
/** * Gets a service. * * @param string $id The service identifier * @param int $invalidBehavior The behavior when the service does not exist * * @return object The associated service * * @throws \InvalidArgumentException if the service is not defined * @throws \LogicException if the service has a circular reference to itself * * @see Reference */ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) { try { return parent::get($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE); } catch (\InvalidArgumentException $e) { if (isset($this->loading[$id])) { throw new \LogicException(sprintf('The service "%s" has a circular reference to itself.', $id)); } if (!$this->hasDefinition($id) && isset($this->aliases[$id])) { return $this->get($this->aliases[$id]); } try { $definition = $this->getDefinition($id); } catch (\InvalidArgumentException $e) { if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) { return null; } throw $e; } $this->loading[$id] = true; $service = $this->createService($definition, $id); unset($this->loading[$id]); return $service; } }
/** * Fetch admin IDs */ public static function get_admin_ids() { if (!Container::get('cache')->isCached('admin_ids')) { Container::get('cache')->store('admin_ids', \FeatherBB\Model\Cache::get_admin_ids()); } return Container::get('cache')->retrieve('admin_ids'); }
public function validate_search_word($word, $idx) { static $stopwords; // If the word is a keyword we don't want to index it, but we do want to be allowed to search it if ($this->is_keyword($word)) { return !$idx; } if (!isset($stopwords)) { if (!Container::get('cache')->isCached('stopwords')) { Container::get('cache')->store('stopwords', \FeatherBB\Model\Cache::get_config(), '+1 week'); } $stopwords = Container::get('cache')->retrieve('stopwords'); } // If it is a stopword it isn't valid if (in_array($word, $stopwords)) { return false; } // If the word is CJK we don't want to index it, but we do want to be allowed to search it if ($this->is_cjk($word)) { return !$idx; } // Exclude % and * when checking whether current word is valid $word = str_replace(array('%', '*'), '', $word); // Check the word is within the min/max length $num_chars = Utils::strlen($word); return $num_chars >= ForumEnv::get('FEATHER_SEARCH_MIN_WORD') && $num_chars <= ForumEnv::get('FEATHER_SEARCH_MAX_WORD'); }
public function attachEvents() { Container::get('hooks')->bind('model.plugins.getLatest', function ($plugins) { // $plugins = $plugins->where_not_equal('id', 1); return $plugins; }); }
public function print_users($username, $start_from, $sort_by, $sort_dir, $show_group) { $userlist_data = array(); $username = Container::get('hooks')->fire('model.userlist.print_users_start', $username, $start_from, $sort_by, $sort_dir, $show_group); // Retrieve a list of user IDs, LIMIT is (really) expensive so we only fetch the IDs here then later fetch the remaining data $result = DB::for_table('users')->select('u.id')->table_alias('u')->where_gt('u.id', 1)->where_not_equal('u.group_id', ForumEnv::get('FEATHER_UNVERIFIED')); if ($username != '') { $result = $result->where_like('u.username', str_replace('*', '%', $username)); } if ($show_group > -1) { $result = $result->where('u.group_id', $show_group); } $result = $result->order_by($sort_by, $sort_dir)->order_by_asc('u.id')->limit(50)->offset($start_from); $result = Container::get('hooks')->fireDB('model.userlist.print_users_query', $result); $result = $result->find_many(); if ($result) { $user_ids = array(); foreach ($result as $cur_user_id) { $user_ids[] = $cur_user_id['id']; } // Grab the users $result['select'] = array('u.id', 'u.username', 'u.title', 'u.num_posts', 'u.registered', 'g.g_id', 'g.g_user_title'); $result = DB::for_table('users')->table_alias('u')->select_many($result['select'])->left_outer_join('groups', array('g.g_id', '=', 'u.group_id'), 'g')->where_in('u.id', $user_ids)->order_by($sort_by, $sort_dir)->order_by_asc('u.id'); $result = Container::get('hooks')->fireDB('model.userlist.print_users_grab_query', $result); $result = $result->find_many(); foreach ($result as $user_data) { $userlist_data[] = $user_data; } } $userlist_data = Container::get('hooks')->fire('model.userlist.print_users', $userlist_data); return $userlist_data; }
public function testSetParameter() { $container = new Container(); $testValue = 'testValue'; $container->set('value', $testValue); $this->assertEquals($testValue, $container->get('value')); }
public static function set_new_password($pass, $key, $user_id) { $query['update'] = array('activate_string' => hash($pass), 'activate_key' => $key, 'last_email_sent' => time()); $query = DB::for_table('users')->where('id', $user_id)->find_one()->set($query['update']); $query = Container::get('hooks')->fireDB('password_forgotten_mail_query', $query); return $query->save(); }
public function display($req, $res, $args) { Container::get('hooks')->fire('controller.admin.maintenance.display'); $action = ''; if (Input::post('action')) { $action = Input::post('action'); } elseif (Input::query('action')) { $action = Input::query('action'); } if ($action == 'rebuild') { $this->model->rebuild(); View::setPageInfo(array('page_title' => array(Utils::escape(ForumSettings::get('o_board_title')), __('Rebuilding search index')), 'query_str' => $this->model->get_query_str()))->addTemplate('admin/maintenance/rebuild.php')->display(); } if ($action == 'prune') { $prune_from = Utils::trim(Input::post('prune_from')); $prune_sticky = intval(Input::post('prune_sticky')); AdminUtils::generateAdminMenu('maintenance'); if (Input::post('prune_comply')) { $this->model->prune_comply($prune_from, $prune_sticky); } View::setPageInfo(array('title' => array(Utils::escape(ForumSettings::get('o_board_title')), __('Admin'), __('Prune')), 'active_page' => 'admin', 'admin_console' => true, 'prune_sticky' => $prune_sticky, 'prune_from' => $prune_from, 'prune' => $this->model->get_info_prune($prune_sticky, $prune_from)))->addTemplate('admin/maintenance/prune.php')->display(); } AdminUtils::generateAdminMenu('maintenance'); View::setPageInfo(array('title' => array(Utils::escape(ForumSettings::get('o_board_title')), __('Admin'), __('Maintenance')), 'active_page' => 'admin', 'admin_console' => true, 'first_id' => $this->model->get_first_id(), 'categories' => $this->model->get_categories()))->addTemplate('admin/maintenance/admin_maintenance.php')->display(); }
public function display($req, $res, $args) { Container::get('hooks')->fire('controller.userlist.display'); if (User::get()->g_view_users == '0') { throw new Error(__('No permission'), 403); } // Determine if we are allowed to view post counts $show_post_count = ForumSettings::get('o_show_post_count') == '1' || User::get()->is_admmod ? true : false; $username = Input::query('username') && User::get()->g_search_users == '1' ? Utils::trim(Input::query('username')) : ''; $show_group = Input::query('show_group') ? intval(Input::query('show_group')) : -1; $sort_by = Input::query('sort_by') && (in_array(Input::query('sort_by'), array('username', 'registered')) || Input::query('sort_by') == 'num_posts' && $show_post_count) ? Input::query('sort_by') : 'username'; $sort_dir = Input::query('sort_dir') && Input::query('sort_dir') == 'DESC' ? 'DESC' : 'ASC'; $num_users = $this->model->fetch_user_count($username, $show_group); // Determine the user offset (based on $page) $num_pages = ceil($num_users / 50); $p = !Input::query('p') || $page <= 1 || $page > $num_pages ? 1 : intval($page); $start_from = 50 * ($p - 1); if (User::get()->g_search_users == '1') { $focus_element = array('userlist', 'username'); } else { $focus_element = array(); } // Generate paging links $paging_links = '<span class="pages-label">' . __('Pages') . ' </span>' . Url::paginate_old($num_pages, $p, '?username='******'&show_group=' . $show_group . '&sort_by=' . $sort_by . '&sort_dir=' . $sort_dir); View::setPageInfo(array('title' => array(Utils::escape(ForumSettings::get('o_board_title')), __('User list')), 'active_page' => 'userlist', 'page_number' => $p, 'paging_links' => $paging_links, 'focus_element' => $focus_element, 'is_indexed' => true, 'username' => $username, 'show_group' => $show_group, 'sort_by' => $sort_by, 'sort_dir' => $sort_dir, 'show_post_count' => $show_post_count, 'dropdown_menu' => $this->model->generate_dropdown_menu($show_group), 'userlist_data' => $this->model->print_users($username, $start_from, $sort_by, $sort_dir, $show_group)))->addTemplate('userlist.php')->display(); }
/** * @param type GetResponseForControllerResultEvent $event * @return type */ public function onKernelView(GetResponseForControllerResultEvent $event) { $request = $event->getRequest(); $parameters = $event->getControllerResult(); $templating = $this->container->get('templating'); if (null === $parameters) { if (!($vars = $request->attributes->get('_template_vars'))) { if (!($vars = $request->attributes->get('_template_default_vars'))) { return; } } $parameters = array(); foreach ($vars as $var) { $parameters[$var] = $request->attributes->get($var); } } if (!is_array($parameters)) { return $parameters; } if (!($template = $request->attributes->get('_template'))) { return $parameters; } $user = $this->container->get('security.context')->getToken()->getUser(); $parameters = array_merge(array('themes' => $request->attributes->get('_parent_template')), $parameters); if (!$request->attributes->get('_template_streamable')) { $event->setResponse($templating->renderResponse($template, $parameters)); } else { $callback = function () use($templating, $template, $parameters) { return $templating->stream($template, $parameters); }; $event->setResponse(new StreamedResponse($callback)); } }
/** * Initialize the router object. * * @param Container $container */ public function __construct($container) { $this->container = $container; $this->request = new Request($container->get('request')); $this->response = new Response($this->request->getCallType(), $this->request->isUpload()); $this->defaultAccess = $container->getParameter('direct.api.default_access'); $this->session = $this->container->get('session')->get($container->getParameter('direct.api.session_attribute')); }
public function get_words() { $word_data = array(); $word_data = DB::for_table('censoring')->order_by_asc('id'); $word_data = Container::get('hooks')->fireDB('model.admin.censoring.update_censoring_word_query', $word_data); $word_data = $word_data->find_array(); return $word_data; }
public static function get_info() { $data = array('exec_time' => Utils::get_microtime() - Container::get('start')); $data['nb_queries'] = isset(DB::get_query_log()[0]) ? count(DB::get_query_log()[0]) : 'N/A'; $data['mem_usage'] = function_exists('memory_get_usage') ? Utils::file_size(memory_get_usage()) : 'N/A'; $data['mem_peak_usage'] = function_exists('memory_get_peak_usage') ? Utils::file_size(memory_get_peak_usage()) : 'N/A'; return $data; }
public function get($key) { $key = $this->findKey($key); if ($key === false) { return null; } return parent::get($key); }
public function testContainerToCallback() { $c = new Container(); $c->share('service', function (Container $container) { return $container; }); $this->assertInstanceOf(Container::class, $c->get('service')); }
public function markread() { Container::get('hooks')->fire('controller.index.markread'); Auth::set_last_visit(User::get()->id, User::get()->logged); // Reset tracked topics Track::set_tracked_topics(null); return Router::redirect(Router::pathFor('home'), __('Mark read redirect')); }
/** * Return lang by name * * @param string $name * @param mixed $default Default value that will be returned if lang is not found * @return string */ function lang($name, $default = null) { if (is_null($default)) { $default = "<span style=\"font-weight: bolder; color: red;\">Missing lang: {$name}</span>"; } // if return $this->langs->get($name, $default); }
public function edit($req, $res, $args) { Container::get('hooks')->fire('controller.admin.bans.edit'); if (Input::post('add_edit_ban')) { return $this->model->insert_ban(); } AdminUtils::generateAdminMenu('bans'); View::setPageInfo(array('admin_console' => true, 'focus_element' => array('bans2', 'ban_user'), 'title' => array(Utils::escape(ForumSettings::get('o_board_title')), __('Admin'), __('Bans')), 'ban' => $this->model->edit_ban_info($args['id'])))->addTemplate('admin/bans/add_ban.php')->display(); }
public static function get_tracked_topics() { $cookie_raw = Container::get('cookie')->get(ForumSettings::get('cookie_name') . '_track'); if (isset($cookie_raw)) { $cookie_data = json_decode($cookie_raw, true); return $cookie_data; } return array('topics' => array(), 'forums' => array()); }
/** * @covers ::__construct * @covers ::initContainer * @covers ::register * @covers ::has * @covers ::get * @covers Nora\Core\DI\Exception\InstanceNotFound::__construct * @expectedException Nora\Core\DI\Exception\InstanceNotFound */ public function testCreate() { $c = new Container(); $c->register(['hOge' => function () { return new \StdClass(); }]); $this->assertTrue($c->has('Hoge')); $this->assertInstanceOf('StdClass', $c->get('hogE')); $c->get('Hoge')->cnt = 0; $c->get('hoGe')->cnt++; $c->on('di.container.pre_get', function ($e) { if (strtolower($e->name) === 'phpunit') { $e->instance = $this; } }); $this->assertEquals($this, $c->get('phpUnit')); $c->get('InvalidName'); }
public function display($req, $res, $args) { Container::get('hooks')->fire('controller.admin.options.display'); if (Request::isPost()) { return $this->model->update_options(); } AdminUtils::generateAdminMenu('options'); View::setPageInfo(array('title' => array(Utils::escape(ForumSettings::get('o_board_title')), __('Admin'), __('Options')), 'active_page' => 'admin', 'admin_console' => true, 'languages' => $this->model->get_langs(), 'styles' => $this->model->get_styles(), 'times' => $this->model->get_times()))->addTemplate('admin/options.php')->display(); }