Пример #1
0
 /**
  * Execute a finite number of stages and end the execution.
  * If the number of stages to be executed are less than zero than the entire
  * pipeline is executed.
  * 
  * @param int $steps number of stages to be passed before stopping the execution
  *
  * @throws \InvalidArgumentException invalid arguments passed
  */
 public function __invoke($steps = -1)
 {
     //check the number of steps
     if (!is_int($steps)) {
         throw new \InvalidArgumentException('The number of steps to execute must be given as an integer number');
     }
     //register the current runtime
     PipelineSupport::RegisterRuntime($this);
     //get the exact number of stages that should be executed
     $stepsNumber = $steps < 0 ? $this->pipeline->countStages() : $steps;
     for ($i = $this->getCompletedStagesCount(); $stepsNumber > 0 && $i < $this->pipeline->countStages() && $this->status != RuntimeStatus::ABORTED; ++$i) {
         try {
             //the pipeline is working right now
             $this->status = RuntimeStatus::WORKING;
             //get the starting time
             $startTime = time();
             $start = microtime(true);
             //register the currently used runtime
             self::$currentExecution =& $this;
             //fetch & execute the pipeline stage
             $reflectedFunction = $this->pipeline->reflectFunctionByIndex($i);
             $executionResult = $reflectedFunction->invokeArgs([&$this->serializableCollection]);
             //unregister the currently used runtime
             self::$currentExecution = null;
             //get the final time
             $time_elapsed_secs = microtime(true) - $start;
             $finalTime = time();
             //generate and store the report
             $this->completionReports[] = ['start_time' => $startTime, 'end_time' => $finalTime, 'elapse_time' => $time_elapsed_secs, 'result' => $executionResult];
             //the pipeline is stopped right now
             $this->status = RuntimeStatus::STOPPED;
         } catch (\Gishiki\Pipeline\PipelineAbortSignal $abortSignal) {
             //the pipeline was aborted
             $this->status = RuntimeStatus::ABORTED;
             //the abort reason must be saved
             $this->abortMessage = $abortSignal->getMessage();
         }
         if (is_null($this->abortMessage) && $this->status != RuntimeStatus::ABORTED && $i == $this->pipeline->countStages() - 1) {
             $this->status = RuntimeStatus::COMPLETED;
         }
         //register the currently active runtime
         PipelineSupport::saveCurrentPupeline();
         --$stepsNumber;
     }
     //runtime ended
     PipelineSupport::UnregisterRuntime();
 }