diff --git a/bin/development-mode b/bin/development-mode new file mode 100755 index 0000000000..87213c2373 --- /dev/null +++ b/bin/development-mode @@ -0,0 +1,220 @@ +#!/usr/bin/env php +response(1, 'No arguments provided.' . PHP_EOL . PHP_EOL . $this->help); + } + + // Requested help + if (in_array($argv[1], ['-h', '--help'], true)) { + $this->response(0, $this->help); + } + + if (!in_array($argv[1], ['disable', 'enable'], true)) { + $this->response(1, 'Unrecognized argument.' . PHP_EOL . PHP_EOL . $this->help); + } + + $action = $argv[1]; + $this->$action(); + } + + protected function response($code, $text = null) + { + if ($text) { + if ($code != 0) { + fwrite(STDERR, $text . PHP_EOL); + } else { + echo $text . PHP_EOL; + } + } + + exit($code); + } + + /** + * Disable development mode + */ + protected function disable() + { + if (!file_exists('config/development.config.php')) { + // nothing to do + $this->response(0, 'Development mode was already disabled.'); + } + + if (file_exists('config/autoload/development.local.php')) { + // optional application config override + unlink('config/autoload/development.local.php'); + } + + unlink('config/development.config.php'); + + $this->removeConfigCacheFile(); + + $this->response(0, 'Development mode is now disabled.'); + } + + /** + * Enable development mode + */ + protected function enable() + { + if (file_exists('config/development.config.php')) { + // nothing to do + $this->response(0, 'Already in development mode!'); + } + + if (!file_exists('config/development.config.php.dist')) { + $this->response(1, 'MISSING "config/development.config.php.dist". Could not switch to development mode!'); + } + + copy('config/development.config.php.dist', 'config/development.config.php'); + + if (file_exists('config/autoload/development.local.php.dist')) { + // optional application config override + copy('config/autoload/development.local.php.dist', 'config/autoload/development.local.php'); + } + + $this->removeConfigCacheFile(); + + $this->response(0, 'You are now in development mode.'); + } + + /** + * Removes the application configuration cache file, if present. + */ + protected function removeConfigCacheFile() + { + $configCacheFile = $this->getConfigCacheFile(); + + if ($configCacheFile && file_exists($configCacheFile)) { + unlink($configCacheFile); + } + } + + /** + * Retrieve the config cache file, if any. + * + * @return false|string + */ + protected function getConfigCacheFile() + { + if (!$configCacheDir = $this->getConfigCacheDir()) { + return false; + } + + $path = sprintf('%s/%s.', $configCacheDir, self::CONFIG_CACHE_BASE); + + if ($configCacheKey = $this->getConfigCacheKey()) { + $path .= $configCacheKey . '.'; + } + + return $path . 'php'; + } + + /** + * Return the configured configuration cache directory, if any. + * + * @return null|string + */ + protected function getConfigCacheDir() + { + $config = $this->getApplicationConfig(); + if (!empty($config['module_listener_options']['cache_dir'])) { + return $config['module_listener_options']['cache_dir']; + } + + return null; + } + + /** + * Return the configured configuration cache key, if any. + * + * @return null|string + */ + protected function getConfigCacheKey() + { + $config = $this->getApplicationConfig(); + if (!empty($config['module_listener_options']['config_cache_key'])) { + return $config['module_listener_options']['config_cache_key']; + } + + return null; + } + + /** + * Return the application configuration. + * + * Memorizes the discovered configuration so subsequent calls can re-use the value. + * + * Exits with status code 1 if unable to find the configuration. + * + * @return array + */ + protected function getApplicationConfig() + { + if (!$this->config) { + if (!file_exists(self::APP_CONFIG_FILE)) { + $this->response( + 1, + 'Cannot locate ' . self::APP_CONFIG_FILE . '; are you in the' . PHP_EOL + . 'application root, and is this a ZendFramework application?' + ); + } + + $this->config = include self::APP_CONFIG_FILE; + } + + return $this->config; + } +} + +new DevelopmentMode($argc, $argv); diff --git a/config/application.config.php b/config/application.config.php index 6f989633db..ea34ed59e8 100644 --- a/config/application.config.php +++ b/config/application.config.php @@ -6,12 +6,8 @@ * @see http://framework.zend.com/manual/current/en/tutorials/config.advanced.html#environment-specific-application-configuration */ return [ - // This should be an array of module namespaces used in the application. - 'modules' => [ - 'Zend\Router', - 'Zend\Validator', - 'Application', - ], + // Retrieve list of modules used in this application. + 'modules' => require __DIR__ . '/modules.config.php', // These are various options for the listeners attached to the ModuleManager 'module_listener_options' => [ diff --git a/config/development.config.php.dist b/config/development.config.php.dist new file mode 100644 index 0000000000..2446217177 --- /dev/null +++ b/config/development.config.php.dist @@ -0,0 +1,18 @@ + [ + ], + // development time configuration globbing + 'module_listener_options' => [ + 'config_glob_paths' => ['config/autoload/{,*.}{global,local}-development.php'], + 'config_cache_enabled' => false, + 'module_map_cache_enabled' => false, + ], +]; diff --git a/config/modules.config.php b/config/modules.config.php new file mode 100644 index 0000000000..92ac3cfd4e --- /dev/null +++ b/config/modules.config.php @@ -0,0 +1,17 @@ +run(); +Application::init($appConfig)->run();