Skip to content

Commit

Permalink
Implement modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jun 9, 2017
1 parent eb33c89 commit b7dbde8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/Phug/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@
use Phug\Parser\NodeInterface;
// Utils
use Phug\Util\AssociativeStorage;
use Phug\Util\ModulesContainerInterface;
use Phug\Util\Partial\ModuleTrait;
use Phug\Util\Partial\OptionTrait;

class Compiler implements CompilerInterface
class Compiler implements ModulesContainerInterface, CompilerInterface
{
use ModuleTrait;
use OptionTrait;

/**
Expand Down Expand Up @@ -120,6 +123,7 @@ public function __construct(array $options = null)
'formatter_class_name' => Formatter::class,
'formatter_options' => [],
'mixins_storage_mode' => AssociativeStorage::REPLACE,
'modules' => [],
'node_compilers' => [
AssignmentListNode::class => AssignmentListCompiler::class,
AssignmentNode::class => AssignmentCompiler::class,
Expand Down Expand Up @@ -182,6 +186,9 @@ public function __construct(array $options = null)
'mixin',
$this->getOption('mixins_storage_mode')
);

$this->setExpectedModuleType(CompilerModuleInterface::class);
$this->addModules($this->getOption('modules'));
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/Phug/CompilerModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Phug;

use Phug\Util\AbstractModule;
use Phug\Util\ModulesContainerInterface;

class CompilerModule extends AbstractModule implements CompilerModuleInterface
{
public function injectCompiler(Compiler $compiler)
{
return $compiler;
}

public function plug(ModulesContainerInterface $parent)
{
parent::plug($this->injectCompiler($parent));
}
}
10 changes: 10 additions & 0 deletions src/Phug/CompilerModuleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Phug;

use Phug\Util\ModuleInterface;

interface CompilerModuleInterface extends ModuleInterface
{
public function injectCompiler(Compiler $compiler);
}
15 changes: 9 additions & 6 deletions tests/Phug/Compiler/AttributeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public function testCompile()
$this->assertCompile('<input name="&lt;a&gt;" />', 'input(name="<a>")');
$this->assertCompile([
'<img ',
'src="<?= htmlspecialchars((is_array($_pug_temp = "foo.$png") || is_object($_pug_temp) ',
'? json_encode($_pug_temp) : $_pug_temp)) ?>" ',
'src="<?= htmlspecialchars((is_array($_pug_temp = "foo.$png") || ',
'(is_object($_pug_temp) && !method_exists($_pug_temp, "__toString")) ? ',
'json_encode($_pug_temp) : strval($_pug_temp))) ?>" ',
'alt="$bar" ',
'width="<?= htmlspecialchars((is_array($_pug_temp = get_width("foo.png")) || is_object($_pug_temp) ',
'? json_encode($_pug_temp) : $_pug_temp)) ?>" ',
'width="<?= htmlspecialchars((is_array($_pug_temp = get_width("foo.png")) || ',
'(is_object($_pug_temp) && !method_exists($_pug_temp, "__toString")) ? ',
'json_encode($_pug_temp) : strval($_pug_temp))) ?>" ',
'height="30" ',
'data-ratio="0.54" ',
'data-code="16205" />',
Expand All @@ -48,8 +50,9 @@ public function testCompile()
'data-code=0x3f4d)',
]);
$this->assertCompile(
'<img src="<?= (is_array($_pug_temp = (isset($image) ? $image : \'\')) || is_object($_pug_temp) '.
'? json_encode($_pug_temp) : $_pug_temp) ?>" />',
'<img src="<?= (is_array($_pug_temp = (isset($image) ? $image : \'\')) || '.
'(is_object($_pug_temp) && !method_exists($_pug_temp, "__toString")) ? '.
'json_encode($_pug_temp) : strval($_pug_temp)) ?>" />',
'img(src!=$image)'
);
$this->assertRender(
Expand Down
28 changes: 28 additions & 0 deletions tests/Phug/CompilerModuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Phug\Test;

use Phug\Compiler;
use Phug\CompilerModule;

/**
* @coversDefaultClass Phug\CompilerModule
*/
class CompilerModuleTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers ::<public>
*/
public function testModule()
{
$copy = null;
$module = new CompilerModule();
$module->onPlug(function ($_compiler) use (&$copy) {
$copy = $_compiler;
});
$compiler = new Compiler([
'modules' => [$module],
]);
self::assertSame($compiler, $copy);
}
}

0 comments on commit b7dbde8

Please sign in to comment.