Example #1
0
 /**
  * Generate logic conditions and their implementation for container and its delegates.
  *
  * @param string     $inputVariable Input condition parameter variable name
  * @param bool|false $started       Flag if condition branching has been started
  */
 public function generateConditions($inputVariable = '$alias', $started = false)
 {
     // Iterate all container dependencies
     foreach ($this->classesMetadata as $classMetadata) {
         $className = $classMetadata->className;
         // Generate condition statement to define if this class is needed
         $conditionFunc = !$started ? 'defIfCondition' : 'defElseIfCondition';
         // Output condition branch
         $this->generator->{$conditionFunc}($this->buildResolverCondition($inputVariable, $className, $classMetadata->name));
         // Define if this class has service scope
         $isService = in_array($className, $this->scopes[self::SCOPE_SERVICES], true);
         /** @var MethodMetadata[] Gather only valid method for container */
         $classValidMethods = $this->getValidClassMethodsMetadata($classMetadata->methodsMetadata);
         /** @var PropertyMetadata[] Gather only valid property for container */
         $classValidProperties = $this->getValidClassPropertiesMetadata($classMetadata->propertiesMetadata);
         // Define class or service variable
         $staticContainerName = $isService ? '$this->' . self::DI_FUNCTION_SERVICES . '[\'' . $classMetadata->name . '\']' : '$temp';
         if ($isService) {
             // Check if dependency was instantiated
             $this->generator->defIfCondition('!array_key_exists(\'' . $classMetadata->name . '\', $this->' . self::DI_FUNCTION_SERVICES . ')');
         }
         if (count($classValidMethods) || count($classValidProperties)) {
             $this->generator->newLine($staticContainerName . ' = ');
             $this->buildResolvingClassDeclaration($className);
             $this->buildConstructorDependencies($classMetadata->methodsMetadata);
             // Internal scope reflection variable
             $reflectionVariable = '$reflectionClass';
             $this->buildReflectionClass($className, $classValidProperties, $classValidMethods, $reflectionVariable);
             // Process class properties
             foreach ($classValidProperties as $property) {
                 // If such property has the dependency
                 if ($property->dependency) {
                     // Set value via refection
                     $this->buildResolverPropertyDeclaration($property->name, $property->dependency, $staticContainerName, $reflectionVariable, $property->isPublic);
                 }
             }
             /** @var MethodMetadata $methodMetadata */
             foreach ($classValidMethods as $methodName => $methodMetadata) {
                 $this->buildResolverMethodDeclaration($methodMetadata->dependencies, $methodName, $staticContainerName, $reflectionVariable, $methodMetadata->isPublic);
             }
             if ($isService) {
                 $this->generator->endIfCondition();
             }
             $this->generator->newLine()->newLine('return ' . $staticContainerName . ';');
         } else {
             if ($isService) {
                 $this->generator->newLine($staticContainerName . ' = ');
                 $this->buildResolvingClassDeclaration($className);
                 $this->buildConstructorDependencies($classMetadata->methodsMetadata);
                 $this->generator->endIfCondition()->newLine('return ' . $staticContainerName . ';');
             } else {
                 $this->generator->newLine('return ');
                 $this->buildResolvingClassDeclaration($className);
                 $this->buildConstructorDependencies($classMetadata->methodsMetadata);
             }
         }
         // Set flag that condition is started
         $started = true;
     }
 }
Example #2
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 #3
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();
     }
 }