Skip to content

Commit

Permalink
Forming the AssetManager class
Browse files Browse the repository at this point in the history
  • Loading branch information
woxxy committed Dec 20, 2012
1 parent 528af60 commit b9cb869
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ The Theme object abstracts the content of the theme. These will be returned by t

Returns a new builder. It's used to create the HTML.

* __$theme->createAssetManager()__
* __$theme->getAssetManager()__

Returns a new asset manager. It's used to load and edit the content of the package that the user will download, like stylesheets and images.
Returns the asset manager. It's used to load and edit the content of the package that the user will download, like stylesheets and images.

#### Builder

Expand Down Expand Up @@ -136,10 +136,10 @@ __It's compulsory to override the `toString()` method (not the `__toString()` ma

* __$view->build()__

Builds _and cached_ the HTML. Returns the HTML.
Builds _and caches_ the HTML. Returns the HTML.

* __$view->doBuild()__

Builds the HTML and stores it in a variable. Rebuilds the HTML every time it's called.

__CHAINABLE__
_CHAINABLE_
15 changes: 0 additions & 15 deletions bootstrap.php

This file was deleted.

144 changes: 144 additions & 0 deletions classes/Foolz/Theme/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,149 @@

class AssetManager
{
/**
* The theme creating this object
*
* @var \Foolz\Theme\Theme|null
*/
protected $theme = null;

/**
* The directory where the files should be put so they are reachable via an URL
*
* @var string
*/
protected $public_dir = "";

/**
* The modification time of the public dir
*
* @var int
*/
protected $public_dir_mtime = 0;

/**
* The base URL where the theme files can be found at
*
* @var string
*/
protected $base_url = "";

/**
* Create a new instance of the asset manager
*
* @param \Foolz\Theme\Theme $theme The reference to the theme creating this asset manager
* @param string $public_dir The directory where files can be accessed via URL
* @param string $base_url The URL that points to the public directory
*
* @return \Foolz\Theme\AssetManager
*/
public function __construct(\Foolz\Theme\Theme $theme, $public_dir, $base_url)
{
$this->theme = $theme;
$this->public_dir = $public_dir;
$this->public_dir_mtime = filemtime($this->getPublicDir());
$this->base_url = $base_url;

// load the assets
if ( ! file_exists($this->getPublicDir()))
{
$this->loadAssets();
}

// reload the assets if the assets directory is more recent
if (filemtime($this->getTheme()->getDir().'assets') > $this->public_dir_mtime)
{
$this->clearAssets();
$this->loadAssets();
}
}

/**
* Returns the Theme object that created this instance of AssetManager
*
* @return \Foolz\Theme\Theme|null The Theme object that created this instance of AssetManager
*/
public function getTheme()
{
return $this->theme;
}

/**
* Returns the path to the directory where the public files get loaded
*
* @return string The path
*/
protected function getPublicDir()
{
return $this->public_dir.$this->getTheme()->getConfig('name').'/';
}

/**
* Returns an URL to the asset being requested
*
* @param $path $path The relative path to the asset to link to
*
* @return string The full URL to the asset
*/
public function getAssetLink($path)
{
return $this->base_url.$this->getTheme()->getConfig('name').'/'.$path;
}

/**
* Loads all the asset files from the theme folder
*/
protected function loadAssets()
{
copy($this->getTheme()->getDir().'assets', $this->getPublicDir());
}

/**
* Clears all the files in the public theme directory
*
* @return \Foolz\Theme\AssetManager The current object
*/
public function clearAssets()
{
static::flushDir($this->getPublicDir());

return $this;
}

/**
* Empties a directory
*
* @param string $path The directory to empty
*/
protected static function flushDir($path)
{
$fp = opendir($path);

while (false !== ($file = readdir($fp)))
{
// Remove '.', '..'
if (in_array($file, array('.', '..')))
{
continue;
}

$filepath = $path.'/'.$file;

if (is_dir($filepath))
{
static::flushDir($filepath);

// removing dir here won't remove the root dir, just as we want it
rmdir($filepath);
continue;
}
elseif (is_file($filepath))
{
unlink($filepath);
}
}

closedir($fp);
}
}
6 changes: 3 additions & 3 deletions classes/Foolz/Theme/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public function createBuilder()
}

/**
* Returns a new global Asset object
* Returns an AssetManager object to deal with the assets
*
* @return \Foolz\Theme\AssetManager A new instance of the AssetManager
*/
public function createAssetManager()
public function getAssetManager()
{
return new AssetManager($this);
return new AssetManager($this, "", "");
}

/**
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b9cb869

Please sign in to comment.