/** * Test that isMatch() returns a valid response. * Test that getParam() returns a single value, or all values. * Test that url() returns the current URL. */ public function testIsMatchAndParamAndUrl() { $g11n = Registry::factory('Titon\\G11n\\G11n'); $g11n->addLocale(new Locale('en_US')); $g11n->useLocale('en_US'); $url = '/en-us/blog/2012/02/26'; $route = new LocaleRoute('/blog/[year]/[month]/[day]', ['module' => 'blog', 'controller' => 'api', 'action' => 'archives', 'custom' => 'value']); $this->assertTrue($route->isMatch($url)); $this->assertEquals(['ext' => '', 'module' => 'blog', 'controller' => 'api', 'action' => 'archives', 'query' => [], 'args' => [], 'year' => 2012, 'month' => 2, 'day' => 26, 'custom' => 'value', 'locale' => 'en-us'], $route->getParams()); $this->assertEquals('blog', $route->getParam('module')); $this->assertEquals('archives', $route->getParam('action')); $this->assertEquals(2012, $route->getParam('year')); $this->assertEquals(2, $route->getParam('month')); $this->assertEquals(26, $route->getParam('day')); $this->assertEquals('value', $route->getParam('custom')); $this->assertEquals(null, $route->getParam('foobar')); $this->assertEquals('en-us', $route->getParam('locale')); $this->assertEquals($url, $route->url()); // module, controller, action $url = '/en/forum/topic/view/123'; $route = new LocaleRoute('/{module}/{controller}/{action}'); $this->assertTrue($route->isMatch($url)); $this->assertEquals(['ext' => '', 'module' => 'forum', 'controller' => 'topic', 'action' => 'view', 'query' => [], 'args' => [123], 'locale' => 'en'], $route->getParams()); // invalid locale $url = '/foo-bar/forum/topic/view/123'; $route = new LocaleRoute('/{module}/{controller}/{action}'); $this->assertFalse($route->isMatch($url)); // no locale $url = '/forum/topic/view/123'; $route = new LocaleRoute('/{module}/{controller}/{action}'); $this->assertFalse($route->isMatch($url)); }
/** * This method is called before a test is executed. */ protected function setUp() { parent::setUp(); $this->object = Registry::factory('Titon\\G11n\\G11n'); $this->object->addLocale(new Locale('en')); $this->object->useLocale('en'); }
/** * Set the view rendering layer. */ public function initialize() { parent::initialize(); /** @type \Titon\View\View $view */ $view = Registry::get('titon.view'); $view->addPath($this->getModule()->getViewPath()); $this->setView($view); }
/** * This method is called before a test is executed. */ protected function setUp() { parent::setUp(); $this->object = Registry::factory('Titon\\G11n\\G11n'); $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ex-no,ex;q=0.5'; foreach (['ex', 'en'] as $code) { $this->object->addLocale(new Locale($code)); } }
/** * Test that register() and get() lazy load callbacks. */ public function testRegisterAndGet() { Registry::register('base', function () { return new Base(['key' => 'registry']); }); $object = Registry::get('base'); $this->assertInstanceOf('Titon\\Common\\Base', $object); $this->assertEquals('registry', $object->getConfig('key')); $this->assertInstanceOf('Titon\\Common\\Base', Registry::get('base')); try { Registry::get('missingKey'); $this->assertTrue(false); } catch (Exception $e) { $this->assertTrue(true); } }
* later retrieval. Classes may also be factory'd using their * namespace. The Registry only provides an object persistence * layer, anything more, like IoC containers, and service * locators will require third-party libraries. * * We suggest the following dependency libraries. * * - PHP DI - http://mnapoli.fr/PHP-DI/ * - Symfony DI - http://symfony.com/components/DependencyInjection * - Aura DI - http://auraphp.com/packages/Aura.Di/1.1.1/ */ /** * Define the controller to use for error handling. */ Registry::register('titon.controller', function () { return new ErrorController(); }); /** * Define the view to use for error handling, * and as the primary view used by MainController. */ Registry::register('titon.view', function () use($app) { $view = new View(); // Pass route params to the engine making it accessible to the view config $view->setEngine(new TemplateEngine($app->getRouter()->current()->getParams())); // Add helpers that can be accessible in the views $view->addHelper('html', new HtmlHelper())->addHelper('block', new BlockHelper())->addHelper('asset', new AssetHelper(['webroot' => $app->getWebroot()])); // Set variables that are also accessible $view->setVariables(['env' => $app->get('env')->current()->getKey()]); return $view; });
define('MODULES_DIR', APP_DIR . 'modules' . DS); define('RESOURCES_DIR', APP_DIR . 'resources' . DS); define('TEMP_DIR', APP_DIR . 'temp' . DS); define('VIEWS_DIR', APP_DIR . 'views' . DS); define('WEB_DIR', APP_DIR . 'web' . DS); /** * -------------------------------------------------------------- * Composer Autoloader * -------------------------------------------------------------- * * Make use of Composers built-in autoloader for all classes and * vendor libraries used in the application. Store a reference * to composer in case we need to modify the loader. */ $composer = (require_once VENDOR_DIR . 'autoload.php'); Registry::set($composer, 'titon.composer'); /** * -------------------------------------------------------------- * Application Bootstrap * -------------------------------------------------------------- * * Now it's time to start the application. We can do this by * instantiating a new Application object. We'll use a multiton * so that we can access the Application object statically. * Lastly, pass in a Request and Response object as constructor * parameters, which will be passed down to child classes. */ $app = Application::getInstance('default', [new Request(), new Response()]); /** * Bootstrap the application with configuration. * Order here is extremely critical, do not change!
/** * Return a new instance of the current class and store it in registry. * * @return $this */ public static function registry() { return Registry::factory(get_called_class(), func_get_args()); }
/** * Primary method to detect if the object being called can be returned; based on restrictions and instantiation. * If an object is not instantiated, it will create it based off the Closure (if applicable) or the options namespace. * * @uses Titon\Common\Registry * * @param string $class * @return object * @throws \Titon\Common\Exception\MissingObjectException * @throws \Titon\Common\Exception\UnsupportedInterfaceException */ public function &getObject($class) { if (isset($this->_attached[$class])) { return $this->_attached[$class]; } else { if (!isset($this->_classes[$class])) { throw new MissingObjectException(sprintf('No object attachment could be found for %s', $class)); } } // Gather options $options = $this->_classes[$class]; // Lazy-load the object if (isset($this->__objectMap[$class])) { $object = $this->__objectMap[$class](); $this->_classes[$class]['class'] = get_class($object); // Create manually } else { if ($options['register']) { $object = Registry::factory($options['class']); } else { $object = new $options['class'](); } } if ($options['interface'] && !$object instanceof $options['interface']) { throw new UnsupportedInterfaceException(sprintf('%s does not implement the %s interface', get_class($object), $options['interface'])); } $this->_attached[$class] =& $object; return $this->_attached[$class]; }
/** * Set an object to use throughout the application. * * @param string $key * @param object $object * @return $this */ public function set($key, $object) { $this->_components[$key] = Registry::set($object); return $this; }
/** * Reset cache. */ protected function tearDown() { parent::tearDown(); Registry::flush(); }
function factory($key) { return Registry::factory($key); }