Skip to content

Commit

Permalink
asset item added
Browse files Browse the repository at this point in the history
  • Loading branch information
indpurvesh committed Oct 19, 2020
1 parent 54a0ff4 commit a1d2049
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 26 deletions.
7 changes: 3 additions & 4 deletions resources/views/css/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{{-- <link href="/admin/css/app.css" rel="stylesheet"> --}}
@foreach ($styles as $key => $style)
<link href="{{ route('admin.styles', $key) }}" rel="stylesheet">
{{-- <script src="{{ route('admin.styles', $key) }}"></script> --}}

@foreach ($styles as $style)
<link href="{{ route('admin.styles', $style->key()) }}" rel="stylesheet">
@endforeach
4 changes: 2 additions & 2 deletions resources/views/js/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@foreach ($scripts as $key => $script)
<script src="{{ route('admin.script', $key) }}"></script>
@foreach ($scripts as $script)
<script src="{{ route('admin.script', $script->key()) }}"></script>
@endforeach
50 changes: 50 additions & 0 deletions src/AssetItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace AvoRed\Assets;

use AvoRed\Assets\Support\Contracts\AssetInterface;

class AssetItem implements AssetInterface
{
/**
* Key for the asset
* @var string $path
*/
public $key;

/**
* Path for the asset
* @var string $path
*/
public $path;

/**
* Set/Get Key for the Asset Item
* @param string|null $key
* @return self|string
*/
public function key($key = null)
{
if ($key === null) {
return $this->key;
}
$this->key = $key;

return $this;
}

/**
* Set/Get path for the Asset Item
* @param string|null $path
* @return self|string
*/
public function path($path = null)
{
if ($path === null) {
return $this->path;
}
$this->path = $path;

return $this;
}
}
48 changes: 28 additions & 20 deletions src/AssetsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,44 @@ public function __construct()
$this->styles = new Collection();
}

/**
* Register Scripts for AvoRed
* @param string $key
* @param string $path
* @return void
*/
public function registerJS($key, $path)
{
$this->register(self::JS, $key, $path);
}


public function getJS($key)
{
return $this->scripts->get($key);
$index = $this->scripts->search(function($item) use ($key) {
return $item->key() === $key;
});
return $this->scripts->get($index);
}
public function getCSS($key)
{
$index = $this->styles->search(function($item) use ($key) {
return $item->key() === $key;
});
return $this->styles->get($key);
}

/**
* Register Scripts for AvoRed
* @param callable $path
* @return void
*/
public function registerJS($item)
{
$asset = new AssetItem();
$item($asset);

$this->register(self::JS, $asset);
}

/**
* Register Styles for AvoRed
* @param string $key
* @param string $path
* @param callable $item
* @return void
*/
public function registerCSS($key, $path)
public function registerCSS($item)
{
$this->register(self::CSS, $key, $path);
$asset = new AssetItem();
$item($asset);
$this->register(self::CSS, $asset);
}

/**
Expand Down Expand Up @@ -92,13 +100,13 @@ public function renderCSS(): View
* @param string $path
* @return void
*/
private function register($type, $key, $path): void
private function register($type, $item): void
{
if ($type === self::JS) {
$this->scripts->put($key, $path);
$this->scripts->push($item);
}
if ($type === self::CSS) {
$this->styles->put($key, $path);
$this->styles->push($item);
}
}
}
20 changes: 20 additions & 0 deletions src/Support/Contracts/AssetInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace AvoRed\Assets\Support\Contracts;

interface AssetInterface
{
/**
* Set/Get Key for the Asset Item
* @param string|null $key
* @return self|string
*/
public function key($key = null);

/**
* Set/Get path for the Asset Item
* @param string|null $path
* @return self|string
*/
public function path($path = null);
}

0 comments on commit a1d2049

Please sign in to comment.