Dependencies
This commit is contained in:
11
aldarien/asset/.gitignore
vendored
Normal file
11
aldarien/asset/.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
composer.phar
|
||||
/vendor/
|
||||
|
||||
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
|
||||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
|
||||
# composer.lock
|
||||
|
||||
#Eclipse IDE
|
||||
.settings
|
||||
.buildpath
|
||||
.project
|
21
aldarien/asset/LICENSE
Normal file
21
aldarien/asset/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Aldarien
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
2
aldarien/asset/README.md
Normal file
2
aldarien/asset/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# asset
|
||||
Asset manager module for my apps
|
21
aldarien/asset/app/Contract/Asset.php
Normal file
21
aldarien/asset/app/Contract/Asset.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace App\Contract;
|
||||
|
||||
use App\Definition\Contract;
|
||||
use App\Service\Asset as AssetService;
|
||||
|
||||
class Asset
|
||||
{
|
||||
use Contract;
|
||||
|
||||
protected static function newInstance()
|
||||
{
|
||||
return new AssetService();
|
||||
}
|
||||
public static function get($identifier)
|
||||
{
|
||||
$instance = self::getInstance();
|
||||
return $instance->get($identifier);
|
||||
}
|
||||
}
|
||||
?>
|
5
aldarien/asset/app/Helper/functions.php
Normal file
5
aldarien/asset/app/Helper/functions.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
function asset($identifier) {
|
||||
return \App\Contract\Asset::get($identifier);
|
||||
}
|
||||
?>
|
88
aldarien/asset/app/Service/Asset.php
Normal file
88
aldarien/asset/app/Service/Asset.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
namespace App\Service;
|
||||
|
||||
class Asset
|
||||
{
|
||||
protected $assets;
|
||||
protected $dir;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->dir = config('locations.public');
|
||||
}
|
||||
public function get($identifier)
|
||||
{
|
||||
$asset = $this->find($identifier);
|
||||
return $asset->url;
|
||||
}
|
||||
protected function find($identifier)
|
||||
{
|
||||
$type = $this->getType($identifier);
|
||||
$asset = null;
|
||||
if ($type == false) {
|
||||
foreach (array_keys($this->assets) as $type) {
|
||||
if (($asset = $this->getAsset($identifier, $type))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$asset = $this->getAsset($identifier, $type);
|
||||
}
|
||||
return $asset;
|
||||
}
|
||||
protected function getType($identifier)
|
||||
{
|
||||
if (strpos($identifier, '.') !== false) {
|
||||
list($name, $ext) = explode('.', $identifier);
|
||||
return $ext;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
protected function getAsset($identifier, $type)
|
||||
{
|
||||
if (!isset($this->assets[$type])) {
|
||||
$this->loadAssets($type);
|
||||
}
|
||||
if (!isset($this->assets[$type])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($this->assets[$type] as $asset) {
|
||||
if ($this->compareIdentifier($asset, $identifier)) {
|
||||
return $asset;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
protected function loadAssets($type)
|
||||
{
|
||||
$dir = $this->dir . '/' . $type . '/*.' . $type;
|
||||
$files = glob($dir);
|
||||
foreach ($files as $file) {
|
||||
$url = $this->url($file);
|
||||
$identifier = pathinfo($file)['filename'];
|
||||
|
||||
$this->assets[$type] []= (object) ['identifier' => $identifier, 'url' => $url, 'type' => $type];
|
||||
}
|
||||
}
|
||||
protected function url($file)
|
||||
{
|
||||
$url = '/' . config('app.project') . '/' . str_replace('\\', '/', str_replace(realpath(config('locations.public')), '', dirname(realpath($file)))) . '/' . basename(realpath($file));
|
||||
$url = preg_replace('/\/+/', '/', $url);
|
||||
return $url;
|
||||
}
|
||||
protected function compareIdentifier($asset, $identifier)
|
||||
{
|
||||
if (strpos($identifier, '.') !== false) {
|
||||
list($name, $ext) = explode('.', $identifier);
|
||||
if ($asset->identifier == $name and $asset->type == $ext) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if ($asset->identifier == $identifier) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
26
aldarien/asset/composer.json
Normal file
26
aldarien/asset/composer.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"name" : "aldarien/asset",
|
||||
"description" : "Asset manager module for my apps",
|
||||
"type" : "library",
|
||||
"require" : {
|
||||
"aldarien/config" : "dev-master",
|
||||
"aldarien/contract" : "dev-master"
|
||||
},
|
||||
"require-dev" : {
|
||||
"phpunit/phpunit" : "^6.3"
|
||||
},
|
||||
"license" : "MIT",
|
||||
"authors" : [{
|
||||
"name" : "Aldarien",
|
||||
"email" : "aldarien85@gmail.com"
|
||||
}
|
||||
],
|
||||
"autoload" : {
|
||||
"psr-4" : {
|
||||
"App\\" : "app"
|
||||
},
|
||||
"files": [
|
||||
"app/Helper/functions.php"
|
||||
]
|
||||
}
|
||||
}
|
17
aldarien/asset/phpunit.xml
Normal file
17
aldarien/asset/phpunit.xml
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
colors="true"
|
||||
verbose="true"
|
||||
bootstrap="./vendor/autoload.php"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Application Test Suite">
|
||||
<directory>./tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>./app</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
25
aldarien/asset/tests/AssetTest.php
Normal file
25
aldarien/asset/tests/AssetTest.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AssetTest extends TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
mkdir(root() . '/public');
|
||||
mkdir(root() . '/public/css');
|
||||
config('locations.public', root() . '/public');
|
||||
file_put_contents(root() . '/public/css/style.css', 'body {color: black;}');
|
||||
}
|
||||
public function tearDown()
|
||||
{
|
||||
unlink(root() . '/public/css/style.css');
|
||||
rmdir(root() . '/public/css');
|
||||
rmdir(root() . '/public');
|
||||
}
|
||||
|
||||
public function testAsset()
|
||||
{
|
||||
$this->assertEquals(asset('style.css'), '/css/style.css');
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user