-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 74f24f7
Showing
3 changed files
with
143 additions
and
0 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 |
---|---|---|
@@ -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" | ||
} |
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,86 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Vahidov\NetteAssets; | ||
|
||
use Nette; | ||
use Latte; | ||
|
||
|
||
class AssetMacro extends Latte\Macros\MacroSet | ||
{ | ||
use Nette\SmartObject; | ||
|
||
const CONFIG_PROVIDER = 'assetMacroConfig'; | ||
|
||
|
||
public static function install(Latte\Compiler $compiler) | ||
{ | ||
$me = new self($compiler); | ||
$me->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 '<script type="text/javascript" src="' . ($config['livereloadhost'] ? $config['livereloadhost'] : '//' . $_SERVER['HTTP_HOST']) . ($config['livereloadport'] ? ':' . $config['livereloadport'] : '') . '/livereload.js' . '"></script>'; | ||
} | ||
|
||
|
||
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); | ||
} | ||
|
||
} |
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,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Vahidov\NetteAssets\DI; | ||
|
||
use Nette\DI; | ||
use Vahidov\NetteAssets\AssetMacro; | ||
|
||
class AssetsExtension extends Nette\DI\CompilerExtension | ||
{ | ||
|
||
public $defaults = [ | ||
'appDir' => '%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'] | ||
); | ||
} | ||
} |