Ejemplo n.º 1
0
 /**
  * Module initialization.
  *
  * @param array $params Initialization parameters collection
  * @return bool Initialization result
  */
 public function init(array $params = array())
 {
     //[PHPCOMPRESSOR(remove,start)]
     // Create SamsonPHP routing table from loaded modules
     $rg = new GenericRouteGenerator($this->system->module_stack);
     // Generate web-application routes
     $routes = $rg->generate();
     $routes->add($this->findGenericDefaultAction());
     // Create cache marker
     $this->cacheFile = $routes->hash() . '.php';
     // If we need to refresh cache
     if ($this->cache_refresh($this->cacheFile)) {
         $generator = new Structure($routes, new \samsonphp\generator\Generator());
         // Generate routing logic function
         $routerLogic = $generator->generate();
         // Store router logic in cache
         file_put_contents($this->cacheFile, '<?php ' . "\n" . $routerLogic);
     }
     require $this->cacheFile;
     //[PHPCOMPRESSOR(remove,end)]
     // This should be change to receive path as a parameter on initialization
     $pathParts = explode(Route::DELIMITER, $_SERVER['REQUEST_URI']);
     SamsonLocale::parseURL($pathParts);
     $this->requestURI = implode(Route::DELIMITER, $pathParts);
     // Subscribe to samsonphp\core routing event
     \samsonphp\event\Event::subscribe('core.routing', array($this, 'router'));
     // Continue initialization
     return parent::init($params);
 }
Ejemplo n.º 2
0
 public function testGeneration()
 {
     // Create routes descriptions with identifiers
     $routeArray = array('main-page' => array('GET', '/', '/'), 'inner-page' => array('GET', '/{page}', '/text-page', array('page' => 'text-page')), 'test-two-similar-fixed' => array('GET', '/userlist', '/userlist'), 'test-two-similar-fixed2' => array('GET', '/userlist/friends', '/userlist/friends'), 'test-two-params-at-end' => array('GET', '/userlist/{group}/{action}', '/userlist/123/kill', array('group' => '123', 'action' => 'kill')), 'user-winners-slash' => array('GET', '/user/winners/', '/user/winners/'), 'user-by-id' => array('GET', '/user/{id}', '/user/123'), 'user-home-without-slash' => array('GET', '/user', '/user'), 'user-by-gender-age' => array('GET', '/user/{gender:male|female}/{age}', '/user/male/19d', array('gender' => 'male', 'age' => '19d')), 'user-by-gender-age-filtered' => array('GET', '/user/{gender:male|female}/{age:[0-9]+}', '/user/female/8', array('gender' => 'female', 'age' => '8')), 'user-by-id-form' => array('GET', '/user/{id}/form', '/user/123/form', array('id' => '123')), 'user-by-id-friends' => array('GET', '/user/{id}/friends', '/user/123/friends', array('id' => '123')), 'user-by-id-friends-with-id' => array('GET', '/user/{id}/friends/{groupid}', '/user/123/friends/1', array('id' => '123', 'groupid' => 1)), 'entity-by-id-form' => array('GET', '/{entity}/{id}/form', '/friend/123/form', array('entity' => 'friend', 'id' => '123')), 'entity-by-id-form-test' => array('GET', '/{id}/test/{page:\\d+}', '/123/test/1', array('id' => '123', 'page' => '1')), 'two-params' => array('GET', '/{num}/{page:\\d+}', '/123/23123', array('num' => '123', 'page' => '23123')), 'user-by-id-node' => array('GET', '/user/{id}/n"$ode', '/user/123/n"$ode', array('id' => '123')), 'user-by-id-node-with-id' => array('GET', '/user/{id}/n"$ode/{param}', '/user/123/n"$ode/321', array('id' => '123', 'param' => '321')), 'user-post-by-id' => array('POST', '/user/{id}/save', '/user/123/save', array('id' => '123')), 'user-post-by-id-param' => array('POST', '/user/{id}/save/{name}', '/user/123/save/vitaly', array('id' => '123', 'name' => 'vitaly')), 'user-post-by-id-param2' => array('POST', '/user/{id}/save/{name}/{group}', '/user/123/save/vitaly/students', array('id' => '123', 'name' => 'vitaly', 'group' => 'students')), 'user-post-by-id-param3' => array('POST', '/cms/gift/form/{id}', '/cms/gift/form/123', array('id' => '123')), 'user-post-by-id-param4' => array('POST', '/cms/gift/{id}/{search}', '/cms/gift/123/321', array('id' => '123', 'search' => '321')));
     // Create routes collection
     $routes = new RouteCollection();
     foreach ($routeArray as $identifier => $routeData) {
         $routes->add(new Route($routeData[1], array($this, 'baseCallback'), $identifier, $routeData[0]));
     }
     $generator = new Structure($routes, new \samsonphp\generator\Generator());
     $this->routerLogicFunction = '__router' . rand(0, 9999);
     $routerLogic = $generator->generate($this->routerLogicFunction);
     // Create real file for debugging
     file_put_contents(__DIR__ . '/testLogic.php', '<?php ' . "\n" . $routerLogic);
     require __DIR__ . '/testLogic.php';
     foreach ($routeArray as $identifier => $routeData) {
         if ($identifier === 'test-two-params-at-end') {
             $a = 1;
         }
         $result = $this->routerLogic($routeData[2], $routeData[0]);
         $this->assertEquals($identifier, $result[0]);
         if (isset($routeData[3])) {
             foreach ($routeData[3] as $key => $value) {
                 $this->assertArrayHasKey($key, $result[1]);
                 $this->assertEquals($value, $result[1][$key]);
             }
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Module initialization.
  *
  * @param array $params Initialization parameters collection
  * @return bool Initialization result
  */
 public function init(array $params = array())
 {
     //[PHPCOMPRESSOR(remove,start)]
     $modules = $this->system->getContainer()->getServices('module');
     // Create SamsonPHP routing table from loaded modules
     $rg = new GenericRouteGenerator($modules);
     // Generate web-application routes
     $routes = $rg->generate();
     $routes->add($this->findGenericDefaultAction());
     // Create cache marker
     $this->cacheFile = $routes->hash() . '.php';
     // If we need to refresh cache
     if ($this->cache_refresh($this->cacheFile)) {
         $generator = new Structure($routes, new \samsonphp\generator\Generator());
         // Generate routing logic function
         $routerLogic = $generator->generate();
         // Store router logic in cache
         file_put_contents($this->cacheFile, '<?php ' . "\n" . $routerLogic);
     }
     require $this->cacheFile;
     //[PHPCOMPRESSOR(remove,end)]
     // Set locale resolver mode
     SamsonLocale::$leaveDefaultLocale = $this->browserLocaleRedirect;
     // This should be change to receive path as a parameter on initialization
     $pathParts = array_values(array_filter(explode(Route::DELIMITER, $_SERVER['REQUEST_URI']), function ($v) {
         return $v !== '' && null !== $v;
     }));
     // Parse URL and store locale found bug
     $localeFound = SamsonLocale::parseURL($pathParts, $this->browserLocaleRedirect);
     // Gather URL path parts with removed locale placeholder
     $this->requestURI = implode(Route::DELIMITER, $pathParts);
     // Get localization data
     $current = SamsonLocale::current();
     $default = SamsonLocale::$defaultLocale;
     // Browser agent language detection logic
     if ($this->browserLocaleRedirect && !$localeFound) {
         // Redirect to browser language
         $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
         // Is browser language supported by application
         $langSupport = in_array($lang, SamsonLocale::get(), true);
         /**
          * If browser language header is supported by our web-application and we are already not on that locale
          * and current locale is not default.
          */
         if ($current === $default && $current !== $lang && $langSupport) {
             header('Location: http://' . $_SERVER['HTTP_HOST'] . '/' . $lang . '/' . $this->requestURI);
             exit;
         } elseif (!$langSupport || $lang === $current) {
             SamsonLocale::$leaveDefaultLocale = false;
         }
     }
     // Subscribe to samsonphp\core routing event
     \samsonphp\event\Event::subscribe('core.routing', array($this, 'router'));
     // Continue initialization
     return parent::init($params);
 }
 public function __construct($routes)
 {
     // Create samsonframework\routing routes collection
     $this->collection = new RouteCollection();
     foreach ($routes as $identifier => $routeData) {
         $this->collection->add(new Route($routeData[1], array($this, 'baseCallback'), $identifier, $routeData[0]));
     }
     // Generate routing logic
     $generator = new Structure($this->collection, new Generator());
     $this->routerLogicFunction = '__router' . rand(0, 1000);
     $routerLogic = $generator->generate($this->routerLogicFunction);
     // Create real file for debugging
     file_put_contents(__DIR__ . '/testLogic.php', '<?php ' . "\n" . $routerLogic);
     require __DIR__ . '/testLogic.php';
 }
Ejemplo n.º 5
0
 public function testGeneration()
 {
     $routes = new RouteCollection();
     $routes['main-page'] = new Route('/', array($this, 'baseCallback'));
     $routes['inner-page'] = new Route('/{page}', array($this, 'baseWithPageCallback'));
     // This one would be overridden by next route due to automatic slash addition to the end of the route
     $routes['user-home'] = new Route('/user/', array($this, 'baseCallback'));
     $routes['user-home-without-slash'] = new Route('/user', array($this, 'baseCallback'));
     $routes['user-winners-slash'] = new Route('/user/winners/', array($this, 'baseCallback'));
     $routes['user-by-id'] = new Route('/user/{id}', array($this, 'userWithIDCallback'));
     $routes['user-by-gender-age'] = new Route('/user/{gender:(male|female)}/{age}', array($this, 'userWithIDCallback'));
     $routes['user-by-gender-age-filtered'] = new Route('/user/{gender:(male|female)}/{age:[0-9]+}', array($this, 'userWithIDCallback'));
     $routes['user-by-id-form'] = new Route('/user/{id}/form', array($this, 'userWithIDFormCallback'));
     $routes['user-by-id-friends'] = new Route('/user/{id}/friends', array($this, 'userWithIDFormCallback'));
     $routes['user-by-id-friends-with-id'] = new Route('/user/{id}/friends/{groupid}', array($this, 'userWithIDFormCallback'));
     $routes['entity-by-id-form'] = new Route('/{entity}/{id}/form', array($this, 'entityWithIDFormCallback'));
     $routes['entity-by-id-form-test'] = new Route('/{id}/test/{page:\\d+}', array($this, 'entityWithIDFormCallback'));
     $routes['two-params'] = new Route('/{num}/{page:\\d+}', array($this, 'entityWithIDFormCallback'));
     $routes['user-by-id-node'] = new Route('/user/{id}/n"$ode', array($this, 'userWithIDFormCallback'));
     $routes['user-by-id-node-with-id'] = new Route('/user/{id}/n"$ode/{param}', array($this, 'userWithIDFormCallback'));
     $routes['user-with-empty'] = new Route('/user/{id}/get', array($this, 'userWithIDCallback'));
     $generator = new Structure($routes, new \samsonphp\generator\Generator());
     $routerLogicFunction = '__router' . rand(0, 1000);
     $routerLogic = $generator->generate($routerLogicFunction);
     // Create real file for debugging
     file_put_contents(__DIR__ . '/testLogic.php', '<?php ' . "\n" . $routerLogic);
     require __DIR__ . '/testLogic.php';
     $result = $routerLogicFunction('/', Route::METHOD_GET);
     $this->assertEquals('main-page', $result[0]);
     $result = $routerLogicFunction('/', Route::METHOD_POST);
     $this->assertEquals(null, $result[0]);
     $result = $routerLogicFunction('/user/winners/', Route::METHOD_GET);
     $this->assertEquals('user-winners-slash', $result[0]);
     $result = $routerLogicFunction('/123', Route::METHOD_GET);
     $this->assertEquals('inner-page', $result[0]);
     $this->assertArrayHasKey('page', $result[1]);
     $result = $routerLogicFunction('/123/23123', Route::METHOD_GET);
     $this->assertEquals('two-params', $result[0]);
     $result = $routerLogicFunction('/user/123', Route::METHOD_GET);
     $this->assertEquals('user-by-id', $result[0]);
     $this->assertArrayHasKey('id', $result[1]);
     $result = $routerLogicFunction('/user/123/form', Route::METHOD_GET);
     $this->assertEquals('user-by-id-form', $result[0]);
     $this->assertArrayHasKey('id', $result[1]);
     $result = $routerLogicFunction('/user/123/friends', Route::METHOD_GET);
     $this->assertEquals('user-by-id-friends', $result[0]);
     $this->assertArrayHasKey('id', $result[1]);
     $result = $routerLogicFunction('/user/male/18+', Route::METHOD_GET);
     $this->assertEquals('user-by-gender-age', $result[0]);
     $this->assertArrayHasKey('gender', $result[1]);
     $this->assertArrayHasKey('age', $result[1]);
     $result = $routerLogicFunction('/user/123/friends/321', Route::METHOD_GET);
     $this->assertEquals('user-by-id-friends-with-id', $result[0]);
     $this->assertArrayHasKey('id', $result[1]);
     $this->assertArrayHasKey('groupid', $result[1]);
     $result = $routerLogicFunction('/friend/123/form', Route::METHOD_GET);
     $this->assertEquals('entity-by-id-form', $result[0]);
     $this->assertArrayHasKey('entity', $result[1]);
     $this->assertArrayHasKey('id', $result[1]);
     $result = $routerLogicFunction('/123/test/1', Route::METHOD_GET);
     $this->assertEquals('entity-by-id-form-test', $result[0]);
     $this->assertArrayHasKey('id', $result[1]);
     $this->assertArrayHasKey('page', $result[1]);
     // We consider that all routes are having slash at the end
     $result = $routerLogicFunction('/user/', Route::METHOD_GET);
     $this->assertEquals('user-home-without-slash', $result[0]);
     $result = $routerLogicFunction('/user', Route::METHOD_GET);
     $this->assertEquals('user-home-without-slash', $result[0]);
     $result = $routerLogicFunction('/user/123/n"$ode', Route::METHOD_GET);
     $this->assertEquals('user-by-id-node', $result[0]);
     $result = $routerLogicFunction('/user/123/n"$ode/321', Route::METHOD_GET);
     $this->assertEquals('user-by-id-node-with-id', $result[0]);
     // TODO Fixed this
     //$result = $routerLogicFunction('/user//get', Route::METHOD_GET);
     //$this->assertEquals('user-with-empty', $result[0]);
 }