From 17b02d265db1329040455816036ad61223698c61 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 21 Jun 2016 17:11:38 -0500 Subject: [PATCH 1/4] Provide development mode zend-development-mode has been a popular feature of Apigility, and a popular standalone module. Unfortunately, its architecture uses the MVC <-> console integration, which is problematic in the streamlined skeleton, which does not include that out-of-the-box. This patch creates development-mode as as standalone script in the project `bin/` directory. Additionally, we add the `config/modules.config.php`, to push modules into a separate file, and `config/development.config.php.dist`, to provide a basic structure for segregated configuration. Finally, the bootstrap is updated to test for the presence of `config/development.config.php` and merge its contents with the application configuration. Usage is: ```bash $ ./bin/development-mode enable ``` Or, to disable: ```bash $ ./bin/development-mode disable ``` --- bin/development-mode | 235 +++++++++++++++++++++++++++++ config/application.config.php | 8 +- config/development.config.php.dist | 18 +++ config/modules.config.php | 17 +++ public/index.php | 9 +- 5 files changed, 280 insertions(+), 7 deletions(-) create mode 100755 bin/development-mode create mode 100644 config/development.config.php.dist create mode 100644 config/modules.config.php diff --git a/bin/development-mode b/bin/development-mode new file mode 100755 index 0000000000..b728be8265 --- /dev/null +++ b/bin/development-mode @@ -0,0 +1,235 @@ +#!/usr/bin/env php + [ - '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(); From dd2e36c5dfb8d7048ed4c89c9838b40adf8073c4 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 21 Jun 2016 17:42:26 -0500 Subject: [PATCH 2/4] Normalize all quotation marks. --- bin/development-mode | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/development-mode b/bin/development-mode index b728be8265..cb10255823 100755 --- a/bin/development-mode +++ b/bin/development-mode @@ -73,7 +73,7 @@ function disable() { if (! file_exists('config/development.config.php')) { // nothing to do - echo "Development mode was already disabled.", PHP_EOL; + echo 'Development mode was already disabled.', PHP_EOL; return 0; } @@ -86,7 +86,7 @@ function disable() removeConfigCacheFile(getConfigCacheFile()); - echo "Development mode is now disabled.", PHP_EOL; + echo 'Development mode is now disabled.', PHP_EOL; return 0; } @@ -99,14 +99,14 @@ function enable() { if (file_exists('config/development.config.php')) { // nothing to do - echo "Already in development mode!", PHP_EOL; + echo 'Already in development mode!', PHP_EOL; return 0; } if (! file_exists('config/development.config.php.dist')) { fwrite( STDERR, - "MISSING \"config/development.config.php.dist\". Could not switch to development mode!" . PHP_EOL + 'MISSING "config/development.config.php.dist". Could not switch to development mode!' . PHP_EOL ); return 1; } From 1800418b4170f1ce7b9cd39eccdf1331de4ec5b0 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 21 Jun 2016 17:43:34 -0500 Subject: [PATCH 3/4] Fix path in bootstrap script --- public/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/index.php b/public/index.php index 3dd188e282..4343eb7559 100644 --- a/public/index.php +++ b/public/index.php @@ -32,7 +32,7 @@ // Retrieve configuration $appConfig = require __DIR__ . '/../config/application.config.php'; -if (file_exists(__DIR__ . '/config/development.config.php')) { +if (file_exists(__DIR__ . '/../config/development.config.php')) { $appConfig = ArrayUtils::merge($appConfig, require __DIR__ . '/../config/development.config.php'); } From 589fbd083bb029e3f38798b34da3bceda7607778 Mon Sep 17 00:00:00 2001 From: webimpress Date: Wed, 22 Jun 2016 00:56:32 +0100 Subject: [PATCH 4/4] development-mode class --- bin/development-mode | 305 ++++++++++++++++++++----------------------- 1 file changed, 145 insertions(+), 160 deletions(-) diff --git a/bin/development-mode b/bin/development-mode index cb10255823..87213c2373 100755 --- a/bin/development-mode +++ b/bin/development-mode @@ -1,14 +1,20 @@ #!/usr/bin/env php response(1, 'No arguments provided.' . PHP_EOL . PHP_EOL . $this->help); + } -if (! in_array($argv[1], ['disable', 'enable'], true)) { - fwrite(STDERR, 'Unrecognized argument.' . PHP_EOL . PHP_EOL); - fwrite(STDERR, $help . PHP_EOL); - exit(1); -} + // Requested help + if (in_array($argv[1], ['-h', '--help'], true)) { + $this->response(0, $this->help); + } -$action = $argv[1]; -$return = $action(); -exit((int) $return); + if (!in_array($argv[1], ['disable', 'enable'], true)) { + $this->response(1, 'Unrecognized argument.' . PHP_EOL . PHP_EOL . $this->help); + } -/** - * Disable development mode - * - * @return int - */ -function disable() -{ - if (! file_exists('config/development.config.php')) { - // nothing to do - echo 'Development mode was already disabled.', PHP_EOL; - return 0; + $action = $argv[1]; + $this->$action(); } - if (file_exists('config/autoload/development.local.php')) { - // optional application config override - unlink('config/autoload/development.local.php'); + protected function response($code, $text = null) + { + if ($text) { + if ($code != 0) { + fwrite(STDERR, $text . PHP_EOL); + } else { + echo $text . PHP_EOL; + } + } + + exit($code); } - unlink('config/development.config.php'); + /** + * Disable development mode + */ + protected function disable() + { + if (!file_exists('config/development.config.php')) { + // nothing to do + $this->response(0, 'Development mode was already disabled.'); + } - removeConfigCacheFile(getConfigCacheFile()); + if (file_exists('config/autoload/development.local.php')) { + // optional application config override + unlink('config/autoload/development.local.php'); + } - echo 'Development mode is now disabled.', PHP_EOL; - return 0; -} + unlink('config/development.config.php'); -/** - * Enable development mode - * - * @return int - */ -function enable() -{ - if (file_exists('config/development.config.php')) { - // nothing to do - echo 'Already in development mode!', PHP_EOL; - return 0; - } + $this->removeConfigCacheFile(); - if (! file_exists('config/development.config.php.dist')) { - fwrite( - STDERR, - 'MISSING "config/development.config.php.dist". Could not switch to development mode!' . PHP_EOL - ); - return 1; + $this->response(0, 'Development mode is now disabled.'); } - copy('config/development.config.php.dist', 'config/development.config.php'); + /** + * 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/autoload/development.local.php.dist')) { - // optional application config override - copy('config/autoload/development.local.php.dist', 'config/autoload/development.local.php'); - } + if (!file_exists('config/development.config.php.dist')) { + $this->response(1, 'MISSING "config/development.config.php.dist". Could not switch to development mode!'); + } - removeConfigCacheFile(getConfigCacheFile()); + copy('config/development.config.php.dist', 'config/development.config.php'); - echo 'You are now in development mode.', PHP_EOL; - return 0; -} + 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'); + } -/** - * Removes the application configuration cache file, if present. - */ -function removeConfigCacheFile($configCacheFile) -{ - if ($configCacheFile && file_exists($configCacheFile)) { - unlink($configCacheFile); - } -} + $this->removeConfigCacheFile(); -/** - * Retrieve the config cache file, if any. - * - * @return false|string - */ -function getConfigCacheFile() -{ - $configCacheDir = getConfigCacheDir(); - $configCacheKey = getConfigCacheKey(); - - if (empty($configCacheDir)) { - return false; + $this->response(0, 'You are now in development mode.'); } - $path = sprintf('%s/%s.', $configCacheDir, CONFIG_CACHE_BASE); + /** + * Removes the application configuration cache file, if present. + */ + protected function removeConfigCacheFile() + { + $configCacheFile = $this->getConfigCacheFile(); - if (! empty($configCacheKey)) { - $path .= $configCacheKey . '.'; + if ($configCacheFile && file_exists($configCacheFile)) { + unlink($configCacheFile); + } } - return $path . 'php'; -} + /** + * Retrieve the config cache file, if any. + * + * @return false|string + */ + protected function getConfigCacheFile() + { + if (!$configCacheDir = $this->getConfigCacheDir()) { + return false; + } -/** - * Return the configured configuration cache directory, if any. - * - * @return null|string - */ -function getConfigCacheDir() -{ - static $dir; + $path = sprintf('%s/%s.', $configCacheDir, self::CONFIG_CACHE_BASE); - if ($dir) { - return $dir; - } + if ($configCacheKey = $this->getConfigCacheKey()) { + $path .= $configCacheKey . '.'; + } - $config = getApplicationConfig(); - if (isset($config['module_listener_options']['cache_dir']) - && ! empty($config['module_listener_options']['cache_dir']) - ) { - $dir = $config['module_listener_options']['cache_dir']; + return $path . 'php'; } - return $dir; -} - -/** - * Return the configured configuration cache key, if any. - * - * @return null|string - */ -function getConfigCacheKey() -{ - static $key; - - if ($key) { - return $key; + /** + * 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; } - $config = getApplicationConfig(); - if (isset($config['module_listener_options']['config_cache_key']) - && ! empty($config['module_listener_options']['config_cache_key']) - ) { - $key = $config['module_listener_options']['config_cache_key']; + /** + * 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 $key; -} - -/** - * Return the application configuration. - * - * Memoizes the discovered configuration so subsequent calls can re-use the - * value. - * - * Exits with status code 2 if unable to find the configuration. - * - * @return array - */ -function getApplicationConfig() -{ - static $config; - - if ($config) { - return $config; - } - - if (! file_exists('config/application.config.php')) { - fwrite( - STDERR, - 'Cannot locate config/application.config.php; are you in the' . PHP_EOL - . 'application root, and is this a zendframework application?' . PHP_EOL - ); - exit(1); + /** + * 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; } - - $config = include 'config/application.config.php'; - return $config; } + +new DevelopmentMode($argc, $argv);