Skip to content

Instantly share code, notes, and snippets.

@brabijan
Last active December 17, 2015 22:19
Show Gist options
  • Save brabijan/5681223 to your computer and use it in GitHub Desktop.
Save brabijan/5681223 to your computer and use it in GitHub Desktop.
Simple assets manager
<?php
class AssetsManager extends Nette\Object
{
/** @var string */
private $jsPath;
/** @var string */
private $cssPath;
/** @var array */
private $js = array();
/** @var array */
private $css = array();
/** @var bool */
private $debugMode;
public function __construct($debugMode, $jsPath, $cssPath)
{
$this->debugMode = $debugMode;
$this->jsPath = $jsPath;
$this->cssPath = $cssPath;
}
public function addCss($files)
{
if (!is_array($files)) {
$files = array($files);
}
foreach ($files as $file) {
$this->css[] = $file;
}
}
public function addJs($files)
{
if (!is_array($files)) {
$files = array($files);
}
foreach ($files as $file) {
$this->js[] = $file;
}
}
public function getCss()
{
$this->validateAssets();
return $this->css;
}
public function getJs()
{
$this->validateAssets();
return $this->js;
}
private function validateAssets()
{
if (!$this->debugMode) {
return;
}
foreach ($this->css as $css) {
if (file_exists($this->cssPath . $css)) {
throw new Nette\FileNotFoundException("Style $css was not found");
}
}
foreach ($this->js as $js) {
if (file_exists($this->jsPath . $js)) {
throw new Nette\FileNotFoundException("Style $js was not found");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment