Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Generate Zend Framework 2 Validate messages from Validator'S messageTemplates

View update_validate_message.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
#!/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.