Example #1
0
 /**
  * Handles routing information received from the rewrite engine
  *
  * @param string|null $uri
  */
 public function handle($uri = null)
 {
     $method = $this->prepareMethodName();
     if (empty($uri)) {
         $uri = $this->getRewriteUri();
     }
     $uri = trim($uri, '/');
     // remove empty cells
     $parts = [];
     foreach (explode('/', $uri) as $p) {
         if (trim($p) !== '') {
             $parts[] = $p;
         }
     }
     $this->extractLanguage($parts);
     // check if it is REST or not, then do what is required
     $this->prepareRestRequest($parts);
     // Cleaning route parts & Rebase array keys
     $parts[] = strtolower(self::DEFAULT_ACTION);
     $camelizedParts = $parts;
     $camelizedParts = array_values(array_map('Rad\\Utility\\Inflection::camelize', $camelizedParts));
     $bundle = reset($camelizedParts);
     $bundles = array_intersect([$bundle, 'App'], Bundles::getLoaded());
     foreach ($bundles as $bundleName) {
         // reset manipulation parameters
         $dummyCamelizedParts = $camelizedParts;
         $dummyParts = $parts;
         if ($bundleName === 'App' && $dummyCamelizedParts[0] != 'App') {
             array_unshift($dummyParts, $bundleName);
             array_unshift($dummyCamelizedParts, $bundleName);
         } else {
             // get bundle namespace instead of its name
             array_shift($dummyCamelizedParts);
             array_unshift($dummyCamelizedParts, trim(Bundles::getNamespace($bundleName), '\\'));
         }
         // add "Action" to array as second param
         array_splice($dummyCamelizedParts, 1, 0, 'Action');
         $this->routingPhase = self::ROUTING_PHASE_ACTION;
         /**
          * routingPhase is sequence of three phases, in the following order
          * 1- direct call of action
          * 2- direct call of method
          * 3- direct call of index action
          */
         // Continue searching till you found any matching
         // Or you have at least three elements in array (Bundle, "Action", Action)
         while (count($dummyCamelizedParts) >= 3) {
             $actionNamespace = implode('\\', $dummyCamelizedParts) . 'Action';
             if (class_exists($actionNamespace)) {
                 $this->finalizeRouterArguments($dummyParts, $dummyCamelizedParts, $actionNamespace, $bundleName, $method);
                 break 2;
             }
             array_pop($dummyCamelizedParts);
             // change router for some other default paths
             switch ($this->routingPhase) {
                 case self::ROUTING_PHASE_INDEX:
                     $this->routingPhase = self::ROUTING_PHASE_ACTION;
                     break;
                 case self::ROUTING_PHASE_METHOD:
                     $dummyCamelizedParts[] = self::DEFAULT_ACTION;
                     $this->routingPhase--;
                     break;
                 case self::ROUTING_PHASE_ACTION:
                     $dummyCamelizedParts[] = $method;
                     $this->routingPhase--;
                     break;
             }
         }
     }
 }