Skip to content

@sasezaki /update_validate_message.php
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Generate Zend Framework 2 Validate messages from Validator'S messageTemplates
#!/usr/bin/env php
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
use Zend\Console;
use Zend\Loader\StandardAutoloader;
use Zend\Code\Scanner\DirectoryScanner;
/**
* Generate Validator message Templates
* php bin/update_validate_message.php -s library -l en -o Zend_Validate.php
*
* Usage:
* --help|-h Get usage message
* --library|-l [ <string> ] Library to parse; if none provided, assumes
* current directory
* --output|-o [ <string> ] Where to write autoload file; if not provided,
* assumes "autoload_classmap.php" in library directory
*/
$libPath = getenv('LIB_PATH') ? getenv('LIB_PATH') : __DIR__ . '/../library';
if (!is_dir($libPath)) {
// Try to load StandardAutoloader from include_path
if (false === (include 'Zend/Loader/StandardAutoloader.php')) {
echo "Unable to locate autoloader via include_path; aborting" . PHP_EOL;
exit(2);
}
} else {
// Try to load StandardAutoloader from library
if (false === (include $libPath . '/Zend/Loader/StandardAutoloader.php')) {
echo "Unable to locate autoloader via library; aborting" . PHP_EOL;
exit(2);
}
}
// Setup autoloading
$loader = new StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
$rules = array(
'help|h' => 'Get usage message',
'src|s-s' => 'library src to parse; if none provided, assumes current directory',
'output|o-s' => 'Where to write plugin map file; if not provided, assumes "plugin_classmap.php" in library directory',
'language|l-s' => 'language',
);
try {
$opts = new Console\Getopt($rules);
$opts->parse();
} catch (Console\Exception\RuntimeException $e) {
echo $e->getUsageMessage();
exit(2);
}
if ($opts->getOption('h')) {
echo $opts->getUsageMessage();
exit();
}
$path = $libPath;
if (array_key_exists('PWD', $_SERVER)) {
$path = $_SERVER['PWD'];
}
if (isset($opts->l)) {
$libraryPath = $opts->s;
$libraryPath = rtrim($libraryPath, '/\\') . DIRECTORY_SEPARATOR;
if (!is_dir($libraryPath)) {
echo "Invalid library directory provided" . PHP_EOL . PHP_EOL;
echo $opts->getUsageMessage();
exit(2);
}
$path = realpath($libraryPath);
}
$validatorMessage = new ZF2ValidatorMessage(array($libraryPath.'/Zend/I18n', $libraryPath.'/Zend/Validator'));
if ('en' == $lang = strtolower($opts->language)) {
$validatorMessage->makeResourceEnFile($opts->output);
} else {
$validatorMessage->makeResourceFile($lang, $opts->output);
}
class ZF2ValidatorMessage
{
const COPYRIGHT_TEMPLATE = <<<'COPYRIGHT'
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-%s Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
COPYRIGHT;
private $directories;
public function __construct(array $directories)
{
$this->directories = $directories;
}
public function makeResourceEnFile($output)
{
$translations = array();
foreach ($this->directories as $dir) {
$translateSet = static::scanMessageTemplates($dir);
foreach ($translateSet as $className => $messages) {
$translations[$className] = array_combine($messages, $messages);
}
}
ksort($translations);
$translate = rtrim($this->exportTranslations($translations));
$copyright = sprintf(static::COPYRIGHT_TEMPLATE, date('Y'));
$date = date("d.M.Y");
$dateNote = <<<DATENOTE
/**
* EN-Revision: $date
*/
DATENOTE;
file_put_contents($output, "<?php\n{$copyright}\n\n{$dateNote}\nreturn array({$translate}\n);\n");
}
public function makeResourceFile($language, $output)
{
$dir = dirname(__DIR__);
$target = include_once $dir."/resources/languages/$language/Zend_Validate.php";
$translations = array();
foreach ($this->directories as $dir) {
$translateSet = static::scanMessageTemplates($dir);
foreach ($translateSet as $className => $messages) {
$translated = array();
foreach ($messages as $m) {
$translated[$m] = isset($target[$m]) ? $target[$m] : $m;
}
$translations[$className] = $translated;
}
}
ksort($translations);
$translate = rtrim($this->exportTranslations($translations));
$copyright = sprintf(static::COPYRIGHT_TEMPLATE, date('Y'));
$date = date("d.M.Y");
$dateNote = <<<DATENOTE
/**
* EN-Revision: $date
*/
DATENOTE;
file_put_contents($output, "<?php\n{$copyright}\n\n{$dateNote}\nreturn array({$translate}\n);\n");
}
protected function exportTranslations($translations)
{
$translate = "\n";
$alreadyKeyHas = array();
$skipIfAlreadyKeyHas = false;
foreach ($translations as $class_name => $messages) {
$translate .= " // $class_name"."\n";
foreach ($messages as $key => $message) {
if (in_array($key, $alreadyKeyHas)) {
if ($skipIfAlreadyKeyHas) {
continue;
}
}
$alreadyKeyHas[]= $key;
$translate .= <<<EOF
"$key" => "$message",
EOF;
$translate .= "\n";
}
$translate .= "\n";
}
return $translate;
}
public static function scanMessageTemplates($dir)
{
$scanner = new DirectoryScanner($dir);
$classes = $scanner->getClasses();
$translateSet = array();
foreach ($classes as $class) {
foreach ($class->getProperties() as $property) {
if ($property->getName() === 'messageTemplates') {
//var_dump($property->getValue());
$reflection = new ReflectionClass($class->getName());
$defaults = $reflection->getDefaultProperties();
$translateSet[$class->getName()] = $defaults['messageTemplates'];
}
}
}
return $translateSet;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.