Skip to content

Commit

Permalink
resolvePath method
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Sep 26, 2018
1 parent 92cb5c5 commit b188004
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 42 deletions.
156 changes: 122 additions & 34 deletions libraries/src/WebAsset/WebAssetItem.php
Expand Up @@ -105,7 +105,7 @@ class WebAssetItem
* @var array
* @since __DEPLOY_VERSION__
*/
protected $js = array();
protected $js = [];

/**
* List of StyleSheet files, ant it's attributes
Expand All @@ -114,15 +114,24 @@ class WebAssetItem
* @var array
* @since __DEPLOY_VERSION__
*/
protected $css = array();
protected $css = [];

/**
* Asset dependencies
*
* @var string[]
* @since __DEPLOY_VERSION__
*/
protected $dependencies = array();
protected $dependencies = [];

/**
* Internal use, to keep track of resolved paths
*
* @var array
*
* @since __DEPLOY_VERSION__
*/
protected $resolvePaths = [];

/**
* Class constructor
Expand All @@ -132,7 +141,7 @@ class WebAssetItem
*
* @since __DEPLOY_VERSION__
*/
public function __construct($name, array $data = array())
public function __construct($name, array $data = [])
{
$this->name = strtolower($name); // No fancy Camels or Elephants
$this->version = !empty($data['version']) ? $data['version'] : null;
Expand Down Expand Up @@ -253,6 +262,60 @@ public function getWeight()
return $this->weight;
}

/**
* Get CSS files
*
* @param boolean $resolvePath Whether need to search for real path
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public function getCSSFiles($resolvePath = true)
{
$files = $this->css;

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

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

return $files;
}

/**
* Get JS files
*
* @param boolean $resolvePath Whether need to search for real path
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public function getJSFiles($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
*
Expand Down Expand Up @@ -285,24 +348,15 @@ public function attach(Document $doc)
*/
protected function attachCSS(Document $doc)
{
foreach ($this->css as $path => $attr)
foreach ($this->getCSSFiles(true) as $path => $attr)
{
$file = $path;
$version = false;

if (!$this->isPathExternal($path))
if ($attr['__fullPath'])
{
// Get the file path
$file = HTMLHelper::_('stylesheet', $path, [
'pathOnly' => true,
'relative' => !$this->isPathAbsolute($path)
]
);
$version = 'auto';
}
$file = $attr['__fullPath'];
$version = $attr['__isExternal'] ? false : 'auto';

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

if ($file)
{
$doc->addStyleSheet($file, ['version' => $version], $attr);
}
}
Expand All @@ -321,31 +375,65 @@ protected function attachCSS(Document $doc)
*/
protected function attachJS(Document $doc)
{
foreach ($this->js as $path => $attr)
foreach ($this->getJSFiles() as $path => $attr)
{
$file = $path;
$version = false;

if (!$this->isPathExternal($path))
if ($attr['__fullPath'])
{
// Get the file path
$file = HTMLHelper::_('script', $path, [
'pathOnly' => true,
'relative' => !$this->isPathAbsolute($path)
]
);
$version = 'auto';
}
$file = $attr['__fullPath'];
$version = $attr['__isExternal'] ? false : 'auto';

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

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

return $this;
}

/**
* Resolve path
*
* @param string $path The path to resolve
* @param string $type The resolver method
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
protected function resolvePath($path, $type)
{
if (!empty($this->resolvePaths[$path]))
{
return $this->resolvePaths[$path];
}

if ($type !== 'script' && $type !== 'stylesheet')
{
throw new \UnexpectedValueException('Unexpected [type], expected "script" or "stylesheet"');
}

$file = $path;
$external = $this->isPathExternal($path);

if (!$external)
{
// Get the file path
$file = HTMLHelper::_($type, $path, [
'pathOnly' => true,
'relative' => !$this->isPathAbsolute($path)
]
);
}

$this->resolvePaths[$path] = [
'external' => $external,
'fullPath' => $file ? $file : false,
];

return $this->resolvePaths[$path];
}

/**
* Check if the Path is External
*
Expand Down
16 changes: 8 additions & 8 deletions libraries/src/WebAsset/WebAssetRegistry.php
Expand Up @@ -85,7 +85,7 @@ class WebAssetRegistry
*
* @since __DEPLOY_VERSION__
*/
protected $dataFiles = array();
protected $dataFiles = [];

/**
* Registry of available Assets
Expand All @@ -94,7 +94,7 @@ class WebAssetRegistry
*
* @since __DEPLOY_VERSION__
*/
protected $assets = array();
protected $assets = [];

/**
* Weight of the most heavier and active asset
Expand Down Expand Up @@ -308,7 +308,7 @@ public function attach(Document $doc)

// Pre-save existing Scripts, and attach them after requested assets.
$jsBackup = $doc->_scripts;
$doc->_scripts = array();
$doc->_scripts = [];

// Attach an active assets do the document
foreach ($assets as $asset)
Expand Down Expand Up @@ -400,7 +400,7 @@ protected function resolveItemDependency(WebAssetItem $asset)
*/
protected function getDependenciesForAsset(WebAssetItem $asset)
{
$assets = array();
$assets = [];

foreach ($asset->getDependencies() as $depName)
{
Expand Down Expand Up @@ -462,7 +462,7 @@ function($a, $b) use ($ask)
*
* @since __DEPLOY_VERSION__
*/
public function createAsset($name, array $data = array())
public function createAsset($name, array $data = [])
{
return new WebAssetItem($name, $data);
}
Expand Down Expand Up @@ -570,9 +570,9 @@ protected function parseDataFile($path)
}

// Keep source info
$assetSource = $data;
$assetSource['dataFile'] = $path;
unset($assetSource['assets']);
$assetSource = [
'dataFile' => $path,
];

// Prepare WebAssetItem instances
foreach ($data['assets'] as $item)
Expand Down

0 comments on commit b188004

Please sign in to comment.