/** * Adds a module to the application for testing. * * The application should be already configured in order to be able to add * a test module to it. * * The module will be configured and ran. * * @param AbstractModule $module Module to be added. * @param array $config [optional] Module config you may want to pass? */ public function addTestModule(AbstractModule $module, array $config = array()) { // apply the passed config through the application config $this->getConfig()->apply(array($module->getName() => $config)); $this->setPhase(Framework::PHASE_BOOTSTRAP); $this->addModule($module); $container = $this->getContainer(); $module->setContainer($container); // configure the module $this->setPhase(Framework::PHASE_CONFIGURE); $framework = new Framework(); $framework->configureModule($module, $this, $container->getParameter('env'), $container->getParameter('debug')); $container->get('router')->readModuleRoutes($module); // run the module $this->setPhase(Framework::PHASE_RUN); $module->run(); }
/** * Runs the module. */ public function run() { parent::run(); $container = $this->container; // after normal generation has finished, also trigger docs generation $container->get('event_manager')->subscribe(DidGenerate::getName(), function () use($container) { $container->get('console')->call('docs'); }); }
/** * Configures the module. */ public function configure() { parent::configure(); $config = $this->getConfig(); $this->container->setParameter('blog.data_file', $config->get('data_file')); $this->container->setParameter('blog.articles_dir', $config->get('articles_dir')); $this->container->setParameter('blog.template', $config->get('template')); $this->container->setParameter('blog.target_dir', $config->get('target_dir')); }
public function configure() { parent::configure(); $config = $this->getConfig(); $container = $this->getContainer(); // copy some params from config to container $this->container->setParameter('form.simple_handler.http_code_key', $config->get('ajax.json.http_code_key')); $this->container->setParameter('filestorage.simple.parent_dir', $config->get('filestorage.simple.web') ? '%web_dir%' : '%root_dir%'); $this->container->setParameter('filestorage.simple.dir', $config->get('filestorage.simple.dir')); // conditional configurations if ($config->get('router.use_request')) { $this->configureRouterRequestConfigurator(); } if ($config->get('ajax.enable')) { $this->configureAjax(); } if ($config->get('databridge.enable')) { $this->configureDataBridge(); } if ($config->get('mailer.enable')) { $this->configureMailer(); } }
/** * Reads routes for the given module and registers them automatically. * * @param AbstractModule $module Module object. */ public function readModuleRoutes(AbstractModule $module) { $moduleName = $module->getName(); $routesDir = rtrim($module->getModuleDir(), '/') . '/Controllers'; if (!is_dir($routesDir)) { return; } $moduleNamespace = $module->getNamespace() . NS . 'Controllers' . NS; $routesDirLength = strlen($routesDir . '/'); $files = FilesystemUtils::glob($routesDir . '/{,**/}*.php', GLOB_BRACE); foreach ($files as $file) { // remove full path to the routes dir, so we're left only with relative path $file = substr($file, $routesDirLength); // also remove extension $file = substr($file, 0, -4); // build full class name $subNamespace = str_replace('/', NS, $file); $class = $moduleNamespace . $subNamespace; // class_exists autoloads the file if (!class_exists($class)) { continue; } // check if this class can be instantiated, if not, skip it $reflection = new ReflectionClass($class); if (!$reflection->isInstantiable()) { continue; } $this->addRoute($moduleName . ':' . $subNamespace, $class, $moduleName, $module->getUrlPrefix() . $class::_getUrl()); } }
/** * This method is called on the module during the run phase. If you need you can include any logic * here. */ public function run() { parent::run(); }
/** * Searches the given module for any commands that could be automatically registered. * * @param AbstractModule $module */ protected function readModuleCommands(AbstractModule $module) { $name = $module->getName(); $commandsDir = $module->getModuleDir() . '/Commands'; if (!is_dir($commandsDir)) { return; } $moduleNamespace = $module->getNamespace() . NS . 'Commands' . NS; $console = $this; // register a closure so we can recursively scan the routes directory $scan = function ($dir, $namespace, $self) use($name, $moduleNamespace, $module, $console) { $namespace = $namespace ? trim($namespace, NS) . NS : ''; $files = scandir($dir); foreach ($files as $file) { // ignore . and .. if (in_array($file, array('.', '..'))) { continue; } // if directory then go recursively if (is_dir($dir . DS . $file)) { $self($dir . DS . $file, $namespace . $file, $self); continue; } $file = explode('.', $file); $rawClass = $file[0]; $class = $moduleNamespace . $namespace . $rawClass; // class_exists autoloads a file if (class_exists($class)) { $name = $class::getName(); $name = trim($name); if (empty($name)) { throw new InvalidCommandException('Command "' . $class . '" has to have a name. Please set the static property $name.'); } $name = ($module->getCommandNamespace() ? $module->getCommandNamespace() . ':' : '') . $name; $console->addCommand($name, $class); } } }; // scan the module $scan($commandsDir, '', $scan); }
/** * Adds a module to the application. * * @param AbstractModule $module Module to be added. * * @throws NotUniqueException When module name is not unique and its already been registered. * @throws \RuntimeException When application has already been bootstrapped and its too late. */ public function addModule(AbstractModule $module) { if ($this->phase > Framework::PHASE_BOOTSTRAP) { throw new \RuntimeException('Application has been already bootstrapped and it is too late to add new modules.'); } $name = $module->getName(); if ($this->hasModule($name)) { throw new NotUniqueException('Module with name "' . $name . '" is already registered in the application.'); } $this->modules[$name] = $module; }
/** * Configures a module in context of the given application. * * @param AbstractModule $module Module to be configured. * @param AbstractApplication $application Application for which this module should be configured. * @param string $env [optional] Environment in which the application is running. Default: `dev`. * @param boolean $debug [optional] Debug mode status for the application. Default: `true`. */ public function configureModule(AbstractModule $module, AbstractApplication $application, $env = 'dev', $debug = true) { $container = $application->getContainer(); $config = $application->getConfig(); // define config for the module $container->register('config.' . $module->getName(), array('extends' => 'config_module.abstract')); // add method calls to load appropriate config files to the definition $configDefinition = $container->getDefinition('config.' . $module->getName()); $configDir = rtrim($module->getConfigDir(), DS); $configFiles = array_merge(FilesystemUtils::glob($configDir . '/config.{yml,yaml,php}', GLOB_BRACE), FilesystemUtils::glob($configDir . '/config.' . $env . '.{yml,yaml,php}', GLOB_BRACE)); foreach ($configFiles as $file) { $configDefinition->addMethodCall('loadFromFile', array($file)); } // add method call to apply the config from the application config $configDefinition->addMethodCall('apply', array($config->getNamespace($module->getName()))); // let it configure itself $module->configure(); }