From 078e2d15bf03e88e41855fd3db30b014eb8d64b3 Mon Sep 17 00:00:00 2001 From: Hash Date: Sat, 13 Oct 2012 16:55:55 +0100 Subject: [PATCH 1/2] Support for PHPTal parsing and MySQL storage --- CHANGELOG | 6 ++ examples/test1.php | 8 ++- examples/test1/wow.tal | 12 ++++ lib/Grammatista.class.php | 5 +- lib/Grammatista/Parser/Pcre.class.php | 14 ++++ lib/Grammatista/Parser/Pcre/Phptal.class.php | 46 ++++++++++++ lib/Grammatista/Storage/MySQL.class.php | 42 +++++++++++ lib/Grammatista/Storage/Pdo.class.php | 76 +++++++------------- lib/Grammatista/Storage/SQLite.class.php | 40 +++++++++++ 9 files changed, 195 insertions(+), 54 deletions(-) create mode 100644 examples/test1/wow.tal create mode 100644 lib/Grammatista/Parser/Pcre/Phptal.class.php create mode 100644 lib/Grammatista/Storage/MySQL.class.php create mode 100644 lib/Grammatista/Storage/SQLite.class.php diff --git a/CHANGELOG b/CHANGELOG index 42f21b3..31688f0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,12 @@ Grammatista Changelog ===================== +0.1.2 (October ?, 2012) +------------------------ + +ADD: Support for PHPTal (Hash) +ADD: Support for MySQL storage (Hash) + 0.1.1 (February ?, 2009) ------------------------ diff --git a/examples/test1.php b/examples/test1.php index 8afa7b8..388ba18 100644 --- a/examples/test1.php +++ b/examples/test1.php @@ -29,8 +29,14 @@ ), ), )); +Grammatista::registerParser('agphptal', array( + 'class' => 'GrammatistaParserPcrePhptal', + 'options' => array( + 'pcre.transform' => array("' . dquote . '" => '"') + ) +)); -Grammatista::setStorage(new GrammatistaStoragePdo(array('pdo.dsn' => 'sqlite:' . dirname(__FILE__) . '/' . $_SERVER['REQUEST_TIME'] . '.sqlite'))); +Grammatista::setStorage(new GrammatistaStorageSQLite(array('pdo.dsn' => 'sqlite:' . dirname(__FILE__) . '/' . $_SERVER['REQUEST_TIME'] . '.sqlite'))); $logger = new GrammatistaLoggerShell(); Grammatista::registerEventResponder('grammatista.parser.parsed', array($logger, 'log')); diff --git a/examples/test1/wow.tal b/examples/test1/wow.tal new file mode 100644 index 0000000..feda818 --- /dev/null +++ b/examples/test1/wow.tal @@ -0,0 +1,12 @@ +
+ +

+ +

There is ${php:count(foo) . ' ' . tm.__('lol', 'lolz', 1, 'tal.baz')} in here

+ + + +

\ No newline at end of file diff --git a/lib/Grammatista.class.php b/lib/Grammatista.class.php index d839aeb..57db6d4 100644 --- a/lib/Grammatista.class.php +++ b/lib/Grammatista.class.php @@ -14,7 +14,7 @@ */ class Grammatista { - const VERSION_NUMBER = '0.1.1'; + const VERSION_NUMBER = '0.1.2'; const VERSION_STATUS = 'dev'; /** @@ -29,6 +29,7 @@ class Grammatista 'GrammatistaParserDwoo' => 'Grammatista/Parser/Dwoo.class.php', 'GrammatistaParserPcre' => 'Grammatista/Parser/Pcre.class.php', 'GrammatistaParserPcreSmarty' => 'Grammatista/Parser/Pcre/Smarty.class.php', + 'GrammatistaParserPcrePhptal' => 'Grammatista/Parser/Pcre/Phptal.class.php', 'GrammatistaParserPcreSmartySlv3' => 'Grammatista/Parser/Pcre/Smarty/Slv3.class.php', 'GrammatistaParserPhp' => 'Grammatista/Parser/Php.class.php', 'GrammatistaParserPhpAgavi' => 'Grammatista/Parser/Php/Agavi.class.php', @@ -39,6 +40,8 @@ class Grammatista 'GrammatistaScannerFilesystemRecursivedirectoryiterator' => 'Grammatista/Scanner/Filesystem/Recursivedirectoryiterator.class.php', 'GrammatistaStorage' => 'Grammatista/Storage.class.php', 'GrammatistaStoragePdo' => 'Grammatista/Storage/Pdo.class.php', + 'GrammatistaStorageSQLite' => 'Grammatista/Storage/SQLite.class.php', + 'GrammatistaStorageMySQL' => 'Grammatista/Storage/MySQL.class.php', 'GrammatistaTranslatable' => 'Grammatista/Translatable.class.php', 'GrammatistaTranslatablePdo' => 'Grammatista/Translatable/Pdo.class.php', 'GrammatistaValueholder' => 'Grammatista/Valueholder.class.php', diff --git a/lib/Grammatista/Parser/Pcre.class.php b/lib/Grammatista/Parser/Pcre.class.php index 2a14a59..e69fd40 100644 --- a/lib/Grammatista/Parser/Pcre.class.php +++ b/lib/Grammatista/Parser/Pcre.class.php @@ -68,6 +68,8 @@ public function parse(GrammatistaEntity $entity) if(!$this->validate($key, $value)) { $problem = true; // var_dump('problem!'); + } else { + $info[$key] = $this->transform($value); } } @@ -106,6 +108,18 @@ protected function findLine($content, $offset) return preg_match_all('/$/m', substr($content, 0, $offset), $matches); } + protected function transform($value) + { + if(isset($this->options['pcre.transform']) && is_array($this->options['pcre.transform'])) { + foreach($this->options['pcre.transform'] as $search => $replace) + { + $value = str_replace($search, $replace, $value); + } + } + + return $value; + } + abstract protected function validate($name, $value); } diff --git a/lib/Grammatista/Parser/Pcre/Phptal.class.php b/lib/Grammatista/Parser/Pcre/Phptal.class.php new file mode 100644 index 0000000..6b91754 --- /dev/null +++ b/lib/Grammatista/Parser/Pcre/Phptal.class.php @@ -0,0 +1,46 @@ +options['pcre.comment_pattern'])) { + $this->options['pcre.comment_pattern'] = '/\)]+?)\s*-->(?!.*?php:.*?tm\._)/s'; + } + + if(!isset($this->options['pcre.patterns'])) { + $this->options['pcre.patterns'] = array( + '/php:.*?tm\._\((["\'](?P.+?)["\'])([,\s]+["\'](?P.+?)["\'])/ms' => true, + '/php:.*?tm\.__\((["\'](?P.+?)["\'])([,\s]+["\'](?P.+?)["\'])([,\s]+.+?)([,\s]+["\'](?P.+?)["\'])/ms' => true, + ); + } + } + + public function handles(GrammatistaEntity $entity) + { + $retval = $entity->type == 'tal'; + + if($retval) { + Grammatista::dispatchEvent('grammatista.parser.handles', array('entity' => $entity)); + } + + return $retval; + } + + protected function validate($name, $value) + { + switch($name) { + case 'singular_message': + case 'plural_message': + return preg_match('/[\{\}]/', $value) == 0; + case 'domain': + return preg_match('/\$/', $value) == 0; + case 'comment': + return true; + } + } +} + +?> \ No newline at end of file diff --git a/lib/Grammatista/Storage/MySQL.class.php b/lib/Grammatista/Storage/MySQL.class.php new file mode 100644 index 0000000..799781d --- /dev/null +++ b/lib/Grammatista/Storage/MySQL.class.php @@ -0,0 +1,42 @@ +options['pdo.init_queries'] as $query) { + $this->connection->query($query); + } + + // TODO: make configurable + $this->connection->exec(' + DROP TABLE IF EXISTS translatables; + CREATE TABLE translatables( + item_name TEXT, + line INTEGER, + domain TEXT, + singular_message TEXT, + plural_message TEXT, + comment TEXT, + parser_name TEXT + ) COLLATE utf8_unicode_ci + '); + + $this->connection->exec(' + DROP TABLE IF EXISTS warnings; + CREATE TABLE warnings( + item_name TEXT, + line INTEGER, + domain TEXT, + singular_message TEXT, + plural_message TEXT, + comment TEXT, + parser_name TEXT + ) COLLATE utf8_unicode_ci + '); + } +} + +?> \ No newline at end of file diff --git a/lib/Grammatista/Storage/Pdo.class.php b/lib/Grammatista/Storage/Pdo.class.php index 8f3837c..f06807e 100644 --- a/lib/Grammatista/Storage/Pdo.class.php +++ b/lib/Grammatista/Storage/Pdo.class.php @@ -1,60 +1,32 @@ options['pdo.dsn'] = null; - $this->options['pdo.username'] = null; - $this->options['pdo.password'] = null; - $this->options['pdo.driver_options'] = array(); - $this->options['pdo.attributes'] = array(); - $this->options['pdo.init_queries'] = array(); - $this->options['pdo.translatable_class_name'] = 'GrammatistaTranslatablePdo'; - $this->options['pdo.translatable_class_ctorargs'] = array(); - - parent::__construct($options); - - // TODO: checks <: - if(!isset($this->options['pdo.attributes'][PDO::ATTR_ERRMODE])) { - $this->options['pdo.attributes'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; - } - - $this->connection = new PDO($this->options['pdo.dsn'], $this->options['pdo.username'], $this->options['pdo.password'], $this->options['pdo.driver_options']); - foreach((array)$this->options['pdo.attributes'] as $attribute => $value) { - $this->connection->setAttribute($attribute, $value); - } - foreach((array)$this->options['pdo.init_queries'] as $query) { - $this->connection->executeQuery($query); - } - - // TODO: make configurable - $this->connection->exec(' - CREATE TABLE translatables( - item_name TEXT, - line INTEGER, - domain TEXT, - singular_message TEXT, - plural_message TEXT, - comment TEXT, - parser_name TEXT - ) - '); - - $this->connection->exec(' - CREATE TABLE warnings( - item_name TEXT, - line INTEGER, - domain TEXT, - singular_message TEXT, - plural_message TEXT, - comment TEXT, - parser_name TEXT - ) - '); - } + public function __construct(array $options = array()) + { + $this->options['pdo.dsn'] = null; + $this->options['pdo.username'] = null; + $this->options['pdo.password'] = null; + $this->options['pdo.driver_options'] = array(); + $this->options['pdo.attributes'] = array(); + $this->options['pdo.init_queries'] = array(); + $this->options['pdo.translatable_class_name'] = 'GrammatistaTranslatablePdo'; + $this->options['pdo.translatable_class_ctorargs'] = array(); + + parent::__construct($options); + + // TODO: checks <: + if(!isset($this->options['pdo.attributes'][PDO::ATTR_ERRMODE])) { + $this->options['pdo.attributes'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; + } + + $this->connection = new PDO($this->options['pdo.dsn'], $this->options['pdo.username'], $this->options['pdo.password'], $this->options['pdo.driver_options']); + foreach((array)$this->options['pdo.attributes'] as $attribute => $value) { + $this->connection->setAttribute($attribute, $value); + } + } public function readTranslatables($unique = true, $order = 'domain') { diff --git a/lib/Grammatista/Storage/SQLite.class.php b/lib/Grammatista/Storage/SQLite.class.php new file mode 100644 index 0000000..631513d --- /dev/null +++ b/lib/Grammatista/Storage/SQLite.class.php @@ -0,0 +1,40 @@ +options['pdo.init_queries'] as $query) { + $this->connection->executeQuery($query); + } + + // TODO: make configurable + $this->connection->exec(' + CREATE TABLE translatables( + item_name TEXT, + line INTEGER, + domain TEXT, + singular_message TEXT, + plural_message TEXT, + comment TEXT, + parser_name TEXT + ) + '); + + $this->connection->exec(' + CREATE TABLE warnings( + item_name TEXT, + line INTEGER, + domain TEXT, + singular_message TEXT, + plural_message TEXT, + comment TEXT, + parser_name TEXT + ) + '); + } +} + +?> \ No newline at end of file From 6ba377f49526a293b1d95a2322047548f40b8ddf Mon Sep 17 00:00:00 2001 From: Hash Date: Sat, 17 Nov 2012 16:52:38 +0000 Subject: [PATCH 2/2] Simplified MySQL support --- lib/Grammatista/Parser/Pcre.class.php | 4 ++-- lib/Grammatista/Storage/MySQL.class.php | 6 +----- lib/Grammatista/Storage/Pdo.class.php | 6 +++++- lib/Grammatista/Storage/SQLite.class.php | 6 +----- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/Grammatista/Parser/Pcre.class.php b/lib/Grammatista/Parser/Pcre.class.php index e69fd40..8c3a823 100644 --- a/lib/Grammatista/Parser/Pcre.class.php +++ b/lib/Grammatista/Parser/Pcre.class.php @@ -110,8 +110,8 @@ protected function findLine($content, $offset) protected function transform($value) { - if(isset($this->options['pcre.transform']) && is_array($this->options['pcre.transform'])) { - foreach($this->options['pcre.transform'] as $search => $replace) + if(isset($this->options['pcre.transform'])) { + foreach((array)$this->options['pcre.transform'] as $search => $replace) { $value = str_replace($search, $replace, $value); } diff --git a/lib/Grammatista/Storage/MySQL.class.php b/lib/Grammatista/Storage/MySQL.class.php index 799781d..a3f02f2 100644 --- a/lib/Grammatista/Storage/MySQL.class.php +++ b/lib/Grammatista/Storage/MySQL.class.php @@ -5,11 +5,7 @@ class GrammatistaStorageMySQL extends GrammatistaStoragePdo public function __construct(array $options = array()) { parent::__construct($options); - - foreach((array)$this->options['pdo.init_queries'] as $query) { - $this->connection->query($query); - } - + // TODO: make configurable $this->connection->exec(' DROP TABLE IF EXISTS translatables; diff --git a/lib/Grammatista/Storage/Pdo.class.php b/lib/Grammatista/Storage/Pdo.class.php index f06807e..f66e5f8 100644 --- a/lib/Grammatista/Storage/Pdo.class.php +++ b/lib/Grammatista/Storage/Pdo.class.php @@ -25,7 +25,11 @@ public function __construct(array $options = array()) $this->connection = new PDO($this->options['pdo.dsn'], $this->options['pdo.username'], $this->options['pdo.password'], $this->options['pdo.driver_options']); foreach((array)$this->options['pdo.attributes'] as $attribute => $value) { $this->connection->setAttribute($attribute, $value); - } + } + + foreach((array)$this->options['pdo.init_queries'] as $query) { + $this->connection->query($query); + } } public function readTranslatables($unique = true, $order = 'domain') diff --git a/lib/Grammatista/Storage/SQLite.class.php b/lib/Grammatista/Storage/SQLite.class.php index 631513d..81377a5 100644 --- a/lib/Grammatista/Storage/SQLite.class.php +++ b/lib/Grammatista/Storage/SQLite.class.php @@ -5,11 +5,7 @@ class GrammatistaStorageSQLite extends GrammatistaStoragePdo public function __construct(array $options = array()) { parent::__construct($options); - - foreach((array)$this->options['pdo.init_queries'] as $query) { - $this->connection->executeQuery($query); - } - + // TODO: make configurable $this->connection->exec(' CREATE TABLE translatables(