diff --git a/.gitignore b/.gitignore index d24ce90..ba35a22 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ desktop.ini node_modules/ public/ assets/ -webpack.config.js \ No newline at end of file +webpack.config.js +tests/template-compile/ \ No newline at end of file diff --git a/composer.json b/composer.json index feded22..abaec66 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "like/json": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^5.0 || ^9.0" + "phpunit/phpunit": "^5.0 || ^9.0", + "smarty/smarty": "^3.1" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index d69fe5a..68572d8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7ed36d2e1997d170bf10cd54c92c0dde", + "content-hash": "f0a4129477eafcfc5ea5753127843155", "packages": [ { "name": "like/encoding", @@ -1395,6 +1395,69 @@ }, "time": "2016-10-03T07:35:21+00:00" }, + { + "name": "smarty/smarty", + "version": "v3.1.39", + "source": { + "type": "git", + "url": "https://github.com/smarty-php/smarty.git", + "reference": "e27da524f7bcd7361e3ea5cdfa99c4378a7b5419" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/smarty-php/smarty/zipball/e27da524f7bcd7361e3ea5cdfa99c4378a7b5419", + "reference": "e27da524f7bcd7361e3ea5cdfa99c4378a7b5419", + "shasum": "" + }, + "require": { + "php": ">=5.2" + }, + "require-dev": { + "phpunit/phpunit": "^7.5 || ^6.5 || ^5.7 || ^4.8", + "smarty/smarty-lexer": "^3.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "libs/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0" + ], + "authors": [ + { + "name": "Monte Ohrt", + "email": "monte@ohrt.com" + }, + { + "name": "Uwe Tews", + "email": "uwe.tews@googlemail.com" + }, + { + "name": "Rodney Rehm", + "email": "rodney.rehm@medialize.de" + } + ], + "description": "Smarty - the compiling PHP template engine", + "homepage": "http://www.smarty.net", + "keywords": [ + "templating" + ], + "support": { + "forum": "http://www.smarty.net/forums/", + "irc": "irc://irc.freenode.org/smarty", + "issues": "https://github.com/smarty-php/smarty/issues", + "source": "https://github.com/smarty-php/smarty/tree/v3.1.39" + }, + "time": "2021-02-17T21:57:51+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.19.0", diff --git a/src/ReadEntrypoints.php b/src/ReadEntrypoints.php index 43e131d..ffb2dff 100644 --- a/src/ReadEntrypoints.php +++ b/src/ReadEntrypoints.php @@ -14,7 +14,7 @@ class ReadEntrypoints { /** * @var array */ - private $entrypoints; + private $entrypoints = []; const PUBLIC_ASSETS = 'public/build'; const ENTRYPOINTS = 'entrypoints.json'; @@ -27,6 +27,10 @@ private function __construct($path) { public function read() { $file = $this->getRaiz() . '/../' . $this->path . '/' . self::ENTRYPOINTS; + if (! file_exists($file)) { + return; + } + $json = file_get_contents($file); $this->entrypoints = Decode::decode($json); } @@ -37,6 +41,14 @@ private function getRaiz() { } public function getEntrypoint($type, $name='app') { + if (! isset($this->entrypoints['entrypoints'])) { + return []; + } + + if (! isset($this->entrypoints['entrypoints'][$name])) { + return []; + } + return $this->entrypoints['entrypoints'][$name][$type]; } diff --git a/src/SmartyTemplateIntegration.php b/src/SmartyTemplateIntegration.php new file mode 100644 index 0000000..21f5dfc --- /dev/null +++ b/src/SmartyTemplateIntegration.php @@ -0,0 +1,16 @@ +registerPlugin('function', 'webpack_encore_css', function ($params) use ($readEntrypoints) { + return $readEntrypoints->getCssTags($params['name'] ?: 'app'); + }); + $smarty->registerPlugin('function', 'webpack_encore_js', function ($params) use ($readEntrypoints) { + return $readEntrypoints->getJsTags($params['name'] ?: 'app'); + }); + } +} diff --git a/tests/ReadEntrypointsErrorsTest.php b/tests/ReadEntrypointsErrorsTest.php new file mode 100644 index 0000000..9339b0f --- /dev/null +++ b/tests/ReadEntrypointsErrorsTest.php @@ -0,0 +1,26 @@ +assertEquals([], $readEntrypoints->getCss()); + $this->assertEquals([], $readEntrypoints->getJs()); + } + + public function testEmpty() { + $readEntrypoints = ReadEntrypoints::get('tests/empty'); + $this->assertEquals([], $readEntrypoints->getCss()); + $this->assertEquals([], $readEntrypoints->getJs()); + } + + public function testEmpty2() { + $readEntrypoints = ReadEntrypoints::get('tests/empty2'); + $this->assertEquals([], $readEntrypoints->getCss()); + $this->assertEquals([], $readEntrypoints->getJs()); + } +} diff --git a/tests/ReadEntrypointsTest.php b/tests/ReadEntrypointsTest.php index a855b8c..879e922 100644 --- a/tests/ReadEntrypointsTest.php +++ b/tests/ReadEntrypointsTest.php @@ -13,7 +13,7 @@ class ReadEntrypointsTest extends TestCase { private $instance; public function setUp() { - $this->instance = ReadEntrypoints::get('tests'); + $this->instance = ReadEntrypoints::get('tests/valid'); } public function testInstance() { diff --git a/tests/SmartyTemplateIntegrationTest.php b/tests/SmartyTemplateIntegrationTest.php new file mode 100644 index 0000000..d69ac9a --- /dev/null +++ b/tests/SmartyTemplateIntegrationTest.php @@ -0,0 +1,28 @@ +addTemplateDir(__DIR__ . '/./template/'); + $smarty->compile_dir = __DIR__ . '/./template-compile/'; + $this->assertEquals(' + + Teste + + + + + +', $smarty->fetch('index.tpl')); + } +} diff --git a/tests/empty/entrypoints.json b/tests/empty/entrypoints.json new file mode 100644 index 0000000..dfc832c --- /dev/null +++ b/tests/empty/entrypoints.json @@ -0,0 +1,8 @@ +{ + "entrypoints": { + "app": { + "js": [], + "css": [] + } + } +} \ No newline at end of file diff --git a/tests/empty2/entrypoints.json b/tests/empty2/entrypoints.json new file mode 100644 index 0000000..22d2195 --- /dev/null +++ b/tests/empty2/entrypoints.json @@ -0,0 +1,5 @@ +{ + "entrypoints": { + "app": {} + } +} \ No newline at end of file diff --git a/tests/template/index.tpl b/tests/template/index.tpl new file mode 100644 index 0000000..19df756 --- /dev/null +++ b/tests/template/index.tpl @@ -0,0 +1,9 @@ + + + Teste + {webpack_encore_css} + + + {webpack_encore_js} + + \ No newline at end of file diff --git a/tests/entrypoints.json b/tests/valid/entrypoints.json similarity index 100% rename from tests/entrypoints.json rename to tests/valid/entrypoints.json