Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for PHPTal parsing and MySQL storage #1

Open
wants to merge 2 commits into
base: 0.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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)
------------------------

Expand Down
8 changes: 7 additions & 1 deletion examples/test1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
12 changes: 12 additions & 0 deletions examples/test1/wow.tal
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="zomg">

<h1 tal:content="php:tm._('wow', 'tal.baz')" />

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

<!--
!!For serious?!!
<p tal:content="php:tm._('lol', 'tal.baz')" />
-->

</div>
5 changes: 4 additions & 1 deletion lib/Grammatista.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class Grammatista
{
const VERSION_NUMBER = '0.1.1';
const VERSION_NUMBER = '0.1.2';
const VERSION_STATUS = 'dev';

/**
Expand All @@ -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',
Expand All @@ -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',
Expand Down
14 changes: 14 additions & 0 deletions lib/Grammatista/Parser/Pcre.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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'])) {
foreach((array)$this->options['pcre.transform'] as $search => $replace)
{
$value = str_replace($search, $replace, $value);
}
}

return $value;
}

abstract protected function validate($name, $value);
}

Expand Down
46 changes: 46 additions & 0 deletions lib/Grammatista/Parser/Pcre/Phptal.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

class GrammatistaParserPcrePhptal extends GrammatistaParserPcre
{
public function __construct(array $options = array())
{
parent::__construct($options);

if(!isset($this->options['pcre.comment_pattern'])) {
$this->options['pcre.comment_pattern'] = '/\<!--\s*%s\s*(?P<comment>[^(-->)]+?)\s*-->(?!.*?php:.*?tm\._)/s';
}

if(!isset($this->options['pcre.patterns'])) {
$this->options['pcre.patterns'] = array(
'/php:.*?tm\._\((["\'](?P<singular_message>.+?)["\'])([,\s]+["\'](?P<domain>.+?)["\'])/ms' => true,
'/php:.*?tm\.__\((["\'](?P<singular_message>.+?)["\'])([,\s]+["\'](?P<plural_message>.+?)["\'])([,\s]+.+?)([,\s]+["\'](?P<domain>.+?)["\'])/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;
}
}
}

?>
38 changes: 38 additions & 0 deletions lib/Grammatista/Storage/MySQL.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

class GrammatistaStorageMySQL extends GrammatistaStoragePdo
{
public function __construct(array $options = array())
{
parent::__construct($options);

// 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
');
}
}

?>
76 changes: 26 additions & 50 deletions lib/Grammatista/Storage/Pdo.class.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,36 @@
<?php

class GrammatistaStoragePdo extends GrammatistaStorage
abstract class GrammatistaStoragePdo extends GrammatistaStorage
{
protected $connection = null;

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);
}
foreach((array)$this->options['pdo.init_queries'] as $query) {
$this->connection->executeQuery($query);
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);
}

// 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
)
');
}
foreach((array)$this->options['pdo.init_queries'] as $query) {
$this->connection->query($query);
}
}

public function readTranslatables($unique = true, $order = 'domain')
{
Expand Down
36 changes: 36 additions & 0 deletions lib/Grammatista/Storage/SQLite.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

class GrammatistaStorageSQLite extends GrammatistaStoragePdo
{
public function __construct(array $options = array())
{
parent::__construct($options);

// 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
)
');
}
}

?>