Example #1
0
 /**
  * Generator inner branches loop handler.
  *
  * @param Branch $branch Branch for looping its inner branches
  * @param bool $conditionStarted Return variable showing if inner branching has been started
  * @param string $pathValue Current routing logic $path variable name
  */
 protected function generatorBranchesLoop(Branch $branch, &$conditionStarted, $pathValue)
 {
     // First stage - open condition
     // If we have started condition branching but this branch has parameters
     if ($conditionStarted && $branch->isParametrized()) {
         $this->generator->endIfCondition()->defIfCondition($branch->toLogicConditionCode($pathValue));
     } elseif (!$conditionStarted) {
         // This is first inner branch
         // Start new condition
         $this->generator->defIfCondition($branch->toLogicConditionCode($pathValue));
         // Set flag that condition has started
         $conditionStarted = true;
     } else {
         // This is regular branching
         $this->generator->defElseIfCondition($branch->toLogicConditionCode($pathValue));
     }
     // Second stage receive parameters
     if ($branch->isParametrized()) {
         // Store parameter value received from condition
         $this->generator->newLine($branch->storeMatchedParameter());
     }
     /**
      * Optimization to remove nested string operations - we create temporary $path variables
      */
     $pathVariable = '$path' . mt_rand(0, 99999);
     // Do not output new $path variable creation if this is logic end
     if (count($branch->branches)) {
         $this->generator->newLine($pathVariable . ' = ' . $branch->removeMatchedPathCode($pathValue) . ';');
     }
     // We should subtract part of $path var to remove this parameter
     // Go deeper in recursion
     $this->innerGenerate2($branch, $pathVariable, false);
 }
Example #2
0
 /**
  * Generate routing conditions logic.
  *
  * @param Branch $parent Current branch in resursion
  * @param string $pathValue Current $path value in routing logic
  * @param bool $conditionStarted Flag that condition started
  */
 protected function innerGenerate2(Branch $parent, $pathValue = '$path', $conditionStarted = false)
 {
     // Iterate inner branches
     foreach ($parent->branches as $branch) {
         // First stage - open condition
         // If we have started condition branching but this branch has parameters
         if ($conditionStarted && $branch->isParametrized()) {
             $this->generator->endIfCondition()->defIfCondition($branch->toLogicConditionCode($pathValue));
         } elseif (!$conditionStarted) {
             // This is first inner branch
             // Start new condition
             $this->generator->defIfCondition($branch->toLogicConditionCode($pathValue));
             // Set flag that condition has started
             $conditionStarted = true;
         } else {
             // This is regular branching
             $this->generator->defElseIfCondition($branch->toLogicConditionCode($pathValue));
         }
         // Second stage receive parameters
         if ($branch->isParametrized()) {
             // Store parameter value received from condition
             $this->generator->newLine($branch->storeMatchedParameter());
         }
         /**
          * Optimization to remove nested string operations - we create temporary $path variables
          */
         $pathVariable = '$path' . rand(0, 99999);
         // Do not output new $path variable creation if this is logic end
         if (sizeof($branch->branches)) {
             $this->generator->newLine($pathVariable . ' = ' . $branch->removeMatchedPathCode($pathValue) . ';');
         }
         // We should subtract part of $path var to remove this parameter
         // Go deeper in recursion
         $this->innerGenerate2($branch, $pathVariable, false);
     }
     // Return route if branch has it
     if ($parent->hasRoute()) {
         // If we had other inner branch for this parent branch - we need to add else
         if (sizeof($parent->branches)) {
             $this->generator->defElseCondition();
         }
         $this->generator->newLine($parent->returnRouteCode());
     }
     // Close first condition
     if ($conditionStarted) {
         $this->generator->endIfCondition();
     }
 }
Example #3
0
 /**
  * Compare two branch and define which has greater priority.
  *
  * @param Branch $aBranch
  * @param Branch $bBranch
  * @return int Comparison result
  */
 protected function sorter(Branch $aBranch, Branch $bBranch)
 {
     // Define if some of the branches if parametrized
     if (!$aBranch->isParametrized() && $bBranch->isParametrized()) {
         return -1;
     } elseif ($aBranch->isParametrized() && !$bBranch->isParametrized()) {
         return 1;
     } elseif ($aBranch->isParametrized() && $bBranch->isParametrized()) {
         if (isset($aBranch->node->regexp[1]) && !isset($bBranch->node->regexp[1])) {
             return -1;
         } elseif (!isset($aBranch->node->regexp[1]) && isset($bBranch->node->regexp[1])) {
             return 1;
         }
     }
     // Return branch size comparison, longer branches should go on top
     return $aBranch->size < $bBranch->size ? 1 : -1;
 }
Example #4
0
 /**
  * Compare two branch and define which has greater priority.
  *
  * @param Branch $aBranch
  * @param Branch $bBranch
  * @return int Comparison result
  */
 protected function sorter(Branch $aBranch, Branch $bBranch)
 {
     /**
      * Rule #1
      * Parametrized branch always has lower priority then textual branch.
      */
     if (!$aBranch->isParametrized() && $bBranch->isParametrized()) {
         return -1;
     } elseif ($aBranch->isParametrized() && !$bBranch->isParametrized()) {
         return 1;
     } elseif ($aBranch->isParametrized() && $bBranch->isParametrized()) {
         /**
          * Rule #2
          * If both branches are parametrized then branch with set regexp filter has higher priority.
          */
         $aRegExp = $aBranch->nodeRegExp();
         $bRegExp = $bBranch->nodeRegExp();
         if (isset($aRegExp[1]) && !isset($bRegExp[1])) {
             return -1;
         } elseif (!isset($aRegExp[1]) && isset($bRegExp[1])) {
             return 1;
         } else {
             /**
              * Rule #4
              * If both branches are parametrized and they have two length-equal string patterns then not
              * "deeper" branch has priority.
              */
             return $aBranch->size < $bBranch->size ? 1 : -1;
         }
         /** TODO: We need to invent a way to compare regexp filter to define who is "wider" */
     } else {
         // Both branches are not parametrized
         /**
          * Rule #4
          * If both are not parametrized and one is final - we choose it as check for it more
          * optimal in logic condition branches.
          */
         if (sizeof($aBranch->branches) === 0) {
             return -1;
         } elseif (sizeof($bBranch->branches === 0)) {
             return 1;
         }
         /**
          * Rule #3
          * If both branches are not parametrized then branch with shorter pattern string has higher priority.
          */
         if (strlen($aBranch->nodeValue()) > strlen($bBranch->nodeValue())) {
             return 1;
         } elseif (strlen($aBranch->nodeValue()) < strlen($bBranch->nodeValue())) {
             return -1;
         } else {
             /**
              * Rule #4
              * If both branches are not parametrized and they have two length-equal string patterns then not
              * "deeper" branch has priority.
              */
             return $aBranch->size > $bBranch->size ? 1 : -1;
         }
     }
 }