You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.2 KiB
PHTML
96 lines
2.2 KiB
PHTML
4 years ago
|
<?php
|
||
|
|
||
|
namespace Artmark\Assets;
|
||
|
|
||
|
use Illuminate\Support\Facades\Blade;
|
||
|
|
||
|
/**
|
||
|
* Description of AssetRenderer
|
||
|
*
|
||
|
* @author Andrey Pokidov <pokidov@e-traffic.ru>
|
||
|
*/
|
||
|
class AssetRenderer
|
||
|
{
|
||
|
/**
|
||
|
*
|
||
|
* @var AssetRenderer
|
||
|
*/
|
||
|
private static $render = null;
|
||
|
|
||
|
private static $registered = false;
|
||
|
|
||
|
private function __construct()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
public static function register()
|
||
|
{
|
||
|
if (self::$registered) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
self::$registered = true;
|
||
|
|
||
|
Blade::directive('css_assets', function ($expression) { return '<?php echo \Artmark\Assets\AssetRenderer::instance()->showStyleSheet(' . $expression . '); ?>'; });
|
||
|
Blade::directive('js_assets', function ($expression) { return '<?php echo \Artmark\Assets\AssetRenderer::instance()->showJavaScript(' . $expression . '); ?>'; });
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @return AssetRenderer
|
||
|
*/
|
||
|
public static function instance()
|
||
|
{
|
||
|
if (is_null(self::$render)) {
|
||
|
self::$render = new AssetRenderer();
|
||
|
}
|
||
|
return self::$render;
|
||
|
}
|
||
|
|
||
|
public function showJavaScript($position)
|
||
|
{
|
||
|
return $this->show($position, Asset::TYPE_JAVASCRIPT);
|
||
|
}
|
||
|
|
||
|
public function showStyleSheet($position)
|
||
|
{
|
||
|
return $this->show($position, Asset::TYPE_CSS);
|
||
|
}
|
||
|
|
||
|
public function show($position, $type)
|
||
|
{
|
||
|
$assets = AssetManager::instance()->getAssetsFor($position, $type);
|
||
|
|
||
|
if (empty($assets)) {
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
$html = '';
|
||
|
|
||
|
foreach ($assets as $asset) {
|
||
|
$html .= $this->getAssetHtml($asset);
|
||
|
}
|
||
|
|
||
|
return $html;
|
||
|
}
|
||
|
|
||
|
private function getAssetHtml(Asset $asset)
|
||
|
{
|
||
|
if ($asset->isCss()) {
|
||
|
return $this->getCssHtml($asset);
|
||
|
}
|
||
|
|
||
|
return $this->getJavaAssetHtml($asset);
|
||
|
}
|
||
|
|
||
|
private function getCssHtml(Asset $stylesheet)
|
||
|
{
|
||
|
return '<link href="' . $stylesheet->urlWithVersion() . '" type="text/css" rel="stylesheet" />' . "\n";
|
||
|
}
|
||
|
|
||
|
private function getJavaAssetHtml(Asset $javascript)
|
||
|
{
|
||
|
return '<script src="' . $javascript->urlWithVersion() . '"></script>' . "\n";
|
||
|
}
|
||
|
}
|