Skip to content

Commit

Permalink
WebAsset improve attach() process
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Sep 27, 2018
1 parent 4e5fb00 commit b3d30a8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 81 deletions.
120 changes: 40 additions & 80 deletions libraries/src/WebAsset/WebAssetItem.php
Expand Up @@ -271,22 +271,32 @@ public function getWeight()
*
* @since __DEPLOY_VERSION__
*/
public function getCSSFiles($resolvePath = true)
public function getStylesheetFiles($resolvePath = true)
{
$files = $this->css;

if ($resolvePath)
{
foreach ($files as $path => $attr)
$files = [];

foreach ($this->css as $path => $attr)
{
$resolved = $this->resolvePath($path, 'stylesheet');
$fullPath = $resolved['fullPath'];

$files[$path]['__isExternal'] = $resolved['external'];
$files[$path]['__fullPath'] = $resolved['fullPath'];
if (!$fullPath)
{
// File not found, But we keep going ???
continue;
}

$files[$fullPath] = $attr;
$files[$fullPath]['__isExternal'] = $resolved['external'];
$files[$fullPath]['__pathOrigin'] = $path;
}

return $files;
}

return $files;
return $this->css;
}

/**
Expand All @@ -298,97 +308,47 @@ public function getCSSFiles($resolvePath = true)
*
* @since __DEPLOY_VERSION__
*/
public function getJSFiles($resolvePath = true)
public function getScriptFiles($resolvePath = true)
{
$files = $this->js;

if ($resolvePath)
{
foreach ($files as $path => $attr)
{
$resolved = $this->resolvePath($path, 'script');

$files[$path]['__isExternal'] = $resolved['external'];
$files[$path]['__fullPath'] = $resolved['fullPath'];
}
}

return $files;
}

/**
* Attach active asset to the Document
*
* @param Document $doc Document for attach StyleSheet/JavaScript
*
* @return self
*
* @throws \RuntimeException If try attach inactive asset
*
* @since __DEPLOY_VERSION__
*/
public function attach(Document $doc)
{
if (!$this->isActive())
{
throw new \RuntimeException('Incative Asset cannot be attached');
}

return $this->attachCSS($doc)->attachJS($doc);
}
$files = [];

/**
* Attach StyleSheet files to the document
*
* @param Document $doc Document for attach StyleSheet/JavaScript
*
* @return self
*
* @since __DEPLOY_VERSION__
*/
protected function attachCSS(Document $doc)
{
foreach ($this->getCSSFiles(true) as $path => $attr)
{
if ($attr['__fullPath'])
foreach ($this->js as $path => $attr)
{
$file = $attr['__fullPath'];
$version = $attr['__isExternal'] ? false : 'auto';
$resolved = $this->resolvePath($path, 'script');
$fullPath = $resolved['fullPath'];

unset($attr['__fullPath'], $attr['__isExternal']);
if (!$fullPath)
{
// File not found, But we keep going ???
continue;
}

$doc->addStyleSheet($file, ['version' => $version], $attr);
$files[$fullPath] = $attr;
$files[$fullPath]['__isExternal'] = $resolved['external'];
$files[$fullPath]['__pathOrigin'] = $path;
}

return $files;
}

return $this;
return $this->js;
}

/**
* Attach JavaScript files to the document
* Return list of the asset files, and it's attributes
*
* @param Document $doc Document for attach StyleSheet/JavaScript
*
* @return self
* @return array
*
* @since __DEPLOY_VERSION__
*/
protected function attachJS(Document $doc)
public function getAssetFiles()
{
foreach ($this->getJSFiles() as $path => $attr)
{
if ($attr['__fullPath'])
{
$file = $attr['__fullPath'];
$version = $attr['__isExternal'] ? false : 'auto';

unset($attr['__fullPath'], $attr['__isExternal']);

$doc->addScript($file, ['version' => $version], $attr);
}
}

return $this;
return [
'script' => $this->getScriptFiles(true),
'stylesheet' => $this->getStylesheetFiles(true),
];
}

/**
Expand Down
22 changes: 21 additions & 1 deletion libraries/src/WebAsset/WebAssetRegistry.php
Expand Up @@ -337,7 +337,27 @@ public function attach(Document $doc)
// Attach an active assets do the document
foreach ($assets as $asset)
{
$asset->attach($doc);
$paths = $asset->getAssetFiles();

// Add StyleSheets of the asset
foreach ($paths['stylesheet'] as $path => $attr)
{
$version = $attr['__isExternal'] ? false : 'auto';

unset($attr['__pathOrigin'], $attr['__isExternal']);

$doc->addScript($path, ['version' => $version], $attr);
}

// Add Scripts of the asset
foreach ($paths['script'] as $path => $attr)
{
$version = $attr['__isExternal'] ? false : 'auto';

unset($attr['__pathOrigin'], $attr['__isExternal']);

$doc->addScript($path, ['version' => $version], $attr);
}
}

// Merge with previously added scripts
Expand Down

0 comments on commit b3d30a8

Please sign in to comment.