private function getLoadedModules() { if (null === $this->loadedModules) { $this->loadedModules = $this->moduleManager->getLoadedModules(); } return $this->loadedModules; }
public function showAction() { $this->composerInfo->parse(); $loadedModules = $this->moduleManager->getLoadedModules(); $packages = $this->composerInfo->getPackages(); $this->viewModel->setPackages($packages); $this->viewModel->setLoadedModules($loadedModules); $this->viewModel->setTemplate('list-show'); return $this->renderer->render($this->viewModel); }
/** * Process the command * * @return integer */ public function processCommandTask() { $modulePaths = $this->getModulePathsForProject(); // define module list if ($this->params->paramModuleList && count($this->params->paramModuleList) > 0) { // use modules parameter $moduleList = $this->params->paramModuleList; } else { $moduleList = $this->loadModulesForProject($modulePaths); } // init loadable modules $loadableModules = []; // loop through module list foreach ($moduleList as $moduleName) { foreach ($modulePaths as $modulePath) { // check module file $moduleFile = $modulePath . '/' . $moduleName . '/Module.php'; if (file_exists($moduleFile)) { $loadableModules[] = $moduleName; } } } // sort by key sort($loadableModules); // configure event managers $sharedEvents = new SharedEventManager(); $eventManager = new EventManager($sharedEvents); // configure module manager $moduleManager = new ModuleManager($loadableModules, $eventManager); // configure defaukt listeners $defaultListeners = new DefaultListenerAggregate(new ListenerOptions(['module_paths' => $modulePaths])); $defaultListeners->attach($moduleManager->getEventManager()); // load modules $moduleManager->loadModules(); // set loaded modules $this->params->loadedModules = $moduleManager->getLoadedModules(); // check loaded modules if (!empty($this->params->loadedModules)) { return 0; } // output fail message $this->console->writeTaskLine('task_fetch_load_modules_not_found', [$this->console->colorize($this->params->workingPath, Color::GREEN)]); return 1; }
/** * Process the command * * @return integer */ public function processCommandTask() { // define module list if ($this->params->paramModuleList && count($this->params->paramModuleList) > 0) { // use modules parameter $moduleList = $this->params->paramModuleList; } else { // fetch modules form path $moduleList = scandir($this->params->projectModuleDir); // clear unwanted entries unset($moduleList[array_search('.', $moduleList)]); unset($moduleList[array_search('..', $moduleList)]); } // check if Module.php file exists foreach ($moduleList as $moduleKey => $moduleName) { // check module file $moduleFile = $this->params->projectModuleDir . '/' . $moduleName . '/Module.php'; if (!file_exists($moduleFile)) { unset($moduleList[$moduleKey]); } } // sort by key sort($moduleList); // configure event listeners for module manager $sharedEvents = new SharedEventManager(); $defaultListeners = new DefaultListenerAggregate(new ListenerOptions(array('module_paths' => array($this->params->projectModuleDir)))); // configure module manager $moduleManager = new ModuleManager($moduleList); $moduleManager->getEventManager()->setSharedManager($sharedEvents); $moduleManager->getEventManager()->attachAggregate($defaultListeners); $moduleManager->loadModules(); // set loaded modules $this->params->loadedModules = $moduleManager->getLoadedModules(); // check loaded modules if (!empty($this->params->loadedModules)) { return 0; } // output fail message $this->console->writeTaskLine('task_fetch_load_modules_not_found', array($this->console->colorize($this->params->projectPath, Color::GREEN))); return 1; }
/** * Returns list of all API-First-enabled modules * * @return array */ protected function getEnabledModules() { if (is_array($this->modules)) { return $this->modules; } $this->modules = array(); foreach ($this->moduleManager->getLoadedModules() as $moduleName => $module) { if (!$module instanceof ApigilityProviderInterface && !$module instanceof ApigilityModuleInterface) { continue; } if ($module instanceof ApigilityModuleInterface) { trigger_error('ZF\\Apigility\\ApigilityModuleInterface is deprecated, use ZF\\Apigility\\Provider\\ApigilityProviderInterface instead', E_USER_DEPRECATED); } $services = $this->getServicesByModule($moduleName); $versions = $this->getVersionsByModule($moduleName, $module); $entity = new ModuleEntity($moduleName, $services['rest'], $services['rpc']); $entity->exchangeArray(array('versions' => $versions, 'default_version' => $this->getModuleDefaultVersion($module))); $this->modules[$entity->getName()] = $entity; } return $this->modules; }
public function testModuleLoadingBehavior() { $moduleManager = new ModuleManager(array('BarModule')); $moduleManager->getEventManager()->attachAggregate($this->defaultListeners); $modules = $moduleManager->getLoadedModules(); $this->assertSame(0, count($modules)); $modules = $moduleManager->getLoadedModules(true); $this->assertSame(1, count($modules)); $moduleManager->loadModules(); // should not cause any problems $moduleManager->loadModule('BarModule'); // should not cause any problems $modules = $moduleManager->getLoadedModules(true); // BarModule already loaded so nothing happens $this->assertSame(1, count($modules)); }
public function testCanCacheMergedConfig() { $options = new ListenerOptions(array( 'cache_dir' => $this->tmpdir, 'config_cache_enabled' => true, )); $configListener = new ConfigListener($options); $moduleManager = $this->moduleManager; $moduleManager->setModules(array('SomeModule', 'ListenerTestModule')); $moduleManager->events()->attach('loadModule', $configListener); $moduleManager->loadModules(); // This should cache the config $modules = $moduleManager->getLoadedModules(); $this->assertTrue($modules['ListenerTestModule']->getConfigCalled); // Now we check to make sure it uses the config and doesn't hit // the module objects getConfig() method(s) $moduleManager = new ModuleManager(array('SomeModule', 'ListenerTestModule')); $moduleManager->events()->attach('loadModule.resolve', new ModuleResolverListener, 1000); $configListener = new ConfigListener($options); $moduleManager->events()->attach('loadModule', $configListener); $moduleManager->loadModules(); $modules = $moduleManager->getLoadedModules(); $this->assertFalse($modules['ListenerTestModule']->getConfigCalled); }
public function testCanLoadMultipleModulesObjectWithString() { require_once __DIR__ . '/TestAsset/SomeModule/Module.php'; $configListener = $this->defaultListeners->getConfigListener(); $moduleManager = new ModuleManager(array('SomeModule' => new \SomeModule\Module(), 'BarModule'), new EventManager()); $moduleManager->getEventManager()->attachAggregate($this->defaultListeners); $moduleManager->loadModules(); $loadedModules = $moduleManager->getLoadedModules(); $this->assertInstanceOf('SomeModule\\Module', $loadedModules['SomeModule']); $config = $configListener->getMergedConfig(); $this->assertSame($config->some, 'thing'); }
/** * @param ModuleManager $modules */ public function __construct(ModuleManager $modules) { $this->modules = $modules->getLoadedModules(); }
/** * Creates and returns the module manager * * Instantiates the default module listeners, providing them configuration * from the "module_listener_options" key of the ApplicationConfig * service. Also sets the default config glob path. * * Module manager is instantiated and provided with an EventManager, to which * the default listener aggregate is attached. The ModuleEvent is also created * and attached to the module manager. * * @param ServiceLocatorInterface $serviceLocator Service Manager * * @return ModuleManager */ public function createService(ServiceLocatorInterface $serviceLocator) { $moduleCollection = new ModuleCollection(); $modules = $moduleCollection->getModules(); $array = array(); $autoloader = AutoloaderFactory::getRegisteredAutoloader(AutoloaderFactory::STANDARD_AUTOLOADER); foreach ($modules as $module) { $array[] = $module->getName(); $path = GC_APPLICATION_PATH . '/library/Modules/' . $module->getName(); if (file_exists($path) === false) { $path = GC_APPLICATION_PATH . '/extensions/Modules/' . $module->getName(); } $autoloader->registerNamespace($module->getName(), $path); } $autoloader->register(); $application = $serviceLocator->get('Application'); $configuration = $serviceLocator->get('ApplicationConfig'); $configuration['module_listener_options']['module_paths'] = array('./library/Modules', './extensions/Modules'); $listenerOptions = new Listener\ListenerOptions($configuration['module_listener_options']); $defaultListeners = new Listener\DefaultListenerAggregate($listenerOptions); $serviceListener = new Listener\ServiceListener($serviceLocator); $this->prepareServices($serviceListener, $serviceLocator); $moduleManager = new ModuleManager($array, $application->getEventManager()); $moduleManager->getEventManager()->attachAggregate($defaultListeners); $moduleManager->getEventManager()->attachAggregate($serviceListener); $moduleManager->loadModules(); $config = $moduleManager->getEvent()->getConfigListener()->getMergedConfig(false); $this->prepareConfig($serviceLocator, $config); foreach ($moduleManager->getLoadedModules() as $module) { if (method_exists($module, 'onBootstrap')) { $module->onBootstrap($application->getMvcEvent()); } } return $moduleManager; }
/** * 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; }