Exemplo n.º 1
0
 /**
  * Test routes
  */
 public function testMapping2()
 {
     $mapper = new Mapper(new \Alchemy\Component\Yaml\Yaml());
     $mapper->loadFrom(self::$rootDir . "/Tests/fixtures/routes3.yaml");
     $result = $mapper->match('/user_role/assign');
     $expected = array("params" => array('_controller' => 'user_role', '_action' => 'assign'), "mapped" => array("_controller" => 'Sandbox\\Controller\\UserRoleController', "_action" => 'assignAction'));
     $this->assertEquals($expected, $result);
     $result = $mapper->match('/tool/leap_year/1998');
     $expected = array("_controller" => 'SomeOther\\Module\\UserTools', "_action" => 'leapYear', "year" => 1998);
     $this->assertEquals($expected, $result);
     //        $result = $mapper->match('/admin/setup/group/profile');
     //        $expected = array(
     //            "_controller" => 'Sandbox\Utils\groupTest',
     //            "_action" => 'do_update_profile',
     //            "year" => 1998
     //        );
     //        $this->assertEquals($expected, $result);
 }
Exemplo n.º 2
0
 /**
  * Construct application object
  * @param array $conf
  * @throws \Exception
  * @internal param \Alchemy\Contains $config all app configuration
  */
 public function init($conf = array())
 {
     defined('DS') || define('DS', DIRECTORY_SEPARATOR);
     $app = $this;
     $this['logger'] = null;
     $this['config'] = function () use($conf, $app) {
         $config = new Config();
         $config->load($conf);
         if (!$config->exists('phpalchemy.root_dir')) {
             $config->set('phpalchemy.root_dir', realpath(__DIR__ . '/../'));
         }
         if (!empty($app->appDir)) {
             $config->set('app.root_dir', $app->getAppDir());
         }
         return $config;
     };
     // load configuration ini files
     $this->loadAppConfigurationFiles();
     // apply configurated php settings
     $this->applyPhpSettings();
     $this['autoloader'] = function () {
         return new ClassLoader();
     };
     $this['yaml'] = function () {
         return new Yaml();
     };
     $this['annotation'] = function () use($app) {
         return new NotojReader();
         //return new Annotations($app['config']);
     };
     $this['mapper'] = function () use($app) {
         $config = $app['config'];
         $routesDir = $config->get('app.root_dir') . DS . 'config' . DS;
         if (file_exists($routesDir . 'routes.php')) {
             $mapper = (include $routesDir . 'routes.php');
         } elseif (file_exists($routesDir . 'routes.yaml')) {
             $mapper = new Mapper($app['yaml']);
             $mapper->setCacheDir($config->get('app.cache_dir'));
             $mapper->loadFrom($routesDir . 'routes.yaml');
         } else {
             throw new \Exception("Application Error: No routes found for this app.\n" . "You need create & configure 'config/routes.yaml'");
         }
         return $mapper;
     };
     //TODO $this['exception_handler'] = $this->share(function () {
     //     return new ExceptionHandler();
     // });
     $this['dispatcher'] = function () use($app) {
         $dispatcher = new EventDispatcher();
         $dispatcher->addSubscriber($app);
         // subscribing events
         $dispatcher->addSubscriber(new EventListener\ResponseListener($app['config']->get('templating.charset')));
         //TODO $dispatcher->addSubscriber(new LocaleListener($app['locale'], $urlMatcher));
         return $dispatcher;
     };
     $this['resolver'] = function () use($app) {
         return new ControllerResolver($app, $app['logger']);
     };
     $this['ui_reader_factory'] = function () use($app) {
         return new ReaderFactory();
     };
     $this['ui_parser'] = function () use($app) {
         return new Parser();
     };
     /** @var \Alchemy\Component\UI\Engine */
     $this['ui_engine'] = function () use($app) {
         return new Engine($app['ui_reader_factory'], $app['ui_parser']);
     };
     $this['assetsHandler'] = function () use($app) {
         return new WebAssets\Bundle();
     };
     /** @var \Alchemy\Kernel\Kernel */
     $this['kernel'] = function () use($app) {
         return new Kernel($app['dispatcher'], $app['mapper'], $app['resolver'], $app['config'], $app['annotation'], $app['ui_engine'], $app['assetsHandler']);
     };
     // registering the aplication namespace to SPL ClassLoader
     $this['autoloader']->register($this['config']->get('app.name'), $this['config']->get('app.app_root_dir'));
     // registering the aplication Extend folder to SPL ClassLoader (if folder was created)
     if (is_dir($this['config']->get('app.root_dir') . DIRECTORY_SEPARATOR . 'Extend')) {
         $this['autoloader']->register($this['config']->get('app.name') . '/Extend', $this['config']->get('app.root_dir') . DIRECTORY_SEPARATOR . 'Extend');
     }
     $this['exception_handler'] = function () use($app) {
         return new ExceptionHandler();
     };
 }