first upload all files
This commit is contained in:
13
Modules/Core/Foundation/Asset/AssetNotFoundException.php
Normal file
13
Modules/Core/Foundation/Asset/AssetNotFoundException.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset;
|
||||
|
||||
use Exception;
|
||||
|
||||
class AssetNotFoundException extends Exception
|
||||
{
|
||||
public static function make($asset)
|
||||
{
|
||||
return new static("Asset [$asset] not found.");
|
||||
}
|
||||
}
|
||||
49
Modules/Core/Foundation/Asset/Manager/AssetManager.php
Normal file
49
Modules/Core/Foundation/Asset/Manager/AssetManager.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset\Manager;
|
||||
|
||||
interface AssetManager
|
||||
{
|
||||
/**
|
||||
* Add a new asset.
|
||||
*
|
||||
* @param string $dependency
|
||||
* @param string $path
|
||||
* @return void
|
||||
*/
|
||||
public function addAsset($asset, $path);
|
||||
|
||||
/**
|
||||
* Get all css files.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function allCss();
|
||||
|
||||
/**
|
||||
* Get all js files.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function allJs();
|
||||
|
||||
/**
|
||||
* Get css file for the given dependency.
|
||||
*
|
||||
* @param string $dependency
|
||||
* @return string
|
||||
*
|
||||
* @throws \Modules\Core\Foundation\Asset\AssetNotFoundException
|
||||
*/
|
||||
public function getJs($dependency);
|
||||
|
||||
/**
|
||||
* Get js file for the given dependency.
|
||||
*
|
||||
* @param string $dependency
|
||||
* @return string
|
||||
*
|
||||
* @throws \Modules\Core\Foundation\Asset\AssetNotFoundException
|
||||
*/
|
||||
public function getCss($dependency);
|
||||
}
|
||||
104
Modules/Core/Foundation/Asset/Manager/FleetCartAssetManager.php
Normal file
104
Modules/Core/Foundation/Asset/Manager/FleetCartAssetManager.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset\Manager;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Modules\Core\Foundation\Asset\AssetNotFoundException;
|
||||
|
||||
class FleetCartAssetManager implements AssetManager
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $css = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $js = [];
|
||||
|
||||
/**
|
||||
* Create new instance of FleetCartAssetManager.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->css = new Collection;
|
||||
$this->js = new Collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new asset.
|
||||
*
|
||||
* @param string $dependency
|
||||
* @param string $path
|
||||
* @return void
|
||||
*/
|
||||
public function addAsset($asset, $path)
|
||||
{
|
||||
$extension = pathinfo($path, PATHINFO_EXTENSION);
|
||||
|
||||
if ($extension === 'css') {
|
||||
$collection = $this->css;
|
||||
} elseif ($extension === 'js') {
|
||||
$collection = $this->js;
|
||||
}
|
||||
|
||||
$collection->put($asset, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all css files.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function allCss()
|
||||
{
|
||||
return $this->css;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all js files.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function allJs()
|
||||
{
|
||||
return $this->js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get css file for the given dependency.
|
||||
*
|
||||
* @param string $dependency
|
||||
* @return string
|
||||
*
|
||||
* @throws \Modules\Core\Foundation\Asset\AssetNotFoundException
|
||||
*/
|
||||
public function getCss($dependency)
|
||||
{
|
||||
return tap($this->css->get($dependency), function ($asset) use ($dependency) {
|
||||
if (is_null($asset)) {
|
||||
throw AssetNotFoundException::make($dependency);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get js file for the given dependency.
|
||||
*
|
||||
* @param string $dependency
|
||||
* @return string
|
||||
*
|
||||
* @throws \Modules\Core\Foundation\Asset\AssetNotFoundException
|
||||
*/
|
||||
public function getJs($dependency)
|
||||
{
|
||||
return tap($this->js->get($dependency), function ($asset) use ($dependency) {
|
||||
if (is_null($asset)) {
|
||||
throw AssetNotFoundException::make($dependency);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
44
Modules/Core/Foundation/Asset/Pipeline/AssetPipeline.php
Normal file
44
Modules/Core/Foundation/Asset/Pipeline/AssetPipeline.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset\Pipeline;
|
||||
|
||||
interface AssetPipeline
|
||||
{
|
||||
/**
|
||||
* Return all css files to include.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function allCss();
|
||||
|
||||
/**
|
||||
* Return all js files to include.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function allJs();
|
||||
|
||||
/**
|
||||
* Add a javascript dependency on the view.
|
||||
*
|
||||
* @param string|array $assets
|
||||
* @return $this
|
||||
*/
|
||||
public function requireAssets($assets);
|
||||
|
||||
/**
|
||||
* Add an asset after another one.
|
||||
*
|
||||
* @param string $asset
|
||||
* @return void
|
||||
*/
|
||||
public function after($asset);
|
||||
|
||||
/**
|
||||
* Add an asset before another one.
|
||||
*
|
||||
* @param string $asset
|
||||
* @return void
|
||||
*/
|
||||
public function before($asset);
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset\Pipeline;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Modules\Core\Foundation\Asset\Manager\AssetManager;
|
||||
|
||||
class FleetCartAssetPipeline implements AssetPipeline
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Support\Collection
|
||||
*/
|
||||
protected $css;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Support\Collection
|
||||
*/
|
||||
protected $js;
|
||||
|
||||
/**
|
||||
* Create a new instance of FleetCartAssetPipeline.
|
||||
*
|
||||
* @param \Modules\Core\Foundation\Asset\Manager\AssetManager $assetManager
|
||||
*/
|
||||
public function __construct(AssetManager $assetManager)
|
||||
{
|
||||
$this->css = new Collection;
|
||||
$this->js = new Collection;
|
||||
$this->assetManager = $assetManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all css files to include.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function allCss()
|
||||
{
|
||||
return $this->css;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all js files to include.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function allJs()
|
||||
{
|
||||
return $this->js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Require assets for the view.
|
||||
*
|
||||
* @param string|array $assets
|
||||
* @return $this
|
||||
*/
|
||||
public function requireAssets($assets)
|
||||
{
|
||||
$assets = is_array($assets) ? $assets : func_get_args();
|
||||
|
||||
foreach ($assets as $asset) {
|
||||
$this->addAsset($asset);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add asset to css/js collection.
|
||||
*
|
||||
* @param string $asset
|
||||
* @return void
|
||||
*/
|
||||
private function addAsset($asset)
|
||||
{
|
||||
$extension = pathinfo($asset, PATHINFO_EXTENSION);
|
||||
|
||||
if ($extension === 'css') {
|
||||
$collection = $this->css;
|
||||
$assetPath = $this->assetManager->getCss($asset);
|
||||
} elseif ($extension === 'js') {
|
||||
$collection = $this->js;
|
||||
$assetPath = $this->assetManager->getJs($asset);
|
||||
}
|
||||
|
||||
$collection->put($asset, $assetPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an asset before another one.
|
||||
*
|
||||
* @param string $asset
|
||||
* @return void
|
||||
*/
|
||||
public function before($asset)
|
||||
{
|
||||
$this->insert($asset, 'before');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an asset after another one.
|
||||
*
|
||||
* @param string $asset
|
||||
* @return void
|
||||
*/
|
||||
public function after($asset)
|
||||
{
|
||||
$this->insert($asset, 'after');
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert an asset in right order.
|
||||
*
|
||||
* @param string $asset
|
||||
* @param string $offset
|
||||
*/
|
||||
private function insert($asset, $offset = 'before')
|
||||
{
|
||||
$offset = $offset === 'before' ? 0 : 1;
|
||||
|
||||
list($assets, $collection) = $this->findDependenciesForKey($asset);
|
||||
list($key, $value) = $this->getLastKeyAndValueOf($assets);
|
||||
|
||||
$pos = $this->getPositionInArray($asset, $assets);
|
||||
|
||||
$assets = array_merge(
|
||||
array_slice($assets, 0, $pos + $offset, true),
|
||||
[$key => $value],
|
||||
array_slice($assets, $pos, count($assets) - 1, true)
|
||||
);
|
||||
|
||||
$this->$collection = new Collection($assets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find in which collection the given asset exists.
|
||||
*
|
||||
* @param string $asset
|
||||
* @return array
|
||||
*/
|
||||
private function findDependenciesForKey($asset)
|
||||
{
|
||||
if ($this->css->get($asset)) {
|
||||
return [$this->css->all(), 'css'];
|
||||
}
|
||||
|
||||
return [$this->js->all(), 'js'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last key and value the given array.
|
||||
*
|
||||
* @param array $assets
|
||||
* @return array
|
||||
*/
|
||||
private function getLastKeyAndValueOf(array $assets)
|
||||
{
|
||||
$value = end($assets);
|
||||
$key = key($assets);
|
||||
|
||||
reset($assets);
|
||||
|
||||
return [$key, $value];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the position in the array of the given key.
|
||||
*
|
||||
* @param string $asset
|
||||
* @param array $assets
|
||||
* @return int
|
||||
*/
|
||||
private function getPositionInArray($asset, array $assets)
|
||||
{
|
||||
return array_search($asset, array_keys($assets), true);
|
||||
}
|
||||
}
|
||||
30
Modules/Core/Foundation/Asset/Types/Asset.php
Normal file
30
Modules/Core/Foundation/Asset/Types/Asset.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset\Types;
|
||||
|
||||
use Illuminate\Support\Facades\File;
|
||||
|
||||
abstract class Asset
|
||||
{
|
||||
protected function asset($url)
|
||||
{
|
||||
list($dirname, $name, $extension) = $this->parse($url);
|
||||
|
||||
$url = "{$dirname}/{$name}";
|
||||
|
||||
if ($extension === 'css' && is_rtl()) {
|
||||
$url .= '.rtl';
|
||||
}
|
||||
|
||||
return "{$url}.{$extension}";
|
||||
}
|
||||
|
||||
private function parse($url)
|
||||
{
|
||||
return [
|
||||
File::dirname($url),
|
||||
File::name($url),
|
||||
File::extension($url),
|
||||
];
|
||||
}
|
||||
}
|
||||
13
Modules/Core/Foundation/Asset/Types/AssetType.php
Normal file
13
Modules/Core/Foundation/Asset/Types/AssetType.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset\Types;
|
||||
|
||||
interface AssetType
|
||||
{
|
||||
/**
|
||||
* Get the URL.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function url();
|
||||
}
|
||||
27
Modules/Core/Foundation/Asset/Types/AssetTypeFactory.php
Normal file
27
Modules/Core/Foundation/Asset/Types/AssetTypeFactory.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset\Types;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class AssetTypeFactory
|
||||
{
|
||||
/**
|
||||
* @param $asset
|
||||
* @return \Modules\Core\Foundation\Asset\Types\AssetType
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function make($asset)
|
||||
{
|
||||
$typeClass = 'Modules\Core\Foundation\Asset\Types\\' . ucfirst(key($asset)) . 'Asset';
|
||||
|
||||
if (! class_exists($typeClass)) {
|
||||
throw new InvalidArgumentException("Asset Type Class [{$typeClass}] not found");
|
||||
}
|
||||
|
||||
return new $typeClass(
|
||||
$asset[key($asset)]
|
||||
);
|
||||
}
|
||||
}
|
||||
18
Modules/Core/Foundation/Asset/Types/CdnAsset.php
Normal file
18
Modules/Core/Foundation/Asset/Types/CdnAsset.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset\Types;
|
||||
|
||||
class CdnAsset implements AssetType
|
||||
{
|
||||
private $path;
|
||||
|
||||
public function __construct($path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
public function url()
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
}
|
||||
22
Modules/Core/Foundation/Asset/Types/ModuleAsset.php
Normal file
22
Modules/Core/Foundation/Asset/Types/ModuleAsset.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset\Types;
|
||||
|
||||
use Nwidart\Modules\Facades\Module;
|
||||
|
||||
class ModuleAsset extends Asset implements AssetType
|
||||
{
|
||||
private $path;
|
||||
|
||||
public function __construct($path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
public function url()
|
||||
{
|
||||
return $this->asset(
|
||||
Module::asset($this->path)
|
||||
);
|
||||
}
|
||||
}
|
||||
22
Modules/Core/Foundation/Asset/Types/ThemeAsset.php
Normal file
22
Modules/Core/Foundation/Asset/Types/ThemeAsset.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Foundation\Asset\Types;
|
||||
|
||||
use Theme;
|
||||
|
||||
class ThemeAsset extends Asset implements AssetType
|
||||
{
|
||||
private $path;
|
||||
|
||||
public function __construct($path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
public function url()
|
||||
{
|
||||
return $this->asset(
|
||||
Theme::url($this->path)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user