From 74f24f7b25eea93e08dcb0f807a7390341bb0cc9 Mon Sep 17 00:00:00 2001 From: Danik Date: Mon, 22 Jan 2018 15:56:11 +0100 Subject: [PATCH] init --- composer.json | 24 ++++++ src/vahidov/NetteAssets/AssetMacro.php | 86 +++++++++++++++++++ .../NetteAssets/DI/AssetsExtension.php | 33 +++++++ 3 files changed, 143 insertions(+) create mode 100644 composer.json create mode 100644 src/vahidov/NetteAssets/AssetMacro.php create mode 100644 src/vahidov/NetteAssets/DI/AssetsExtension.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a28ce1d --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "vahidov/netteassets", + "description": "Nette assests", + "type": "library", + "require": { + "php": ">=7.0", + "latte/latte": "^2.4", + "nette/di": "^2.4", + "nette/utils": "^2.4" + }, + "license": "MIT", + "authors": [ + { + "name": "Danil Vahidov", + "email": "vahidov@gmail.com" + } + ], + "autoload": { + "psr-0": { + "Vahidov": "src" + } + }, + "minimum-stability": "dev" +} \ No newline at end of file diff --git a/src/vahidov/NetteAssets/AssetMacro.php b/src/vahidov/NetteAssets/AssetMacro.php new file mode 100644 index 0000000..cb47f96 --- /dev/null +++ b/src/vahidov/NetteAssets/AssetMacro.php @@ -0,0 +1,86 @@ +addMacro('asset', [$me, 'macroAsset']); + $me->addMacro('livereloadscript', [$me, 'macroLivereloadScript']); + } + + public function macroAsset(Latte\MacroNode $node, Latte\PhpWriter $writer): string + { + if ($node->modifiers && $node->modifiers!='|noescape') { + throw new Latte\CompileException('Only \'noescape\' modifier is allowed in ' . $node->getNotation()); + } + // Validate arguments count + $args = trim($node->args); + $argsCount = $args==='' ? 0 : (substr_count($args, ',') + 1); + if ($argsCount===0) { + throw new Latte\CompileException("Assets macro requires at least one argument."); + } + + return $writer->write( + 'echo ' . ($node->modifiers!=='|noescape' ? '%escape' : '') . + '(' . self::class . '::getOutputAsset(' . + '%node.word, ' . + '%node.array, ' . + '$this->global->' . self::CONFIG_PROVIDER . '))'); + } + + public static function getOutputAsset($asset, array $args, $config): string + { + if ($config['productionMode']!=true) { + return ($config['scripthost'] ? $config['scripthost'] : '//' . $_SERVER['HTTP_HOST']) . ($config['scriptport'] ? ':' . $config['scriptport'] : '') . '/' . trim($asset, '/'); + } else { + $asset = trim($asset, '/'); + if (isset($config['manifest-data'][$asset])) { + return '/' . $config['manifest-data'][$asset]; + } + return '/' . $asset; + } + } + + + public function macroLivereloadScript(Latte\MacroNode $node, Latte\PhpWriter $writer): string + { + if ($node->modifiers && $node->modifiers!='|noescape') { + throw new Latte\CompileException('Only \'noescape\' modifier is allowed in ' . $node->getNotation()); + } + return $writer->write( + 'echo ' . + '(' . self::class . '::getOutputLivereloadScript(' . '$this->global->' . self::CONFIG_PROVIDER . '))'); + } + + public static function getOutputLivereloadScript($config): string + { + if ($config['productionMode']==true) { + return ''; + } + return ''; + } + + + public static function getManifest(array $config): array + { + $path = rtrim($config['wwwDir'], '/') . '/../../' . $config['manifestFile']; + if (!is_file($path)) { + return []; + } + return Nette\Utils\Json::decode(file_get_contents($path), Nette\Utils\Json::FORCE_ARRAY); + } + +} diff --git a/src/vahidov/NetteAssets/DI/AssetsExtension.php b/src/vahidov/NetteAssets/DI/AssetsExtension.php new file mode 100644 index 0000000..ddf83e8 --- /dev/null +++ b/src/vahidov/NetteAssets/DI/AssetsExtension.php @@ -0,0 +1,33 @@ + '%appDir%', + 'wwwDir' => '%wwwDir%', + 'productionMode' => '%productionMode%', + 'manifestFile' => '', + 'manifest-data' => [], + ]; + + public function beforeCompile(): void + { + $builder = $this->getContainerBuilder(); + $config = $this->getConfig($this->defaults); + $this->defaults['manifest-data'] = AssetMacro::getManifest($config); + $config = $this->getConfig($this->defaults); + + $builder->getDefinition('latte.latteFactory') + ->addSetup("?->addProvider(?, ?)", ['@self', AssetMacro::CONFIG_PROVIDER, $config]) + ->addSetup("?->onCompile[] = function(\$engine) { " . + AssetMacro::class . "::install(\$engine->getCompiler()); }", + ['@self'] + ); + } +} \ No newline at end of file