Browse Source

Move mud.php to Symfony Console Commands

pull/183/head
Timothée Jaussoin 10 years ago
parent
commit
713b48fcab
  1. 3
      composer.json
  2. 64
      composer.lock
  3. 2
      daemon.php
  4. 179
      mud.php
  5. 79
      src/Movim/Console/ConfigCommand.php
  6. 45
      src/Movim/Console/DatabaseCommand.php

3
composer.json

@ -32,6 +32,7 @@
"respect/validation": "1.0.*",
"ezyang/htmlpurifier": "^4.7",
"ramsey/uuid": "^3.2",
"react/promise-timer": "^1.1"
"react/promise-timer": "^1.1",
"symfony/console": "^3.0"
}
}

64
composer.lock

@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "959957c08a6e0a7b09d5b3bccf6f6979",
"content-hash": "958b7ab7a85b3465f8038d6848504ff1",
"hash": "d7e0f609d8202a51996db1bf15e31cd6",
"content-hash": "a0989ca3e7631971324ba9600482edb2",
"packages": [
{
"name": "cboden/ratchet",
@ -1636,6 +1636,66 @@
],
"time": "2015-12-03 05:49:01"
},
{
"name": "symfony/console",
"version": "v3.0.4",
"source": {
"type": "git",
"url": "https://github.com/symfony/console.git",
"reference": "6b1175135bc2a74c08a28d89761272de8beed8cd"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/console/zipball/6b1175135bc2a74c08a28d89761272de8beed8cd",
"reference": "6b1175135bc2a74c08a28d89761272de8beed8cd",
"shasum": ""
},
"require": {
"php": ">=5.5.9",
"symfony/polyfill-mbstring": "~1.0"
},
"require-dev": {
"psr/log": "~1.0",
"symfony/event-dispatcher": "~2.8|~3.0",
"symfony/process": "~2.8|~3.0"
},
"suggest": {
"psr/log": "For using the console logger",
"symfony/event-dispatcher": "",
"symfony/process": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Console\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Console Component",
"homepage": "https://symfony.com",
"time": "2016-03-16 17:00:50"
},
{
"name": "symfony/event-dispatcher",
"version": "v3.0.4",

2
daemon.php

@ -40,7 +40,7 @@ if($infos != null) {
}
echo colorize("\nTo update the database run\n", 'green');
echo colorize("php mud.php db set\n", 'purple');
echo colorize("php mud.php db --set\n", 'purple');
exit;
}

179
mud.php

@ -5,178 +5,15 @@ require __DIR__ . '/vendor/autoload.php';
define('DOCUMENT_ROOT', dirname(__FILE__));
use Movim\Bootstrap;
use Movim\Console\DatabaseCommand;
use Movim\Console\ConfigCommand;
use Symfony\Component\Console\Application;
$bootstrap = new Bootstrap();
$bootstrap->boot();
$argsize = count($argv);
if($argsize == 1) {
echo
colorize("Welcome to Mud - Movim Unified Doer
Here some requests you can do with me :", 'green')."
- ".colorize("getloc", 'yellow')." grab all the translations in Movim and generate a global locale file
- ".colorize("comploc", 'yellow')." compile the current locales to a simple PHP array to boost Movim execution
- ".colorize("comptz", 'yellow')." compile the timezones
- ".colorize("db", 'yellow')." create/update the database
- ".colorize("config", 'yellow')." set the configuration of Movim (separated by commas and colons) eg. info:Test,description:Hop
";
} elseif($argsize == 2) {
switch ($argv[1]) {
case 'getloc':
getloc();
break;
/*case 'comploc':
comploc();
break;*/
case 'comptz':
comptz();
break;
case 'config':
echo colorize("You need to pass an argument", 'red');
break;
case 'db':
$md = Modl\Modl::getInstance();
$infos = $md->check();
if($infos == null) {
echo colorize("Nothing to do\n", 'green');
} else {
echo colorize("The database need to be updated\n", 'green');
foreach($infos as $i) {
echo colorize($i."\n", 'blue');
}
}
break;
}
} elseif($argsize == 3) {
switch ($argv[1]) {
case 'config':
config($argv[2]);
break;
case 'db':
if($argv[2] == 'set') {
$md = Modl\Modl::getInstance();
$md->check(true);
echo colorize("Database updated\n", 'green');
}
break;
}
}
function config($values) {
echo colorize("Movim configuration setter\n", 'green');
$cd = new \Modl\ConfigDAO();
$config = $cd->get();
$values = explode(',', $values);
foreach($values as $value) {
$exp = explode(':', $value);
$key = $exp[0];
array_shift($exp);
$value = implode(':', $exp);
if(property_exists($config, $key)) {
$old = $config->$key;
$config->$key = $value;
$cd->set($config);
echo colorize("The configuration key ", 'yellow').
colorize($key, 'red').
colorize(" has been updated from ", 'yellow').
colorize($old, 'blue').
colorize(" to ", 'yellow').
colorize($value, 'blue')."\n";
}
}
}
function getloc() {
echo colorize("Locales grabber\n", 'green');
// We look for all the ini files
$inifiles = glob(WIDGETS_PATH.'*/*.ini');
array_push($inifiles, LOCALES_PATH . 'locales.ini');
$locales = CACHE_PATH.'locales.php';
$pot = CACHE_PATH.'messages.pot';
// We create the cache file
$out = "<?php\n";
foreach($inifiles as $ini) {
$keys = parse_ini_file($ini);
foreach($keys as $key => $value) {
$out .= "t(\"$value\");\n";
}
}
$fp = fopen($locales, 'w');
fwrite($fp, $out);
fclose($fp);
echo "File $locales created\n";
// And we run gettext on it
exec("xgettext -e --no-wrap -kt -o $pot -L PHP $locales ");
echo "File $pot created\n";
}
function comploc() {
echo colorize("Locales compiler\n", 'green');
$folder = CACHE_PATH.'/locales/';
if(!file_exists($folder)) {
$bool = mkdir($folder);
if(!$bool) {
echo colorize("The locales cache folder can't be created", 'red');
exit;
}
} else
echo colorize("Folder already exist, don't re-create it\n", 'red');
$langs = loadLangArray();
foreach($langs as $key => $value) {
$langarr = parseLangFile(DOCUMENT_ROOT . '/locales/' . $key . '.po');
$out = '<?php global $translations;
$translations = array(';
foreach($langarr as $msgid => $msgstr)
if($msgid != '')
$out .= '"'.$msgid.'" => "'. $msgstr . '",'."\n";
$out .= ');';
$fp = fopen($folder.$key.'.php', 'w');
fwrite($fp, $out);
fclose($fp);
echo "- $key compiled\n";
}
}
function comptz() {
$file = HELPERS_PATH.'TimezoneList.php';
$tz = generateTimezoneList();
$out = '<?php global $timezones;
$timezones = array(';
foreach($tz as $key => $value)
$out .= '"'.$key.'" => "'. $value . '",'."\n";
$out .= ');';
$fp = fopen($file, 'w');
fwrite($fp, $out);
fclose($fp);
echo "- Timezones compiled in $file\n";
}
$application = new Application;
$application->add(new DatabaseCommand);
$application->add(new ConfigCommand);
$application->run();
echo "\n";

79
src/Movim/Console/ConfigCommand.php

@ -0,0 +1,79 @@
<?php
namespace Movim\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class ConfigCommand extends Command
{
protected function configure()
{
$this
->setName('config')
->setDescription('Change the configuration')
->addOption(
'info',
null,
InputOption::VALUE_REQUIRED,
'Content of the info box on the login page'
)
->addOption(
'username',
null,
InputOption::VALUE_REQUIRED,
'Username for the admin area'
)
->addOption(
'password',
null,
InputOption::VALUE_REQUIRED,
'Password for the admin area'
)
->addOption(
'timezone',
null,
InputOption::VALUE_REQUIRED,
'The server timezone'
)
->addOption(
'loglevel',
null,
InputOption::VALUE_REQUIRED,
'The server loglevel, default 0'
)
->addOption(
'locale',
null,
InputOption::VALUE_REQUIRED,
'The server main locale'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$cd = new \Modl\ConfigDAO();
$config = $cd->get();
foreach($input->getOptions() as $key => $value) {
if(property_exists($config, $key) && isset($value)) {
if($key == 'password') $value = sha1($value);
$old = $config->$key;
$config->$key = $value;
$cd->set($config);
$output->writeln(
'<info>The configuration key</info> '.
$key.
' <info>has been updated from</info> '.
$old.
' <info>to</info> '.
$value);
}
}
}
}

45
src/Movim/Console/DatabaseCommand.php

@ -0,0 +1,45 @@
<?php
namespace Movim\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DatabaseCommand extends Command
{
protected function configure()
{
$this
->setName('db')
->setDescription('Database updater')
->addOption(
'set',
's',
InputOption::VALUE_NONE,
'Will apply updates on the database'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$md = \Modl\Modl::getInstance();
if($input->getOption('set')) {
$md->check(true);
$output->writeln('<info>Database updated</info>');
} else {
$toDo = $md->check();
if($toDo != null) {
$output->writeln('<comment>The database needs to be updated</comment>');
foreach($toDo as $do) {
$output->writeln('<question>'.$do.'</question>');
}
} else {
$output->writeln('<info>Nothing to do here</info>');
}
}
}
}
Loading…
Cancel
Save