Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Migrate to Twig ~3.0 - Add global variables from View::shared - Remove Laravel events * Fix signature inconsistencies * Add Laravel Events support Fix phpcs issues * correct some formatting issues * Use the same php requirements as Twig3
- Loading branch information
Showing
33 changed files
with
241 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
language: php | ||
|
||
php: | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
- hhvm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* This file is part of the TwigBridge package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace TwigBridge\Extension\Laravel; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use TwigBridge\NodeVisitor\LaravelEventNodeVisitor; | ||
|
||
/** | ||
* Send Laravel View Events | ||
*/ | ||
class Event extends AbstractExtension | ||
{ | ||
|
||
public function getNodeVisitors() | ||
{ | ||
return [ | ||
new LaravelEventNodeVisitor(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
|
||
namespace TwigBridge\Extension\Loader; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\Extension\GlobalsInterface; | ||
|
||
/** | ||
* Add 'app' and all global variables shared through View::share | ||
*/ | ||
class Globals extends AbstractExtension implements GlobalsInterface | ||
{ | ||
|
||
public function getGlobals(): array | ||
{ | ||
$globals = app('view')->getShared(); | ||
$globals['app'] = app(); | ||
return $globals; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
|
||
namespace TwigBridge\Node; | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\View\View; | ||
use Twig\Compiler; | ||
use Twig\Node\Node; | ||
|
||
class EventNode extends Node | ||
{ | ||
|
||
public function compile(Compiler $compiler): void | ||
{ | ||
$compiler | ||
->write( | ||
sprintf( | ||
'$context = ' . | ||
EventNode::class . '::triggerLaravelEvents($this->getTemplateName(), $context);' | ||
) | ||
) | ||
->raw("\n"); | ||
} | ||
|
||
public static function triggerLaravelEvents(string $templateName, array &$context): array | ||
{ | ||
if (Str::endsWith($templateName, '.twig')) { | ||
$templateName = Str::substr($templateName, 0, mb_strlen($templateName) - 5); | ||
} | ||
/** @var \Illuminate\View\Factory $factory */ | ||
$env = resolve('view'); | ||
$viewName = $templateName; | ||
|
||
$view = new View( | ||
$env, | ||
$env->getEngineResolver()->resolve('twig'), | ||
$viewName, | ||
null, | ||
$context | ||
); | ||
$env->callCreator($view); | ||
$env->callComposer($view); | ||
return $view->getData(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* This file is part of the TwigBridge package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace TwigBridge\NodeVisitor; | ||
|
||
use LogicException; | ||
use Twig\Environment; | ||
use Twig\Node\ModuleNode; | ||
use Twig\Node\Node; | ||
use Twig\NodeVisitor\NodeVisitorInterface; | ||
use TwigBridge\Node\EventNode; | ||
|
||
class LaravelEventNodeVisitor implements NodeVisitorInterface | ||
{ | ||
|
||
public function enterNode(Node $node, Environment $env): Node | ||
{ | ||
if ($node instanceof ModuleNode) { | ||
try { | ||
$parentNode = $node->getNode('parent'); | ||
//https://regex101.com/r/4PQe3r/1 | ||
$isEmbedded = !!preg_match('/$\s*{%\s*embed/m', $parentNode->getSourceContext()->getCode()); | ||
} catch (LogicException $e) { | ||
$isEmbedded = false; | ||
} | ||
if (!$isEmbedded) { | ||
$node->setNode('display_start', new Node([new EventNode(), $node->getNode('display_start')])); | ||
} | ||
} | ||
return $node; | ||
} | ||
|
||
public function leaveNode(Node $node, Environment $env): ?Node | ||
{ | ||
return $node; | ||
} | ||
|
||
public function getPriority() | ||
{ | ||
return 0; | ||
} | ||
} |
Oops, something went wrong.