Skip to content
This repository was archived by the owner on Jan 1, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions bin/development-mode
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
#!/usr/bin/env php
<?php

/**
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class DevelopmentMode
{
const CONFIG_CACHE_BASE = 'module-config-cache';

const APP_CONFIG_FILE = 'config/application.config.php';

private $config;

private $help = <<< EOH
Enable/Disable development mode.

Usage:

development-mode [-h|--help] disable|enable

--help|-h Print this usage message.
disable Disable development mode.
enable Enable development mode
(do not use in production).

To enable development mode, the following file MUST exist:

- config/development.config.php.dist; this file will be copied to
config/development.config.php

Optionally:

- config/autoload/development.local.php.dist; this file will be copied to
config/autoload/development.local.php

When disabling development mode:

- config/development.config.php will be removed if it exists
- config/autoload/development.local.php will be removed if it exists

Additionally, both when disabling and enabling development mode, the
script will remove the file cache/module-config-cache.php (or the file
specified by the combination of the module_listener_options.cache_dir
and module_listener_options.config_cache_key options).
EOH;

public function __construct($argc, array $argv)
{
// Called without arguments
if ($argc < 2) {
$this->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);
8 changes: 2 additions & 6 deletions config/application.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
18 changes: 18 additions & 0 deletions config/development.config.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

return [
// Development time modules
'modules' => [
],
// 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,
],
];
17 changes: 17 additions & 0 deletions config/modules.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* List of enabled modules for this application.
*
* This should be an array of module namespaces used in the application.
*/
return [
'Zend\Router',
'Zend\Validator',
'Application',
];
9 changes: 8 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Zend\Mvc\Application;
use Zend\Stdlib\ArrayUtils;

/**
* This makes our life easier when dealing with paths. Everything is relative
Expand Down Expand Up @@ -29,5 +30,11 @@
);
}

// Retrieve configuration
$appConfig = require __DIR__ . '/../config/application.config.php';
if (file_exists(__DIR__ . '/../config/development.config.php')) {
$appConfig = ArrayUtils::merge($appConfig, require __DIR__ . '/../config/development.config.php');
}

// Run the application!
Application::init(require __DIR__ . '/../config/application.config.php')->run();
Application::init($appConfig)->run();