Compare commits
3 commits
6aa4113310
...
97a6b4b7cb
Author | SHA1 | Date | |
---|---|---|---|
97a6b4b7cb | |||
c626bc177a | |||
f499087241 |
4 changed files with 149 additions and 30 deletions
2
eesyphp
2
eesyphp
|
@ -16,4 +16,4 @@ else
|
||||||
die("Fail to find composer vendor/autoload.php file\n");
|
die("Fail to find composer vendor/autoload.php file\n");
|
||||||
|
|
||||||
App::init(null, null, __DIR__);
|
App::init(null, null, __DIR__);
|
||||||
Cli :: handle_args();
|
Cli :: handle_args(null, true);
|
||||||
|
|
|
@ -75,6 +75,38 @@ class Check {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function ip_address($ip, $allow_private_ip_address=true, $allow_reserved_ip_address=false,
|
||||||
|
$ipv4_only=false, $ipv6_only=false) {
|
||||||
|
$flag = null;
|
||||||
|
if ($ipv4_only)
|
||||||
|
$flag |= FILTER_FLAG_IPV4;
|
||||||
|
|
||||||
|
if ($ipv6_only)
|
||||||
|
$flag |= FILTER_FLAG_IPV6;
|
||||||
|
|
||||||
|
if (!isset($allow_private_ip_address) || !$allow_private_ip_address)
|
||||||
|
$flag |= FILTER_FLAG_NO_PRIV_RANGE;
|
||||||
|
|
||||||
|
if (!isset($allow_reserved_ip_address) || !$allow_reserved_ip_address)
|
||||||
|
$flag |= FILTER_FLAG_NO_RES_RANGE;
|
||||||
|
|
||||||
|
if (!is_null($flag))
|
||||||
|
return (bool)filter_var($ip, FILTER_VALIDATE_IP, $flag);
|
||||||
|
else
|
||||||
|
return (bool)filter_var($ip, FILTER_VALIDATE_IP);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function tcp_or_udp_port($value) {
|
||||||
|
if (!is_int($value)) {
|
||||||
|
if (!preg_match('/^[0-9]+$/', $value))
|
||||||
|
return false;
|
||||||
|
$value = intval($value);
|
||||||
|
}
|
||||||
|
if ($value <= 0) return false;
|
||||||
|
if ($value > 65635) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public static function is_empty($val) {
|
public static function is_empty($val) {
|
||||||
switch(gettype($val)) {
|
switch(gettype($val)) {
|
||||||
case "boolean":
|
case "boolean":
|
||||||
|
|
116
src/Cli.php
116
src/Cli.php
|
@ -6,22 +6,59 @@ use Exception;
|
||||||
|
|
||||||
class Cli {
|
class Cli {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EesyPHP core mode
|
||||||
|
*/
|
||||||
|
private static $core_mode = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize
|
* Initialize
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function init() {
|
public static function init() {
|
||||||
if (php_sapi_name() != 'cli')
|
Hook :: register('cli_set_core_mode', array('\\EesyPHP\\Cli', 'on_cli_set_core_mode'));
|
||||||
return;
|
}
|
||||||
self :: add_command(
|
|
||||||
'new_project',
|
/**
|
||||||
array('\\EesyPHP\\Cli', 'cli_new_project'),
|
* Get/set core mode
|
||||||
I18n :: ___("Create a new project using EesyPHP framework"),
|
* @return bool
|
||||||
null,
|
*/
|
||||||
I18n :: ___(
|
public static function core_mode($enable=null) {
|
||||||
"This command could be used to easily build the structure of a new project using the ".
|
if (!is_null($enable)) {
|
||||||
"EesyPHP framework.")
|
self :: $core_mode = boolval($enable);
|
||||||
);
|
Hook :: trigger('cli_set_core_mode', array('enabled' => self :: $core_mode));
|
||||||
|
}
|
||||||
|
return self :: $core_mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On CLI set core mode hook
|
||||||
|
* @param \EesyPHP\HookEvent $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function on_cli_set_core_mode($event) {
|
||||||
|
if ($event->enabled) {
|
||||||
|
self :: add_command(
|
||||||
|
'new_project',
|
||||||
|
array('\\EesyPHP\\Cli', 'cli_new_project'),
|
||||||
|
I18n :: ___("Create a new project using EesyPHP framework"),
|
||||||
|
null,
|
||||||
|
I18n :: ___(
|
||||||
|
"This command could be used to easily build the structure of a new project using the ".
|
||||||
|
"EesyPHP framework.")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self :: add_command(
|
||||||
|
'serve',
|
||||||
|
array('\\EesyPHP\\Cli', 'cli_serve'),
|
||||||
|
I18n :: ___("Start the PHP built-in HTTP server to serve the application"),
|
||||||
|
null,
|
||||||
|
I18n :: ___(
|
||||||
|
"This command could be used to start the PHP built-in HTTP server to serve the ".
|
||||||
|
"application.")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -127,10 +164,12 @@ class Cli {
|
||||||
/**
|
/**
|
||||||
* Handle command line arguments
|
* Handle command line arguments
|
||||||
* @param array|null $args Command line argurment to handle (optional, default: $argv)
|
* @param array|null $args Command line argurment to handle (optional, default: $argv)
|
||||||
|
* @param bool|null $core_mode Force enable/disable EesyPHP core mode (optional, default: null)
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function handle_args($args=null) {
|
public static function handle_args($args=null, $core_mode=null) {
|
||||||
global $argv;
|
global $argv;
|
||||||
|
self :: core_mode($core_mode);
|
||||||
$args = is_array($args)?$args:array_slice($argv, 1);
|
$args = is_array($args)?$args:array_slice($argv, 1);
|
||||||
$log_level_set = false;
|
$log_level_set = false;
|
||||||
self :: $command = null;
|
self :: $command = null;
|
||||||
|
@ -244,4 +283,57 @@ class Cli {
|
||||||
echo "done. Start coding!\n";
|
echo "done. Start coding!\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command to start PHP built-in HTTP server to serve the EesyPHP project
|
||||||
|
*
|
||||||
|
* @param array $command_args The command arguments
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function cli_serve($command_args) {
|
||||||
|
if (count($command_args) > 1) {
|
||||||
|
self :: usage(
|
||||||
|
_('This command only accept one argument (the listen address in formart host:port).')
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check listen address
|
||||||
|
$listen_address = ($command_args?$command_args[0]:'127.0.0.1:8000');
|
||||||
|
$parts = explode(':', $listen_address);
|
||||||
|
if (count($parts) != 2) {
|
||||||
|
self :: usage(
|
||||||
|
_('Invalid listen address specify. Must be in formart host:port.')
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($parts[0])) {
|
||||||
|
$parts[0] = '0.0.0.0';
|
||||||
|
}
|
||||||
|
else if (!Check::ip_address($parts[0])) {
|
||||||
|
self :: usage(
|
||||||
|
_('Invalid listen host specified. Must be an IPv4 or IPv6 address.')
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Check::tcp_or_udp_port($parts[1])) {
|
||||||
|
self :: usage(
|
||||||
|
_('Invalid listen port specified. Must be a positive integer between 1 and 65535.')
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$listen_address = implode(':', $parts);
|
||||||
|
|
||||||
|
$public_html = App::get('root_directory_path')."/public_html";
|
||||||
|
chdir($public_html) or die(
|
||||||
|
sprintf(
|
||||||
|
'Fail to enter in the public_html directory of the application (%s).',
|
||||||
|
$public_html
|
||||||
|
)
|
||||||
|
);
|
||||||
|
passthru(PHP_BINARY." -S $listen_address index.php", $exit_code);
|
||||||
|
exit($exit_code);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
29
src/I18n.php
29
src/I18n.php
|
@ -319,13 +319,12 @@ class I18n {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function cli_extract_messages($command_args) {
|
public static function cli_extract_messages($command_args) {
|
||||||
$core_mode = in_array('--core', $command_args);
|
$root_path = Cli::core_mode()?self::$core_root_path:self::$root_path;
|
||||||
$root_path = $core_mode?self::$core_root_path:self::$root_path;
|
|
||||||
|
|
||||||
// Store list of generated POT files
|
// Store list of generated POT files
|
||||||
$pot_files = array();
|
$pot_files = array();
|
||||||
|
|
||||||
if ($core_mode) {
|
if (Cli::core_mode()) {
|
||||||
// List EesyPHP PHP files to parse
|
// List EesyPHP PHP files to parse
|
||||||
$eesyphp_php_files = run_external_command(
|
$eesyphp_php_files = run_external_command(
|
||||||
array(
|
array(
|
||||||
|
@ -401,8 +400,8 @@ class I18n {
|
||||||
|
|
||||||
// Extract messages from JS files using xgettext in each registered static directories
|
// Extract messages from JS files using xgettext in each registered static directories
|
||||||
foreach(Tpl::static_directories() as $idx => $static_directory) {
|
foreach(Tpl::static_directories() as $idx => $static_directory) {
|
||||||
if ($core_mode && $static_directory != Tpl::$core_static_directory) continue;
|
if (Cli::core_mode() && $static_directory != Tpl::$core_static_directory) continue;
|
||||||
if (!$core_mode && $static_directory == Tpl::$core_static_directory) continue;
|
if (!Cli::core_mode() && $static_directory == Tpl::$core_static_directory) continue;
|
||||||
// List JS files to parse
|
// List JS files to parse
|
||||||
$result = run_external_command(
|
$result = run_external_command(
|
||||||
array('find', escapeshellarg(basename($static_directory)), '-name', "'*.js'"),
|
array('find', escapeshellarg(basename($static_directory)), '-name', "'*.js'"),
|
||||||
|
@ -441,8 +440,8 @@ class I18n {
|
||||||
|
|
||||||
if (Tpl :: initialized()) {
|
if (Tpl :: initialized()) {
|
||||||
foreach (Tpl :: templates_directories() as $idx => $templates_directory) {
|
foreach (Tpl :: templates_directories() as $idx => $templates_directory) {
|
||||||
if ($core_mode && $templates_directory != Tpl::$core_templates_directory) continue;
|
if (Cli::core_mode() && $templates_directory != Tpl::$core_templates_directory) continue;
|
||||||
if (!$core_mode && $templates_directory == Tpl::$core_templates_directory) continue;
|
if (!Cli::core_mode() && $templates_directory == Tpl::$core_templates_directory) continue;
|
||||||
// Extract messages from templates files using tsmarty2c.php
|
// Extract messages from templates files using tsmarty2c.php
|
||||||
$result = run_external_command(
|
$result = run_external_command(
|
||||||
array(
|
array(
|
||||||
|
@ -502,20 +501,17 @@ class I18n {
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function cli_update_messages($command_args) {
|
public static function cli_update_messages($command_args) {
|
||||||
$core_mode = false;
|
|
||||||
$compendium_args = array();
|
$compendium_args = array();
|
||||||
foreach ($command_args as $arg) {
|
foreach ($command_args as $arg) {
|
||||||
if ($arg == '--core')
|
if (!file_exists($arg))
|
||||||
$core_mode = true;
|
|
||||||
else if (!file_exists($arg))
|
|
||||||
Log :: fatal(self::_("Compendium file %s not found."), $arg);
|
Log :: fatal(self::_("Compendium file %s not found."), $arg);
|
||||||
else {
|
else {
|
||||||
$compendium_args[] = '-C';
|
$compendium_args[] = '-C';
|
||||||
$compendium_args[] = $arg;
|
$compendium_args[] = $arg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$domain = $core_mode?self::CORE_TEXT_DOMAIN:self::TEXT_DOMAIN;
|
$domain = Cli::core_mode()?self::CORE_TEXT_DOMAIN:self::TEXT_DOMAIN;
|
||||||
$root_path = $core_mode?self::$core_root_path:self::$root_path;
|
$root_path = Cli::core_mode()?self::$core_root_path:self::$root_path;
|
||||||
|
|
||||||
$pot_file = "$root_path/messages.pot";
|
$pot_file = "$root_path/messages.pot";
|
||||||
if (!is_file($pot_file))
|
if (!is_file($pot_file))
|
||||||
|
@ -595,9 +591,8 @@ class I18n {
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function cli_compile_messages($command_args) {
|
public static function cli_compile_messages($command_args) {
|
||||||
$core_mode = in_array('--core', $command_args);
|
$domain = Cli::core_mode()?self::CORE_TEXT_DOMAIN:self::TEXT_DOMAIN;
|
||||||
$domain = $core_mode?self::CORE_TEXT_DOMAIN:self::TEXT_DOMAIN;
|
$root_path = Cli::core_mode()?self::$core_root_path:self::$root_path;
|
||||||
$root_path = $core_mode?self::$core_root_path:self::$root_path;
|
|
||||||
if ($dh = opendir($root_path)) {
|
if ($dh = opendir($root_path)) {
|
||||||
$error = False;
|
$error = False;
|
||||||
while (($file = readdir($dh)) !== false) {
|
while (($file = readdir($dh)) !== false) {
|
||||||
|
@ -676,7 +671,7 @@ class I18n {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compile messages from PO file to JS catalog file
|
// Compile messages from PO file to JS catalog file
|
||||||
$js_catalog = self :: po2json($lang, $po_file, $core_mode);
|
$js_catalog = self :: po2json($lang, $po_file, Cli::core_mode());
|
||||||
$js_file = "$root_path/$lang.js";
|
$js_file = "$root_path/$lang.js";
|
||||||
if(!$fd = fopen($js_file, 'w')) {
|
if(!$fd = fopen($js_file, 'w')) {
|
||||||
Log :: error(self::_("Fail to open %s JS catalog file in write mode (%s)."),
|
Log :: error(self::_("Fail to open %s JS catalog file in write mode (%s)."),
|
||||||
|
|
Loading…
Reference in a new issue