Skip to content
This repository has been archived by the owner on Aug 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #8 from nextras/new_nette
Browse files Browse the repository at this point in the history
Updated for Nette 2.4
  • Loading branch information
hrach authored Jul 22, 2016
2 parents 3dba13f + ad72325 commit d3ba6dd
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/composer.lock
/vendor
/tests/temp
/tests/output
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
language: php
php:
- 5.6
- 7.0

matrix:
fast_finish: true

sudo: false

cache:
directories:
- $HOME/.composer/cache
- vendor

before_script:
# Install Nette Tester
- phpenv config-rm xdebug.ini
- if [ "$TRAVIS_PHP_VERSION" == "7.0" ]; then composer require --dev jakub-onderka/php-parallel-lint:~0.9.2; fi
- travis_retry composer update --no-interaction --prefer-source

script:
- ./vendor/bin/tester ./tests/
- if [ "$TRAVIS_PHP_VERSION" == "7.0" ]; then vendor/bin/parallel-lint src; fi

after_failure:
# Print *.actual content & log content
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
- for i in $(find tests -name \*.log); do echo "--- $i"; cat $i; echo; echo; done
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
## Nextras\Latte\Macros
Nextras LatteMacros
===================

Useful Latte Macros for Nette Framework.
[![Build Status](https://travis-ci.org/nextras/latte-macros.svg?branch=master)](https://travis-ci.org/nextras/latte-macros)
[![Downloads this Month](https://img.shields.io/packagist/dm/nextras/latte-macros.svg?style=flat)](https://packagist.org/packages/nextras/latte-macros)
[![Stable version](http://img.shields.io/packagist/v/nextras/latte-macros.svg?style=flat)](https://packagist.org/packages/nextras/latte-macros)

## Installation
Useful Macros for Latte from Nette Framework.

The best way to install is using [Composer](http://getcomposer.org/):
### Installation

```sh
Use composer:

```bash
$ composer require nextras/latte-macros
```

## Macros
### Macros

- **RedefineMacro** - block macro such as `{define}` macro, but the latest defined macro is used.

### License

MIT. See full [license](license.md).
29 changes: 15 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
{
"name": "nextras/latte-macros",
"type": "library",
"description": "Latte macros for Nette Framework.",
"keywords": ["nextras", "nette", "latte", "macros", "block-macros"],
"license": ["MIT"],
"description": "Macros for Latte from Nette Framework.",
"keywords": ["nextras", "nette", "latte", "macros", "redefine"],
"homepage": "https://github.com/nextras/latte-macros",
"license": "MIT",
"authors": [
{
"name": "Jan Skrasek",
"email": "[email protected]",
"homepage": "http://jan.skrasek.com"
}
{ "name": "Nextras Project", "homepage": "https://github.com/nextras/orm/graphs/contributors" }
],
"require": {
"php": ">=5.3",
"latte/latte": "~2.2"
"php": ">=5.6",
"latte/latte": "~2.4"
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
}
"require-dev": {
"tracy/tracy": "~2.4",
"nette/tester": "~1.7"
},
"autoload": {
"psr-4": { "Nextras\\Latte\\Macros\\": "src/" }
},
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
}
}
}
3 changes: 2 additions & 1 deletion LICENSE → license.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License
===============

Copyright (c) 2013 Jan Skrasek
Copyright (c) 2014 Nextras Project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
113 changes: 62 additions & 51 deletions src/RedefineMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,99 @@
*
* @license MIT
* @link https://github.com/nextras/latte-macros
* @author Jan Skrasek
*/

namespace Nextras\Latte\Macros;

use Latte\CompileException;
use Latte\Compiler;
use Latte\MacroNode;
use Latte\Macros\MacroSet;
use Latte\Macros\BlockMacros;
use Latte\PhpWriter;


class RedefineMacro extends MacroSet
class RedefineMacro extends BlockMacros
{
/** @var array */
private $namedBlocks = array();
private $redefinedBlocks = [];


public static function install(Compiler $compiler)
{
$me = new static($compiler);
$me->addMacro('redefine', array($me, 'macroRedefine'), array($me, 'macroRedefineEnd'));
return $me;
$me->addMacro('redefine', [$me, 'macroRedefine'], [$me, 'macroRedefineEnd']);
}


/**
* {redefine [#]name}
*/
public function macroRedefine(MacroNode $node, PhpWriter $writer)
public function initialize()
{
parent::initialize();
$this->redefinedBlocks = [];
}


public function finalize()
{
$name = $node->tokenizer->fetchWord();
$node->data->name = $name = ltrim($name, '#');
$compiler = $this->getCompiler();
$properties = $compiler->getProperties();
$blocks = isset($properties['blocks']) ? $properties['blocks'] : [];
$blockTypes = isset($properties['blockTypes']) ? $properties['blockTypes'] : [];

if (isset($this->namedBlocks[$name])) {
throw new CompileException("Cannot redeclare static block '$name'");
list($prolog) = parent::finalize();

if (isset($compiler->getProperties()['blocks'])) {
foreach ($compiler->getProperties()['blocks'] as $key => $val) {
$blocks[$key] = $val;
}
}
if (isset($compiler->getProperties()['blockTypes'])) {
foreach ($compiler->getProperties()['blockTypes'] as $key => $val) {
$blockTypes[$key] = $val;
}
}
$compiler->addProperty('blocks', $blocks);
$compiler->addProperty('blockTypes', $blockTypes);

$this->namedBlocks[$name] = TRUE;
if (!$this->redefinedBlocks) {
return [$prolog];
}

$prolog = $this->namedBlocks ? '' : "if (\$_l->extends) { ob_end_clean(); return Latte\\Macros\\CoreMacros::includeTemplate(\$_l->extends, get_defined_vars(), \$template)->render(); }\n";
return $prolog;
$compiler = $this->getCompiler();
$compiler->addProperty('redefinedBlocks', $this->redefinedBlocks);
return [
$prolog . '
$blocksToPrepend = [];
foreach ($this->redefinedBlocks as $redefinedBlock) {
foreach ($this->blockQueue[$redefinedBlock] as $i => $callback) {
if ($callback[0] === $this) {
$blocksToPrepend[] = [$redefinedBlock, $i];
}
}
}
foreach ($blocksToPrepend as $blockToPrepend) {
$cb = $this->blockQueue[$blockToPrepend[0]][$blockToPrepend[1]];
unset($this->blockQueue[$blockToPrepend[0]][$blockToPrepend[1]]);
array_unshift($this->blockQueue[$blockToPrepend[0]], $cb);
}
'
];
}


/**
* {/redefine}
*/
public function macroRedefineEnd(MacroNode $node, PhpWriter $writer)
public function macroRedefine(MacroNode $node, PhpWriter $writer)
{
if (empty($node->data->leave)) {
$this->namedBlocks[$node->data->name] = $tmp = rtrim(ltrim($node->content, "\n"), " \t");
$node->content = substr_replace($node->content, $node->openingCode . "\n", strspn($node->content, "\n"), strlen($tmp));
$node->openingCode = "<?php ?>";
}
$node->name = 'define';
$result = parent::macroBlock($node, $writer);
$node->name = 'redefine';
$this->redefinedBlocks[] = $node->data->name;
return $result;
}


/**
* Finishes template parsing.
* @return array(prolog, epilog)
*/
public function finalize()
public function macroRedefineEnd(MacroNode $node, PhpWriter $writer)
{
$prolog = array();

if ($this->namedBlocks) {
foreach ($this->namedBlocks as $name => $code) {
$func = '_lb' . substr(md5($this->getCompiler()->getTemplateId() . $name), 0, 10) . '_' . preg_replace('#[^a-z0-9_]#i', '_', $name);
$prolog[] = "//\n// block $name\n//\n"
. "\$_internal = isset(\$_b) ? \$_b : \$_l;"
. "\$_block = &\$_internal->blocks[" . var_export($name, TRUE) . "];"
. "\$_block = \$_block === NULL ? array() : \$_block;array_unshift(\$_block, '$func');"
. "if (!function_exists('$func')) { "
. "function $func(\$_b, \$_args) { \$_l = \$_b; extract(\$_args)"
. "\n?>$code<?php\n}}";
}
$prolog[] = "//\n// end of blocks\n//";
}
$this->namedBlocks = array();

return array(implode("\n\n", $prolog), "");
$node->name = 'define';
$result = parent::macroBlockEnd($node, $writer);
$node->name = 'redefine';
return $result;
}

}
30 changes: 30 additions & 0 deletions tests/RedefineMacroTests.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Latte\Engine;
use Nextras\Latte\Macros\RedefineMacro;
use Tester\Assert;
use Tester\Environment;


require_once __DIR__ . '/../vendor/autoload.php';
Environment::setup();


function render($file)
{
$latte = new Latte\Engine;
$latte->setTempDirectory(__DIR__ . '/temp');
$latte->onCompile[] = function (Engine $engine) {
RedefineMacro::install($engine->getCompiler());
};
$result = $latte->renderToString(__DIR__ . '/' . $file);
return trim($result);
}



Assert::same('import.latte', render('case1/main.latte'));
Assert::same('main.latte', render('case2/main.latte'));
Assert::same('import.latte', render('case3/main.latte'));
Assert::same('import.latte', render('case4/main.latte'));
Assert::same('No blocks', render('case5/main.latte'));
1 change: 1 addition & 0 deletions tests/case1/import.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{redefine main}import.latte{/redefine}
3 changes: 3 additions & 0 deletions tests/case1/main.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{import import.latte}
{define main}main.latte{/define}
{include main}
1 change: 1 addition & 0 deletions tests/case2/import.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{redefine main}import.latte{/redefine}
3 changes: 3 additions & 0 deletions tests/case2/main.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{import import.latte}
{redefine main}main.latte{/redefine}
{include main}
1 change: 1 addition & 0 deletions tests/case3/import.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{redefine main}import.latte{/redefine}
3 changes: 3 additions & 0 deletions tests/case3/main.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{redefine main}main.latte{/redefine}
{import import.latte}
{include main}
1 change: 1 addition & 0 deletions tests/case4/import.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{redefine main}import.latte{/redefine}
2 changes: 2 additions & 0 deletions tests/case4/layout.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{define main}layout.latte{/define}
{include content}
6 changes: 6 additions & 0 deletions tests/case4/main.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{layout layout.latte}
{redefine main}main.latte{/redefine}
{block content}
{import import.latte}
{include main}
{/block}
1 change: 1 addition & 0 deletions tests/case5/main.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No blocks

0 comments on commit d3ba6dd

Please sign in to comment.