Add a strict mode to ensure ProductionSettings

This commit is contained in:
Romain Neutron
2012-03-05 18:51:05 +01:00
parent c1af665180
commit 60214728a6

View File

@@ -44,6 +44,7 @@ class module_console_fileEnsureProductionSetting extends Command
, 'checkOpcodeCacheService' , 'checkOpcodeCacheService'
); );
protected $errors = 0; protected $errors = 0;
protected $alerts = 0;
public function __construct($name = null) public function __construct($name = null)
{ {
@@ -52,6 +53,7 @@ class module_console_fileEnsureProductionSetting extends Command
$this->setDescription('Ensure production settings'); $this->setDescription('Ensure production settings');
$this->addArgument('conf', InputArgument::OPTIONAL, 'The file to check', null); $this->addArgument('conf', InputArgument::OPTIONAL, 'The file to check', null);
$this->addOption('strict', 's', InputOption::VALUE_NONE, 'Wheter to fail on alerts or not');
return $this; return $this;
} }
@@ -74,8 +76,23 @@ class module_console_fileEnsureProductionSetting extends Command
$this->runTests($output); $this->runTests($output);
$retval = $this->errors;
if ($input->getOption('strict'))
{
$retval += $this->alerts;
}
if ($retval > 0)
{
$output->writeln("\n<error>Some errors found in your conf</error>");
}
else
{
$output->writeln("\n<info>Your production settings are setted correctly ! Enjoy</info>");
}
$output->writeln('End'); $output->writeln('End');
return 0;
return $retval;
} }
private function runTests(OutputInterface $output) private function runTests(OutputInterface $output)
@@ -112,15 +129,6 @@ class module_console_fileEnsureProductionSetting extends Command
call_user_func(array($this, $test), $output); call_user_func(array($this, $test), $output);
} }
if ($this->errors)
{
$output->writeln("\n<error>Some errors found in your conf</error>");
}
else
{
$output->writeln("\n<info>Your production settings are setted correctly ! Enjoy</info>");
}
return $this->errors;
} }
private function checkParse(OutputInterface $output) private function checkParse(OutputInterface $output)
@@ -156,6 +164,7 @@ class module_console_fileEnsureProductionSetting extends Command
else else
{ {
$work_message = '<comment>Cache server recommended</comment>'; $work_message = '<comment>Cache server recommended</comment>';
$this->alerts++;
} }
} }
else else
@@ -185,6 +194,7 @@ class module_console_fileEnsureProductionSetting extends Command
else else
{ {
$work_message = '<comment>Opcode recommended</comment>'; $work_message = '<comment>Opcode recommended</comment>';
$this->alerts++;
} }
} }
else else
@@ -211,6 +221,7 @@ class module_console_fileEnsureProductionSetting extends Command
switch ($conf) switch ($conf)
{ {
default: default:
$this->alerts++;
$this->printConf($output, $conf, $value, false, '<comment>Not recognized</comment>'); $this->printConf($output, $conf, $value, false, '<comment>Not recognized</comment>');
break; break;
case 'servername': case 'servername':
@@ -226,6 +237,7 @@ class module_console_fileEnsureProductionSetting extends Command
} }
elseif ($url == 'http://sub.domain.tld/') elseif ($url == 'http://sub.domain.tld/')
{ {
$this->alerts++;
$message = "<comment>may be wrong</comment>"; $message = "<comment>may be wrong</comment>";
} }
elseif (!filter_var($url, FILTER_VALIDATE_URL)) elseif (!filter_var($url, FILTER_VALIDATE_URL))
@@ -235,6 +247,7 @@ class module_console_fileEnsureProductionSetting extends Command
} }
elseif ($parseUrl["scheme"] !== "https") elseif ($parseUrl["scheme"] !== "https")
{ {
$this->alerts++;
$message = "<comment>should be https</comment>"; $message = "<comment>should be https</comment>";
} }
else else
@@ -247,7 +260,14 @@ class module_console_fileEnsureProductionSetting extends Command
case 'debug': case 'debug':
case 'display_errors': case 'display_errors':
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = $value ? '<error>Should be false</error>' : '<info>OK</info>'; $message = '<info>OK</info>';
if ($value !== false)
{
$message = '<error>Should be false</error>';
$this->errors++;
}
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
case 'database': case 'database':
@@ -335,11 +355,17 @@ class module_console_fileEnsureProductionSetting extends Command
switch ($conf) switch ($conf)
{ {
default: default:
$this->alerts++;
$this->printConf($output, $conf, $value, false, '<comment>Not recognized</comment>'); $this->printConf($output, $conf, $value, false, '<comment>Not recognized</comment>');
break; break;
case 'charset': case 'charset':
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = $value == 'UTF8' ? '<info>OK</info>' : '<comment>Not recognized</comment>'; $message = '<info>OK</info>';
if ($value !== 'UTF8')
{
$message = '<comment>Not recognized</comment>';
$this->alerts++;
}
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
case 'path': case 'path':
@@ -351,23 +377,50 @@ class module_console_fileEnsureProductionSetting extends Command
case 'user': case 'user':
case 'host': case 'host':
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = is_scalar($value) ? '<info>OK</info>' : '<error>Should be scalar</error>'; $message = '<info>OK</info>';
if (!is_scalar($value))
{
$message = '<error>Should be scalar</error>';
$this->errors++;
}
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
case 'port': case 'port':
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = ctype_digit($value) ? '<info>OK</info>' : '<error>Should be scalar</error>'; $message = '<info>OK</info>';
if (!ctype_digit($value))
{
$message = '<error>Should be ctype_digit</error>';
$this->errors++;
}
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
case 'password': case 'password':
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = is_scalar($value) ? '<info>OK</info>' : '<error>Should be scalar</error>'; $message = '<info>OK</info>';
if (!is_scalar($value))
{
$message = '<error>Should be scalar</error>';
$this->errors++;
}
$value = '***********'; $value = '***********';
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
case 'driver': case 'driver':
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = $value === 'pdo_mysql' ? '<info>OK</info>' : '<error>MySQL recommended</error>'; $message = '<info>OK</info>';
if ($value !== 'pdo_mysql')
{
$message = '<error>MySQL recommended</error>';
$this->errors++;
}
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
} }
@@ -442,10 +495,18 @@ class module_console_fileEnsureProductionSetting extends Command
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
case 'options': case 'options':
$message = is_array($value) ? '<info>OK</info>' : '<error>Should be array</error>'; $message = '<info>OK</info>';
if (!is_array($value))
{
$message = '<error>Should be array</error>';
$this->errors++;
}
$this->printConf($output, $conf, 'array()', false, $message); $this->printConf($output, $conf, 'array()', false, $message);
break; break;
default: default:
$this->alerts++;
$this->printConf($output, $conf, 'unknown', false, '<comment>Not recognized</comment>'); $this->printConf($output, $conf, 'unknown', false, '<comment>Not recognized</comment>');
break; break;
} }
@@ -458,21 +519,43 @@ class module_console_fileEnsureProductionSetting extends Command
case 'debug'; case 'debug';
case 'strict_variables'; case 'strict_variables';
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = $value == false ? '<info>OK</info>' : '<error>Should be false</error>'; $message = '<info>OK</info>';
if ($value !== false)
{
$message = '<error>Should be false</error>';
$this->errors++;
}
$this->printConf($output, "\t" . $conf, $value, false, $message); $this->printConf($output, "\t" . $conf, $value, false, $message);
break; break;
case 'autoescape'; case 'autoescape';
case 'optimizer'; case 'optimizer';
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = $value == true ? '<info>OK</info>' : '<error>Should be true</error>'; $message = '<info>OK</info>';
if ($value !== true)
{
$message = '<error>Should be true</error>';
$this->errors++;
}
$this->printConf($output, "\t" . $conf, $value, false, $message); $this->printConf($output, "\t" . $conf, $value, false, $message);
break; break;
case 'charset'; case 'charset';
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = $value == 'utf-8' ? '<info>OK</info>' : '<comment>Not recognized</comment>'; $message = '<info>OK</info>';
if ($value !== 'utf-8')
{
$message = '<comment>Not recognized</comment>';
$this->alerts++;
}
$this->printConf($output, "\t" . $conf, $value, false, $message); $this->printConf($output, "\t" . $conf, $value, false, $message);
break; break;
default: default:
$this->alerts++;
$this->printConf($output, "\t" . $conf, $value, false, '<comment>Not recognized</comment>'); $this->printConf($output, "\t" . $conf, $value, false, '<comment>Not recognized</comment>');
break; break;
} }
@@ -533,10 +616,18 @@ class module_console_fileEnsureProductionSetting extends Command
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
case 'options': case 'options':
$message = is_array($value) ? '<info>OK</info>' : '<error>Should be array</error>'; $message = '<info>OK</info>';
if (!is_array($value))
{
$message = '<error>Should be array</error>';
$this->errors++;
}
$this->printConf($output, $conf, 'array()', false, $message); $this->printConf($output, $conf, 'array()', false, $message);
break; break;
default: default:
$this->alerts++;
$this->printConf($output, $conf, 'unknown', false, '<comment>Not recognized</comment>'); $this->printConf($output, $conf, 'unknown', false, '<comment>Not recognized</comment>');
break; break;
} }
@@ -548,12 +639,26 @@ class module_console_fileEnsureProductionSetting extends Command
switch ($conf) switch ($conf)
{ {
case 'log': case 'log':
$message = $value == false ? '<info>OK</info>' : '<error>Should be deactivated</error>'; $message = '<info>OK</info>';
if ($value !== false)
{
$message = '<error>Should be deactivated</error>';
$this->errors++;
}
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
case 'cache': case 'cache':
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = is_array($value) ? '<info>OK</info>' : '<error>Should be Array</error>'; $message = '<info>OK</info>';
if (!is_array($value))
{
$message = '<error>Should be Array</error>';
$this->errors++;
}
$this->printConf($output, $conf, 'array()', false, $message); $this->printConf($output, $conf, 'array()', false, $message);
$required_caches = array('query', 'result', 'metadata'); $required_caches = array('query', 'result', 'metadata');
@@ -575,6 +680,7 @@ class module_console_fileEnsureProductionSetting extends Command
} }
else else
{ {
$this->alerts++;
if ($server) if ($server)
{ {
$work_message = '<comment>Cache server recommended</comment>'; $work_message = '<comment>Cache server recommended</comment>';
@@ -598,6 +704,7 @@ class module_console_fileEnsureProductionSetting extends Command
$this->verifyCacheOptions($output, $value_cache); $this->verifyCacheOptions($output, $value_cache);
break; break;
default: default:
$this->alerts++;
$this->printConf($output, "\t" . $key_cache, $value_cache, false, '<comment>Not recognized</comment>'); $this->printConf($output, "\t" . $key_cache, $value_cache, false, '<comment>Not recognized</comment>');
break; break;
} }
@@ -617,7 +724,14 @@ class module_console_fileEnsureProductionSetting extends Command
break; break;
case 'debug': case 'debug':
$required = array_diff($required, array($conf)); $required = array_diff($required, array($conf));
$message = $value == false ? '<info>OK</info>' : '<error>Should be false</error>'; $message = '<info>OK</info>';
if ($value !== false)
{
$message = '<error>Should be false</error>';
$this->errors++;
}
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
case 'dbal': case 'dbal':
@@ -636,6 +750,7 @@ class module_console_fileEnsureProductionSetting extends Command
$this->printConf($output, $conf, $value, false, $message); $this->printConf($output, $conf, $value, false, $message);
break; break;
default: default:
$this->alerts++;
$this->printConf($output, $conf, $value, false, '<comment>Not recognized</comment>'); $this->printConf($output, $conf, $value, false, '<comment>Not recognized</comment>');
break; break;
} }
@@ -684,15 +799,30 @@ class module_console_fileEnsureProductionSetting extends Command
{ {
case 'host'; case 'host';
$required_options = array_diff($required_options, array($conf)); $required_options = array_diff($required_options, array($conf));
$message = is_scalar($value) ? '<info>OK</info>' : '<error>Should be scalar</error>'; $message = '<info>OK</info>';
if (!is_scalar($value))
{
$message = '<error>Should be scalar</error>';
$this->errors++;
}
$this->printConf($output, "\t\t" . $conf, $value, false, $message); $this->printConf($output, "\t\t" . $conf, $value, false, $message);
break; break;
case 'port'; case 'port';
$required_options = array_diff($required_options, array($conf)); $required_options = array_diff($required_options, array($conf));
$message = ctype_digit($value) ? '<info>OK</info>' : '<comment>Not recognized</comment>'; $message = '<info>OK</info>';
if (!ctype_digit($value))
{
$message = '<comment>Not recognized</comment>';
$this->alerts++;
}
$this->printConf($output, "\t\t" . $conf, $value, false, $message); $this->printConf($output, "\t\t" . $conf, $value, false, $message);
break; break;
default: default:
$this->alerts++;
$this->printConf($output, "\t\t" . $conf, $value, false, '<comment>Not recognized</comment>'); $this->printConf($output, "\t\t" . $conf, $value, false, '<comment>Not recognized</comment>');
break; break;
} }