/**
  * Inspect the result, and cast it to a ViewModel if a string is detected
  *
  * @param MvcEvent $e
  * @return void
  */
 public function createViewModelFromString(MvcEvent $e)
 {
     $result = $e->getResult();
     if (!is_string($result)) {
         return;
     }
     // create Console model
     $model = new ConsoleModel();
     // store the result in a model variable
     $model->setVariable(ConsoleModel::RESULT, $result);
     $e->setResult($model);
 }
 public function runAction()
 {
     $sm = $this->getServiceLocator();
     /* @var $console \Zend\Console\Adapter\AdapterInterface */
     /* @var $config array */
     /* @var $mm \Zend\ModuleManager\ModuleManager */
     $console = $sm->get('console');
     $config = $sm->get('Configuration');
     $mm = $sm->get('ModuleManager');
     $verbose = $this->params()->fromRoute('verbose', false);
     $debug = $this->params()->fromRoute('debug', false);
     $quiet = !$verbose && !$debug && $this->params()->fromRoute('quiet', false);
     $breakOnFailure = $this->params()->fromRoute('break', false);
     $checkGroupName = $this->params()->fromRoute('filter', false);
     // Get basic diag configuration
     $config = isset($config['diagnostics']) ? $config['diagnostics'] : array();
     // Collect diag tests from modules
     $modules = $mm->getLoadedModules(false);
     foreach ($modules as $moduleName => $module) {
         if (is_callable(array($module, 'getDiagnostics'))) {
             $checks = $module->getDiagnostics();
             if (is_array($checks)) {
                 $config[$moduleName] = $checks;
             }
             // Exit the loop early if we found check definitions for
             // the only check group that we want to run.
             if ($checkGroupName && $moduleName == $checkGroupName) {
                 break;
             }
         }
     }
     // Filter array if a check group name has been provided
     if ($checkGroupName) {
         $config = array_intersect_ukey($config, array($checkGroupName => 1), 'strcasecmp');
         if (empty($config)) {
             $m = new ConsoleModel();
             $m->setResult($console->colorize(sprintf("Unable to find a group of diagnostic checks called \"%s\". Try to use module name (i.e. \"%s\").\n", $checkGroupName, 'Application'), ColorInterface::YELLOW));
             $m->setErrorLevel(1);
             return $m;
         }
     }
     // Check if there are any diagnostic checks defined
     if (empty($config)) {
         $m = new ConsoleModel();
         $m->setResult($console->colorize("There are no diagnostic checks currently enabled for this application - please add one or more " . "entries into config \"diagnostics\" array or add getDiagnostics() method to your Module class. " . "\n\nMore info: https://github.com/zendframework/ZFTool/blob/master/docs/" . "DIAGNOSTICS.md#adding-checks-to-your-module\n", ColorInterface::YELLOW));
         $m->setErrorLevel(1);
         return $m;
     }
     // Analyze check definitions and construct check instances
     $checkCollection = array();
     foreach ($config as $checkGroupName => $checks) {
         foreach ($checks as $checkLabel => $check) {
             // Do not use numeric labels.
             if (!$checkLabel || is_numeric($checkLabel)) {
                 $checkLabel = false;
             }
             // Handle a callable.
             if (is_callable($check)) {
                 $check = new Callback($check);
                 if ($checkLabel) {
                     $check->setLabel($checkGroupName . ': ' . $checkLabel);
                 }
                 $checkCollection[] = $check;
                 continue;
             }
             // Handle check object instance.
             if (is_object($check)) {
                 if (!$check instanceof CheckInterface) {
                     throw new RuntimeException('Cannot use object of class "' . get_class($check) . '" as check. ' . 'Expected instance of ZendDiagnostics\\Check\\CheckInterface');
                 }
                 // Use duck-typing for determining if the check allows for setting custom label
                 if ($checkLabel && is_callable(array($check, 'setLabel'))) {
                     $check->setLabel($checkGroupName . ': ' . $checkLabel);
                 }
                 $checkCollection[] = $check;
                 continue;
             }
             // Handle an array containing callback or identifier with optional parameters.
             if (is_array($check)) {
                 if (!count($check)) {
                     throw new RuntimeException('Cannot use an empty array() as check definition in "' . $checkGroupName . '"');
                 }
                 // extract check identifier and store the remainder of array as parameters
                 $testName = array_shift($check);
                 $params = $check;
             } elseif (is_scalar($check)) {
                 $testName = $check;
                 $params = array();
             } else {
                 throw new RuntimeException('Cannot understand diagnostic check definition "' . gettype($check) . '" in "' . $checkGroupName . '"');
             }
             // Try to expand check identifier using Service Locator
             if (is_string($testName) && $sm->has($testName)) {
                 $check = $sm->get($testName);
                 // Try to use the ZendDiagnostics namespace
             } elseif (is_string($testName) && class_exists('ZendDiagnostics\\Check\\' . $testName)) {
                 $class = new \ReflectionClass('ZendDiagnostics\\Check\\' . $testName);
                 $check = $class->newInstanceArgs($params);
                 // Try to use the ZFTool namespace
             } elseif (is_string($testName) && class_exists('ZFTool\\Diagnostics\\Check\\' . $testName)) {
                 $class = new \ReflectionClass('ZFTool\\Diagnostics\\Check\\' . $testName);
                 $check = $class->newInstanceArgs($params);
                 // Check if provided with a callable inside an array
             } elseif (is_callable($testName)) {
                 $check = new Callback($testName, $params);
                 if ($checkLabel) {
                     $check->setLabel($checkGroupName . ': ' . $checkLabel);
                 }
                 $checkCollection[] = $check;
                 continue;
                 // Try to expand check using class name
             } elseif (is_string($testName) && class_exists($testName)) {
                 $class = new \ReflectionClass($testName);
                 $check = $class->newInstanceArgs($params);
             } else {
                 throw new RuntimeException('Cannot find check class or service with the name of "' . $testName . '" (' . $checkGroupName . ')');
             }
             if (!$check instanceof CheckInterface) {
                 // not a real check
                 throw new RuntimeException('The check object of class ' . get_class($check) . ' does not implement ' . 'ZendDiagnostics\\Check\\CheckInterface');
             }
             // Use duck-typing for determining if the check allows for setting custom label
             if ($checkLabel && is_callable(array($check, 'setLabel'))) {
                 $check->setLabel($checkGroupName . ': ' . $checkLabel);
             }
             $checkCollection[] = $check;
         }
     }
     // Configure check runner
     $runner = new Runner();
     $runner->addChecks($checkCollection);
     $runner->getConfig()->setBreakOnFailure($breakOnFailure);
     if (!$quiet && $this->getRequest() instanceof ConsoleRequest) {
         if ($verbose || $debug) {
             $runner->addReporter(new VerboseConsole($console, $debug));
         } else {
             $runner->addReporter(new BasicConsole($console));
         }
     }
     // Run tests
     $results = $runner->run();
     // Return result
     if ($this->getRequest() instanceof ConsoleRequest) {
         // Return appropriate error code in console
         $model = new ConsoleModel();
         $model->setVariable('results', $results);
         if ($results->getFailureCount() > 0) {
             $model->setErrorLevel(1);
         } else {
             $model->setErrorLevel(0);
         }
     } else {
         // Display results as a web page
         $model = new ViewModel();
         $model->setVariable('results', $results);
     }
     return $model;
 }
 /**
  * Run diagnostics
  *
  * @return ConsoleModel|ViewModel
  * @throws \ZFTool\Diagnostics\Exception\RuntimeException
  */
 public function runAction()
 {
     // check for help mode
     if ($this->requestOptions->getFlagHelp()) {
         return $this->runHelp();
     }
     // get needed options to shorten code
     $flagVerbose = $this->requestOptions->getFlagVerbose();
     $flagDebug = $this->requestOptions->getFlagDebug();
     $flagQuiet = $this->requestOptions->getFlagQuiet();
     $flagBreak = $this->requestOptions->getFlagBreak();
     $testGroupName = $this->requestOptions->getTestGroupName();
     // output header
     if (!$flagQuiet) {
         $this->consoleHeader('Starting diagnostics for Zend Framework 2 project');
     }
     // start output
     if (!$flagQuiet) {
         $this->console->writeLine('       => Get basic diag configuration');
     }
     // Get basic diag configuration
     $config = isset($this->configuration['diagnostics']) ? $this->configuration['diagnostics'] : array();
     // start output
     if (!$flagQuiet) {
         $this->console->writeLine('       => Collect diag tests from modules ');
     }
     // Collect diag tests from modules
     $modules = $this->moduleManager->getLoadedModules(false);
     foreach ($modules as $moduleName => $module) {
         if (is_callable(array($module, 'getDiagnostics'))) {
             $tests = $module->getDiagnostics();
             if (is_array($tests)) {
                 $config[$moduleName] = $tests;
             }
             // Exit the loop early if we found test definitions for
             // the only test group that we want to run.
             if ($testGroupName && $moduleName == $testGroupName) {
                 break;
             }
         }
     }
     // Filter array if a test group name has been provided
     if ($testGroupName) {
         $config = array_intersect_key($config, array($testGroupName => 1));
     }
     // start output
     if (!$flagQuiet) {
         $this->console->writeLine('       => Analyze test definitions and construct test instances');
     }
     // Analyze test definitions and construct test instances
     $testCollection = array();
     foreach ($config as $testGroupName => $tests) {
         foreach ($tests as $testLabel => $test) {
             // Do not use numeric labels.
             if (!$testLabel || is_numeric($testLabel)) {
                 $testLabel = false;
             }
             // Handle a callable.
             if (is_callable($test)) {
                 $test = new Callback($test);
                 if ($testLabel) {
                     $test->setLabel($testGroupName . ': ' . $testLabel);
                 }
                 $testCollection[] = $test;
                 continue;
             }
             // Handle test object instance.
             if (is_object($test)) {
                 if (!$test instanceof TestInterface) {
                     throw new RuntimeException('Cannot use object of class "' . get_class($test) . '" as test. ' . 'Expected instance of ZFTool\\Diagnostics\\Test\\TestInterface');
                 }
                 if ($testLabel) {
                     $test->setLabel($testGroupName . ': ' . $testLabel);
                 }
                 $testCollection[] = $test;
                 continue;
             }
             // Handle an array containing callback or identifier with optional parameters.
             if (is_array($test)) {
                 if (!count($test)) {
                     throw new RuntimeException('Cannot use an empty array() as test definition in "' . $testGroupName . '"');
                 }
                 // extract test identifier and store the remainder of array as parameters
                 $testName = array_shift($test);
                 $params = $test;
             } elseif (is_scalar($test)) {
                 $testName = $test;
                 $params = array();
             } else {
                 throw new RuntimeException('Cannot understand diagnostic test definition "' . gettype($test) . '" in "' . $testGroupName . '"');
             }
             // Try to expand test identifier using Service Locator
             if (is_string($testName) && $this->getServiceLocator()->has($testName)) {
                 $test = $this->getServiceLocator()->get($testName);
                 // Try to use the built-in test class
             } elseif (is_string($testName) && class_exists('ZFTool\\Diagnostics\\Test\\' . $testName)) {
                 $class = new \ReflectionClass('ZFTool\\Diagnostics\\Test\\' . $testName);
                 $test = $class->newInstanceArgs($params);
                 // Check if provided with a callable inside the array
             } elseif (is_callable($testName)) {
                 $test = new Callback($testName, $params);
                 if ($testLabel) {
                     $test->setLabel($testGroupName . ': ' . $testLabel);
                 }
                 $testCollection[] = $test;
                 continue;
                 // Try to expand test using class name
             } elseif (is_string($testName) && class_exists($testName)) {
                 $class = new \ReflectionClass($testName);
                 $test = $class->newInstanceArgs($params);
             } else {
                 throw new RuntimeException('Cannot find test class or service with the name of "' . $testName . '" (' . $testGroupName . ')');
             }
             if (!$test instanceof TestInterface) {
                 // not a real test
                 throw new RuntimeException('The test object of class ' . get_class($test) . ' does not implement ' . 'ZFTool\\Diagnostics\\Test\\TestInterface');
             }
             // Apply label
             if ($testLabel) {
                 $test->setLabel($testGroupName . ': ' . $testLabel);
             }
             $testCollection[] = $test;
         }
     }
     if (!$flagQuiet) {
         $this->console->writeLine();
         $this->console->write(' Diag ', Color::NORMAL, Color::CYAN);
         $this->console->write(' ');
     }
     // Configure test runner
     $runner = new Runner();
     $runner->addTests($testCollection);
     $runner->getConfig()->setBreakOnFailure($flagBreak);
     if (!$flagQuiet && $this->getRequest() instanceof ConsoleRequest) {
         if ($flagVerbose || $flagDebug) {
             $runner->addReporter(new VerboseConsole($this->console, $flagDebug));
         } else {
             $runner->addReporter(new BasicConsole($this->console));
         }
     }
     // Run tests
     $results = $runner->run();
     // Return result
     if ($this->getRequest() instanceof ConsoleRequest) {
         // Return appropriate error code in console
         $model = new ConsoleModel();
         $model->setVariable('results', $results);
         if ($results->getFailureCount() > 0) {
             $model->setErrorLevel(1);
         } else {
             $model->setErrorLevel(0);
         }
     } else {
         // Display results as a web page
         $model = new ViewModel();
         $model->setVariable('results', $results);
     }
     return $model;
 }