Skip to content

Commit

Permalink
fix: update custom twig tags to address Twig API changes starting to …
Browse files Browse the repository at this point in the history
…cause errors in multiple environments
  • Loading branch information
sghoweri committed Mar 7, 2019
1 parent 2276f10 commit 3e9aaff
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 102 deletions.
4 changes: 2 additions & 2 deletions packages/core-php/src/Layout/GridCellNode.php
Expand Up @@ -15,7 +15,7 @@


// Custom Wrapper Twig Tag Node
class GridCellNode extends \Twig_Node {
class GridCellNode extends \Twig\Node\Node {

public function __construct($params, $lineno = 0, $tag = null){
parent::__construct(array ('params' => $params), array (), $lineno, $tag);
Expand Down Expand Up @@ -119,7 +119,7 @@ static public function cellFunctionToCall(){
echo $rendered, PHP_EOL;
}

public function compile(\Twig_Compiler $compiler) {
public function compile(\Twig\Compiler $compiler) {
$count = count($this->getNode('params'));

$compiler->addDebugInfo($this);
Expand Down
18 changes: 9 additions & 9 deletions packages/core-php/src/Layout/GridCellTokenParser.php
Expand Up @@ -14,9 +14,9 @@


// Custom Token Parser for Wrapper component
class GridCellTokenParser extends \Twig_TokenParser {
class GridCellTokenParser extends \Twig\TokenParser\AbstractTokenParser {

public function parse(\Twig_Token $token) {
public function parse(\Twig\Token $token) {

$lineno = $token->getLine();
$stream = $this->parser->getStream();
Expand Down Expand Up @@ -58,7 +58,7 @@ public function parse(\Twig_Token $token) {
// if your endmytag can also contains params, you can uncomment this line:
// $params = array_merge($params, $this->getInlineParams($token));
// and comment this one:
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$stream->expect(\Twig\Token::BLOCK_END_TYPE);
}

return new GridCellNode(new \Twig_Node($params), $lineno, $this->getTag());
Expand All @@ -67,16 +67,16 @@ public function parse(\Twig_Token $token) {
/**
* Recovers all tag parameters until we find a BLOCK_END_TYPE ( %} )
*
* @param \Twig_Token $token
* @param \Twig\Token $token
* @return array
*/
public function getInlineParams(\Twig_Token $token) {
public function getInlineParams(\Twig\Token $token) {
$stream = $this->parser->getStream();
$params = array ();
while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
while (!$stream->test(\Twig\Token::BLOCK_END_TYPE)) {
$params[] = $this->parser->getExpressionParser()->parseExpression();
}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$stream->expect(\Twig\Token::BLOCK_END_TYPE);
return $params;
}

Expand All @@ -89,10 +89,10 @@ public function getTag() {
* Callback called at each tag name when subparsing, must return
* true when the expected end tag is reached.
*
* @param \Twig_Token $token
* @param \Twig\Token $token
* @return bool
*/
public function decideMyTagFork(\Twig_Token $token) {
public function decideMyTagFork(\Twig\Token $token) {
return $token->test(array("cell", "endcell"));
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core-php/src/Layout/GridTagNode.php
Expand Up @@ -12,7 +12,7 @@
$GLOBALS['counter'] = 0;
// Expose the D8 and Pattern Lab "create_attribute" function in case this custom Twig Tag gets loaded before the create_attribute Twig extension exists.

class GridTagNode extends \Twig_Node {
class GridTagNode extends \Twig\Node\Node {

public function __construct($params, $lineno = 0, $tag = null){
parent::__construct(array ('params' => $params), array (), $lineno, $tag);
Expand Down Expand Up @@ -102,7 +102,7 @@ static public function functionToCall(){
echo $rendered, PHP_EOL;
}

public function compile(\Twig_Compiler $compiler) {
public function compile(\Twig\Compiler $compiler) {
$count = count($this->getNode('params'));
$compiler->addDebugInfo($this);

Expand Down
18 changes: 9 additions & 9 deletions packages/core-php/src/Layout/GridTagTokenParser.php
Expand Up @@ -8,9 +8,9 @@
* @author Salem Ghoweri
*/

class GridTagTokenParser extends \Twig_TokenParser {
class GridTagTokenParser extends \Twig\TokenParser\AbstractTokenParser {

public function parse(\Twig_Token $token) {
public function parse(\Twig\Token $token) {

$lineno = $token->getLine();
$stream = $this->parser->getStream();
Expand Down Expand Up @@ -51,7 +51,7 @@ public function parse(\Twig_Token $token) {
// if your endmytag can also contains params, you can uncomment this line:
// $params = array_merge($params, $this->getInlineParams($token));
// and comment this one:
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$stream->expect(\Twig\Token::BLOCK_END_TYPE);
}

return new GridTagNode(new \Twig_Node($params), $lineno, $this->getTag());
Expand All @@ -60,16 +60,16 @@ public function parse(\Twig_Token $token) {
/**
* Recovers all tag parameters until we find a BLOCK_END_TYPE ( %} )
*
* @param \Twig_Token $token
* @param \Twig\Token $token
* @return array
*/
public function getInlineParams(\Twig_Token $token) {
public function getInlineParams(\Twig\Token $token) {
$stream = $this->parser->getStream();
$params = array ();
while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
while (!$stream->test(\Twig\Token::BLOCK_END_TYPE)) {
$params[] = $this->parser->getExpressionParser()->parseExpression();
}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$stream->expect(\Twig\Token::BLOCK_END_TYPE);
return $params;
}

Expand All @@ -82,10 +82,10 @@ public function getTag() {
* Callback called at each tag name when subparsing, must return
* true when the expected end tag is reached.
*
* @param \Twig_Token $token
* @param \Twig\Token $token
* @return bool
*/
public function decideMyTagFork(\Twig_Token $token) {
public function decideMyTagFork(\Twig\Token $token) {
return $token->test(array("grid", "endgrid"));
}
}
75 changes: 4 additions & 71 deletions packages/core-php/src/SSR/SSRTagNode.php
Expand Up @@ -13,91 +13,24 @@
// $GLOBALS['counter'] = 0;
// Expose the D8 and Pattern Lab "create_attribute" function in case this custom Twig Tag gets loaded before the create_attribute Twig extension exists.

class SSRTagNode extends \Twig_Node {
class SSRTagNode extends \Twig\Node\Node {

public function __construct($params, $lineno = 0, $tag = null){
parent::__construct(array ('params' => $params), array (), $lineno, $tag);
}

// // Loop through any non-string data paramaters passed into the component
// static public function displayRecursiveResults($arrayObject) {
// foreach($arrayObject as $key=>$value) {
// // Handle attributes objects a little differently so we can merge this directly with the inheritted attrs
// if(is_array($value) && $key == 'attributes') {
// $GLOBALS['grid_attributes_custom'][ $GLOBALS['counter'] ] = array_merge_recursive($GLOBALS['grid_attributes_custom'][ $GLOBALS['counter'] ], $value);
// } elseif (is_array($value)) {
// self::displayRecursiveResults($value);
// } elseif(is_object($value)) {
// self::displayRecursiveResults($value);
// } else {
// $GLOBALS['grid_props'][ $GLOBALS['counter'] ] = array_merge_recursive($GLOBALS['grid_props'][ $GLOBALS['counter'] ], array($key => $value));
// }
// }
// }


static public function functionToCall(){
$params = func_get_args();
$contents = array_shift($params);
// If paramaters exist for this particular component instance, merge everything together
// if ($params){
// // $new_grid_attributes = array("class" => array("")); // Store inline strings
// // $GLOBALS['counter'] = $GLOBALS['counter'] + 1; //Track the current grid instance
// // $GLOBALS['grid_props'][ $GLOBALS['counter'] ] = array(); //Store misc data that isn't attributes
// // $GLOBALS['grid_attributes_custom'][ $GLOBALS['counter'] ] = array(); // Store custom attributes passed in


// // Handle legacy way of handling data via a simple string on the component (ex. {% grid 'u-1/1' %} )
// // foreach ($params as $key => $value){
// // if (gettype($value) == 'string') {
// // // $classes = explode(" ", $value);
// // // $new_grid_attributes = array_merge_recursive(array("class" => $classes), $new_grid_attributes);
// // } elseif (gettype($value) == 'array') {
// // self::displayRecursiveResults($value);
// // } else {
// // // Catch all for everything else.
// // }
// // }
// }
// Do any custom attributes already exist? If not, set an empty array so it can get merged (below)
// if (!isset($GLOBALS['grid_attributes_custom'][ $GLOBALS['counter'] ])) {
// $GLOBALS['grid_attributes_custom'][ $GLOBALS['counter'] ] = array();
// }
// After capturing and merging in all string + array parameters on the component instance, take the unique set of data and merge it with the defaults
// $GLOBALS['grid_attributes_custom'][ $GLOBALS['counter'] ] = array_merge_recursive($GLOBALS['grid_attributes'], $GLOBALS['grid_attributes_custom'][ $GLOBALS['counter'] ]);
// Handle instances where no vanilla string parameters (not in an array) are directly added to a component
// if (isset($new_grid_attributes)){
// $merged_attributes = array_merge_recursive($GLOBALS['grid_attributes_custom'][ $GLOBALS['counter'] ], $new_grid_attributes);
// } elseif (isset($GLOBALS['grid_attributes_custom'][ $GLOBALS['counter'] ])) {
// $merged_attributes = $GLOBALS['grid_attributes_custom'][ $GLOBALS['counter'] ];
// } else {
// $merged_attributes = array();
// }
// Finally, handle instances where literally zero data or props got passed into the component
// if (!isset($GLOBALS['grid_props'][ $GLOBALS['counter'] ])){
// $GLOBALS['grid_props'][ $GLOBALS['counter'] ] = array();
// }
// // Run the captured attributes through D8's createAttribute function, prior to rendering
// $attributes = new Attribute($merged_attributes);
$params = func_get_args();
$contents = array_shift($params);

$stringLoader = new BoltStringLoader();

//Setup data into 2 groups: attributes + everything else that we're going to namespace under the component name.
// $data = array(
// "attributes" => $attributes,
// "grid" => $GLOBALS['grid_props'][ $GLOBALS['counter'] ]
// );
$twig_to_html = $stringLoader->render(array("string" => $contents, "data" => []));
$rendered_html = \Bolt\TwigFunctions::bolt_ssr($twig_to_html);

//@TODO: pull in template logic used here from external Twig file.
// $string = "{{ '" . htmlspecialchars($rendered_html) . "' }}";
// Pre-render the inline Twig template + the data we've merged and normalized
// $rendered = $stringLoader->render(array("string" => $string, "data" => []));
echo $rendered_html, PHP_EOL;
}

public function compile(\Twig_Compiler $compiler) {
public function compile(\Twig\Compiler $compiler) {
$count = count($this->getNode('params'));
$compiler->addDebugInfo($this);

Expand Down
18 changes: 9 additions & 9 deletions packages/core-php/src/SSR/SSRTagTokenParser.php
Expand Up @@ -8,9 +8,9 @@
* @author Salem Ghoweri
*/

class SSRTagTokenParser extends \Twig_TokenParser {
class SSRTagTokenParser extends \Twig\TokenParser\AbstractTokenParser {

public function parse(\Twig_Token $token) {
public function parse(\Twig\Token $token) {

$lineno = $token->getLine();
$stream = $this->parser->getStream();
Expand Down Expand Up @@ -51,7 +51,7 @@ public function parse(\Twig_Token $token) {
// if your endmytag can also contains params, you can uncomment this line:
// $params = array_merge($params, $this->getInlineParams($token));
// and comment this one:
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$stream->expect(\Twig\Token::BLOCK_END_TYPE);
}

return new SSRTagNode(new \Twig_Node($params), $lineno, $this->getTag());
Expand All @@ -60,16 +60,16 @@ public function parse(\Twig_Token $token) {
/**
* Recovers all tag parameters until we find a BLOCK_END_TYPE ( %} )
*
* @param \Twig_Token $token
* @param \Twig\Token $token
* @return array
*/
public function getInlineParams(\Twig_Token $token) {
public function getInlineParams(\Twig\Token $token) {
$stream = $this->parser->getStream();
$params = array ();
while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
while (!$stream->test(\Twig\Token::BLOCK_END_TYPE)) {
$params[] = $this->parser->getExpressionParser()->parseExpression();
}
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$stream->expect(\Twig\Token::BLOCK_END_TYPE);
return $params;
}

Expand All @@ -82,10 +82,10 @@ public function getTag() {
* Callback called at each tag name when subparsing, must return
* true when the expected end tag is reached.
*
* @param \Twig_Token $token
* @param \Twig\Token $token
* @return bool
*/
public function decideMyTagFork(\Twig_Token $token) {
public function decideMyTagFork(\Twig\Token $token) {
return $token->test(array("ssr", "endssr"));
}
}

0 comments on commit 3e9aaff

Please sign in to comment.