示例#1
0
 /**
  * Ensure the UI module is loaded
  *
  * @param ModuleManagerInterface $modules
  */
 public function init(ModuleManagerInterface $modules)
 {
     $loaded = $modules->getLoadedModules();
     if (isset($loaded['ZF\\Apigility\\Admin\\Ui'])) {
         return;
     }
     $modules->loadModule('ZF\\Apigility\\Admin\\Ui');
 }
示例#2
0
 /**
  * Initialize module.
  *
  * If the admin UI module is not loaded yet, load it.
  *
  * Disable the opcache as well.
  *
  * @param ModuleManagerInterface $modules
  */
 public function init(ModuleManagerInterface $modules)
 {
     $loaded = $modules->getLoadedModules(false);
     if (!isset($loaded['ZF\\Apigility\\Admin\\Ui'])) {
         $modules->loadModule('ZF\\Apigility\\Admin\\Ui');
     }
     $this->disableOpCache();
 }
 /**
  * Build Console usage information by querying currently loaded modules.
  *
  * @param ConsoleAdapter         $console
  * @param string                 $scriptName
  * @param ModuleManagerInterface $moduleManager
  * @return string
  * @throws RuntimeException
  */
 protected function getConsoleUsage(ConsoleAdapter $console, $scriptName, ModuleManagerInterface $moduleManager = null)
 {
     /*
      * Loop through all loaded modules and collect usage info
      */
     $usageInfo = array();
     if ($moduleManager !== null) {
         foreach ($moduleManager->getLoadedModules(false) as $name => $module) {
             // Strict-type on ConsoleUsageProviderInterface, or duck-type
             // on the method it defines
             if (!$module instanceof ConsoleUsageProviderInterface && !method_exists($module, 'getConsoleUsage')) {
                 continue;
                 // this module does not provide usage info
             }
             // We prepend the usage by the module name (printed in red), so that each module is
             // clearly visible by the user
             $moduleName = sprintf("%s\n%s\n%s\n", str_repeat('-', $console->getWidth()), $name, str_repeat('-', $console->getWidth()));
             $moduleName = $console->colorize($moduleName, ColorInterface::RED);
             $usage = $module->getConsoleUsage($console);
             // Normalize what we got from the module or discard
             if (is_array($usage) && !empty($usage)) {
                 array_unshift($usage, $moduleName);
                 $usageInfo[$name] = $usage;
             } elseif (is_string($usage) && $usage != '') {
                 $usageInfo[$name] = array($moduleName, $usage);
             }
         }
     }
     /*
      * Handle an application with no usage information
      */
     if (!count($usageInfo)) {
         // TODO: implement fetching available console routes from router
         return '';
     }
     /*
      * Transform arrays in usage info into columns, otherwise join everything together
      */
     $result = '';
     $table = false;
     $tableCols = 0;
     $tableType = 0;
     foreach ($usageInfo as $moduleName => $usage) {
         if (!is_string($usage) && !is_array($usage)) {
             throw new RuntimeException(sprintf('Cannot understand usage info for module "%s"', $moduleName));
         }
         if (is_string($usage)) {
             // It's a plain string - output as is
             $result .= $usage . "\n";
             continue;
         }
         // It's an array, analyze it
         foreach ($usage as $a => $b) {
             /*
              * 'invocation method' => 'explanation'
              */
             if (is_string($a) && is_string($b)) {
                 if (($tableCols !== 2 || $tableType != 1) && $table !== false) {
                     // render last table
                     $result .= $this->renderTable($table, $tableCols, $console->getWidth());
                     $table = false;
                     // add extra newline for clarity
                     $result .= "\n";
                 }
                 // Colorize the command
                 $a = $console->colorize($scriptName . ' ' . $a, ColorInterface::GREEN);
                 $tableCols = 2;
                 $tableType = 1;
                 $table[] = array($a, $b);
                 continue;
             }
             /*
              * array('--param', '--explanation')
              */
             if (is_array($b)) {
                 if ((count($b) != $tableCols || $tableType != 2) && $table !== false) {
                     // render last table
                     $result .= $this->renderTable($table, $tableCols, $console->getWidth());
                     $table = false;
                     // add extra newline for clarity
                     $result .= "\n";
                 }
                 $tableCols = count($b);
                 $tableType = 2;
                 $table[] = $b;
                 continue;
             }
             /*
              * 'A single line of text'
              */
             if ($table !== false) {
                 // render last table
                 $result .= $this->renderTable($table, $tableCols, $console->getWidth());
                 $table = false;
                 // add extra newline for clarity
                 $result .= "\n";
             }
             $tableType = 0;
             $result .= $b . "\n";
         }
     }
     // Finish last table
     if ($table !== false) {
         $result .= $this->renderTable($table, $tableCols, $console->getWidth());
     }
     return $result;
 }
示例#4
0
 /**
  * Build Console usage information by querying currently loaded modules.
  *
  * @param ConsoleAdapter         $console
  * @param string                 $scriptName
  * @param ModuleManagerInterface $moduleManager
  * @return string
  * @throws RuntimeException
  */
 protected function getConsoleUsage(ConsoleAdapter $console, $scriptName, ModuleManagerInterface $moduleManager = null)
 {
     /**
      * Loop through all loaded modules and collect usage info
      */
     $usageInfo = array();
     if ($moduleManager !== null) {
         foreach ($moduleManager->getLoadedModules(false) as $name => $module) {
             if (!$module instanceof ConsoleUsageProviderInterface) {
                 continue;
                 // this module does not provide usage info
             }
             /* @var $module ConsoleUsageProviderInterface */
             $usage = $module->getConsoleUsage($console);
             // Normalize what we got from the module or discard
             if (is_array($usage)) {
                 $usageInfo[$name] = $usage;
             } elseif (is_string($usage)) {
                 $usageInfo[$name] = array($usage);
             }
         }
     }
     /**
      * Handle an application with no usage information
      */
     if (!count($usageInfo)) {
         // TODO: implement fetching available console routes from router
         return '';
     }
     /**
      * Transform arrays in usage info into columns, otherwise join everything together
      */
     $result = '';
     $table = false;
     $tableCols = 0;
     $tableType = 0;
     foreach ($usageInfo as $moduleName => $usage) {
         if (is_string($usage)) {
             // It's a plain string - output as is
             $result .= $usage . "\n";
         } elseif (is_array($usage)) {
             // It's an array, analyze it
             foreach ($usage as $a => $b) {
                 if (is_string($a) && is_string($b)) {
                     /**
                      *    'ivocation method' => 'explanation'
                      */
                     if (($tableCols !== 2 || $tableType != 1) && $table !== false) {
                         // render last table
                         $result .= $this->renderTable($table, $tableCols, $console->getWidth());
                         $table = false;
                         // add extra newline for clarity
                         $result .= "\n";
                     }
                     $tableCols = 2;
                     $tableType = 1;
                     $table[] = array($scriptName . ' ' . $a, $b);
                 } elseif (is_array($b)) {
                     /**
                      *  array( '--param', '--explanation' )
                      */
                     if ((count($b) != $tableCols || $tableType != 2) && $table !== false) {
                         // render last table
                         $result .= $this->renderTable($table, $tableCols, $console->getWidth());
                         $table = false;
                         // add extra newline for clarity
                         $result .= "\n";
                     }
                     $tableCols = count($b);
                     $tableType = 2;
                     $table[] = $b;
                 } else {
                     /**
                      *    'A single line of text'
                      */
                     if ($table !== false) {
                         // render last table
                         $result .= $this->renderTable($table, $tableCols, $console->getWidth());
                         $table = false;
                         // add extra newline for clarity
                         $result .= "\n";
                     }
                     $tableType = 0;
                     $result .= $b . "\n";
                 }
             }
         } else {
             throw new RuntimeException('Cannot understand usage info for module ' . $moduleName);
         }
     }
     // Finish last table
     if ($table !== false) {
         $result .= $this->renderTable($table, $tableCols, $console->getWidth());
     }
     return $result;
 }