Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidov committed Jan 22, 2018
0 parents commit 74f24f7
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
24 changes: 24 additions & 0 deletions composer.json
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"
}
86 changes: 86 additions & 0 deletions src/vahidov/NetteAssets/AssetMacro.php
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);
}

}
33 changes: 33 additions & 0 deletions src/vahidov/NetteAssets/DI/AssetsExtension.php
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']
);
}
}

0 comments on commit 74f24f7

Please sign in to comment.