Skip to content

Commit

Permalink
Adding support for named routes into Sintags
Browse files Browse the repository at this point in the history
  • Loading branch information
bermi committed Jan 13, 2010
1 parent 21c58c0 commit 7720816
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions test/akelos/action_pack/cases/sintags.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function _test_sintags() {
$this->_run_from_file('sintags_test_data.txt');
}
public function test_sintags_helpers() {
AkRouterHelper::generateHelperFunctionsFor('named_route', $this->mock('AkRoute'));
$this->_run_from_file('sintags_helpers_data.txt');
}

Expand Down
18 changes: 18 additions & 0 deletions test/akelos/action_pack/fixtures/sintags_helpers_data.txt
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,21 @@
-----------------------------------

<?php echo $controller->text_helper->translate($controller->text_helper->h('<b>foo</b>')); ?>

===================================

<%= named_route_url :action => 'select_database' %>

-----------------------------------


<?php echo named_route_url( array('action' => 'select_database')); ?>

===================================

<%= named_route_path :action => 'select_database' %>

-----------------------------------


<?php echo named_route_path( array('action' => 'select_database')); ?>
30 changes: 19 additions & 11 deletions vendor/akelos/action_pack/template_engines/sintags/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class AkSintagsParser
public $_current_match;
public $_block_vars = array();
public $_errors = array();
public $_current_function_opening = null;
public $avoid_php_tags = null;
public $parsed_code = null;
public $output;
public $escape_chars = array(
Expand All @@ -32,7 +34,7 @@ public function __construct($mode = 'Text') {
$this->_last_match = '';
$this->_current_match = '';
}

public function parse($raw) {
if(empty($this->parsed_code)){
$this->_Lexer->parse($this->beforeParsing($this->_escapeChars($raw)));
Expand All @@ -53,7 +55,7 @@ public function afterParsing($parsed) {
public function ignore($match, $state) {
return true;
}



public function setHelperLoader(&$HelperLoader){
Expand Down Expand Up @@ -365,19 +367,25 @@ public function Helper($match, $state, $position = null, $is_inline_function = f
return true;
}
$method_name = trim($match," =(\n\t".$this->_SINTAGS_OPEN_HELPER_TAG);
if($helper = $this->_getHelperNameForMethod($method_name)){
$helper = $this->_getHelperNameForMethod($method_name);
$named_route = in_array($method_name, AkRouterHelper::getDefinedFunctions());
if($helper || $named_route){
$this->avoid_php_tags = !$is_inline_function && !strstr($match,'=');
$this->_current_function_opening = strlen($this->output);
if(!$this->avoid_php_tags){
$this->output .= $is_inline_function ? '' : '<?php echo ';
}
if(!strpos($helper, 'helper')){
$method_name = AkInflector::variablize($method_name);
}
if($helper == 'controller'){
$this->output .= "\$controller->$method_name(";
if($named_route){
$this->output .= "$method_name(";
}else{
$this->output .= "\$controller->{$helper}->$method_name(";
if(!strpos($helper, 'helper')){
$method_name = AkInflector::variablize($method_name);
}
if($helper == 'controller'){
$this->output .= "\$controller->$method_name(";
}else{
$this->output .= "\$controller->{$helper}->$method_name(";
}
}
return true;
}else{
Expand Down Expand Up @@ -714,7 +722,7 @@ private function _getHelperNameForMethod(&$method_name) {
return $this->available_helpers[$method_name];
}
}

private function _loadLexer(){
static $Lexer;
if(empty($Lexer)){
Expand All @@ -725,6 +733,6 @@ private function _loadLexer(){
$this->_Lexer->init($this);
}
}

}

0 comments on commit 7720816

Please sign in to comment.