Skip to content

Fix code style #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 69 additions & 23 deletions src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* Class Assets.
*
* @author Sang Nguyen
* @since 22/07/2015 11:23 PM
*/
Expand Down Expand Up @@ -51,20 +52,24 @@ class Assets
protected $build = '';

const ASSETS_SCRIPT_POSITION_HEADER = 'header';

const ASSETS_SCRIPT_POSITION_FOOTER = 'footer';

/**
* Assets constructor.
*
* @author Sang Nguyen
* @param Repository $config
* @param HtmlBuilder $htmlBuilder
* @param Repository $config
* @param HtmlBuilder $htmlBuilder
*/
public function __construct(Repository $config, HtmlBuilder $htmlBuilder)
{
$this->config = $config->get('assets');

$this->htmlBuilder = $htmlBuilder;

$this->scripts = $this->config['scripts'];

$this->styles = $this->config['styles'];

$this->build = $this->config['enable_version'] ? '?v='.$this->config['version'] : '';
Expand All @@ -73,7 +78,7 @@ public function __construct(Repository $config, HtmlBuilder $htmlBuilder)
/**
* Add scripts to current module.
*
* @param array $assets
* @param array $assets
* @return $this
* @author Sang Nguyen
*/
Expand All @@ -82,6 +87,7 @@ public function addScripts($assets)
if (! is_array($assets)) {
$assets = [$assets];
}

$this->scripts = array_merge($this->scripts, $assets);

return $this;
Expand All @@ -90,7 +96,7 @@ public function addScripts($assets)
/**
* Add Css to current module.
*
* @param array $assets
* @param array $assets
* @return $this
* @author Sang Nguyen
*/
Expand All @@ -99,13 +105,16 @@ public function addStyles($assets)
if (! is_array($assets)) {
$assets = [$assets];
}

$this->styles = array_merge($this->styles, $assets);

return $this;
}

/**
* @param $assets
* Add styles directly.
*
* @param array|string $assets
* @return $this
* @author Sang Nguyen
*/
Expand All @@ -114,11 +123,13 @@ public function addStylesDirectly($assets)
if (! is_array($assets)) {
$assets = func_get_args();
}

foreach ($assets as &$item) {
$item = $item.$this->build;

if (! in_array($item, $this->appendedStyles)) {
$this->appendedStyles[] = [
'src' => $item,
'src' => $item,
'attributes' => [],
];
}
Expand All @@ -128,8 +139,10 @@ public function addStylesDirectly($assets)
}

/**
* @param $assets
* @param string $location
* Add scripts directly.
*
* @param string|array $assets
* @param string $location
* @return $this
* @author Sang Nguyen
*/
Expand All @@ -141,6 +154,7 @@ public function addScriptsDirectly($assets, $location = self::ASSETS_SCRIPT_POSI

foreach ($assets as &$item) {
$item = $item.$this->build;

if (! in_array($item, $this->appendedScripts[$location])) {
$this->appendedScripts[$location][] = [
'src' => $item,
Expand All @@ -155,7 +169,7 @@ public function addScriptsDirectly($assets, $location = self::ASSETS_SCRIPT_POSI
/**
* Remove Css to current module.
*
* @param array $assets
* @param array $assets
* @return $this
* @author Sang Nguyen
*/
Expand All @@ -164,6 +178,7 @@ public function removeStyles($assets)
if (! is_array($assets)) {
$assets = [$assets];
}

foreach ($assets as $rem) {
unset($this->styles[array_search($rem, $this->styles)]);
}
Expand All @@ -174,7 +189,7 @@ public function removeStyles($assets)
/**
* Add scripts.
*
* @param array $assets
* @param array $assets
* @return $this
* @author Sang Nguyen
*/
Expand All @@ -183,6 +198,7 @@ public function removeScripts($assets)
if (! is_array($assets)) {
$assets = [$assets];
}

foreach ($assets as $rem) {
unset($this->scripts[array_search($rem, $this->scripts)]);
}
Expand All @@ -193,14 +209,16 @@ public function removeScripts($assets)
/**
* Get All scripts in current module.
*
* @param string $location : top or bottom
* @param string $location `top` or `bottom`
* @return array
* @author Sang Nguyen
*/
public function getScripts($location = null)
{
$scripts = [];

$this->scripts = array_unique($this->scripts);

foreach ($this->scripts as $script) {
$configName = 'resources.scripts.'.$script;

Expand All @@ -223,7 +241,7 @@ public function getScripts($location = null)
/**
* Get All CSS in current module.
*
* @param array $lastModules : append last CSS to current module
* @param array $lastModules Append last CSS to current module
* @return array
* @author Sang Nguyen
*/
Expand All @@ -235,13 +253,18 @@ public function getStyles($lastModules = [])
}

$this->styles = array_unique($this->styles);

foreach ($this->styles as $style) {
$configName = 'resources.styles.'.$style;

if (array_has($this->config, $configName)) {
$src = array_get($this->config, $configName.'.src.local');

$attributes = array_get($this->config, $configName.'.attributes', []);

if (array_get($this->config, $configName.'.use_cdn') && ! $this->config['offline']) {
$src = array_get($this->config, $configName.'.src.cdn');

$attributes = [];
}

Expand All @@ -258,7 +281,10 @@ public function getStyles($lastModules = [])
}

/**
* @param $name
* Convert script to html.
*
* @param string $name
* @return string|null
* @author Sang Nguyen
*/
public function scriptToHtml($name)
Expand All @@ -267,7 +293,9 @@ public function scriptToHtml($name)
}

/**
* @param $name
* Convert style to html.
*
* @param string $name
* @author Sang Nguyen
*/
public function styleToHtml($name)
Expand All @@ -276,19 +304,24 @@ public function styleToHtml($name)
}

/**
* Render assets to header.
*
* @return string
* @throws \Throwable
* @author Sang Nguyen
*/
public function renderHeader()
{
$styles = $this->getStyles(['core']);

$headScripts = $this->getScripts(self::ASSETS_SCRIPT_POSITION_HEADER);

return view('assets::header', compact('styles', 'headScripts'))->render();
}

/**
* Render assets to footer.
*
* @return string
* @throws \Throwable
* @author Sang Nguyen
Expand All @@ -301,6 +334,8 @@ public function renderFooter()
}

/**
* Get script item.
*
* @param $location
* @param $configName
* @param $script
Expand All @@ -309,13 +344,18 @@ public function renderFooter()
protected function getScriptItem($location, $configName, $script)
{
$scripts = [];

$src = array_get($this->config, $configName.'.src.local');

$cdn = false;

$attributes = array_get($this->config, $configName.'.attributes', []);

if (array_get($this->config, $configName.'.use_cdn') && ! $this->config['offline']) {
$src = array_get($this->config, $configName.'.src.cdn');

$cdn = true;

$attributes = [];
}

Expand All @@ -332,9 +372,9 @@ protected function getScriptItem($location, $configName, $script)
}

if (empty($src) &&
$cdn && $location === self::ASSETS_SCRIPT_POSITION_HEADER &&
array_has($this->config, $configName.'.fallback')
) {
$cdn &&
$location === self::ASSETS_SCRIPT_POSITION_HEADER &&
array_has($this->config, $configName.'.fallback')) {
$scripts[] = $this->getFallbackScript($src, $configName);
}

Expand All @@ -343,22 +383,25 @@ protected function getScriptItem($location, $configName, $script)

/**
* Fallback to local script if CDN fails.
* @param $src
* @param $configName
*
* @param string $src
* @param string $configName
* @return array
*/
protected function getFallbackScript($src, $configName)
{
return [
'src' => $src,
'fallback' => array_get($this->config, $configName.'.fallback'),
'src' => $src,
'fallback' => array_get($this->config, $configName.'.fallback'),
'fallbackURL' => array_get($this->config, $configName.'.src.local'),
];
}

/**
* @param $name
* @param string $type
* Convert item to html.
*
* @param string $name
* @param string $type
* @return null|string
*/
protected function itemToHtml($name, $type = 'style')
Expand All @@ -368,12 +411,14 @@ protected function itemToHtml($name, $type = 'style')
}

$config = 'resources.styles.'.$name;

if ($type === 'script') {
$config = 'resources.scripts.'.$name;
}

if (array_has($this->config, $config)) {
$src = array_get($this->config, $config.'.src.local');

if (array_get($this->config, $config.'.use_cdn') && ! $this->config['offline']) {
$src = array_get($this->config, $config.'.src.cdn');
}
Expand All @@ -383,6 +428,7 @@ protected function itemToHtml($name, $type = 'style')
}

$html = '';

foreach ($src as $item) {
$html .= $this->htmlBuilder->{$type}($item.$this->build, ['class' => 'hidden'])->toHtml();
}
Expand Down
1 change: 1 addition & 0 deletions src/Facades/AssetsFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* Class AssetsFacade.
*
* @author Sang Nguyen
* @since 22/07/2015 11:25 PM
*/
Expand Down
1 change: 1 addition & 0 deletions src/Providers/AssetsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* Class AssetsServiceProvider.
*
* @author Sang Nguyen
* @since 22/07/2015 11:23 PM
*/
Expand Down
13 changes: 7 additions & 6 deletions tests/Unit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function tearDown()
Mockery::close();
}

public function testGetJavascript()
public function testGetScripts()
{
$this->assets
->shouldReceive('getJavascript')
->shouldReceive('getScripts')
->once()
->andReturn([
[
Expand All @@ -35,16 +35,17 @@ public function testGetJavascript()
],
]);

$response = $this->assets->getJavascript();
$response = $this->assets->getScripts();

$this->assertNotEmpty($response);

$this->assertEmpty($response[0]['attributes']);
}

public function testGetStylesheets()
public function testGetStyles()
{
$this->assets
->shouldReceive('getStylesheets')
->shouldReceive('getStyles')
->once()
->andReturn([
[
Expand All @@ -56,7 +57,7 @@ public function testGetStylesheets()
],
]);

$response = $this->assets->getStylesheets();
$response = $this->assets->getStyles();

$this->assertNotEmpty($response);
$this->assertNotEmpty($response[0]['attributes']);
Expand Down