Skip to content

Instantly share code, notes, and snippets.

@harikt
Created June 2, 2014 17:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save harikt/24f9e14f3f3ca9dc63b1 to your computer and use it in GitHub Desktop.
The making of Asset Controller
<?php
namespace Aura\Asset_Bundle\Web;
use Aura\Web\Request;
use Aura\Web\Response;
class AssetController
{
/**
*
* A web (not HTTP!) request object.
*
* @var Request
*
*/
protected $request;
/**
*
* A web (not HTTP!) response object.
*
* @var Request
*
*/
protected $response;
/**
*
* FormatTypes
*
* @var $format_types;
*
*/
protected $format_types;
/**
*
* The Aura config modes in which we should cache web assets.
*
* @var array
*
*/
protected $cache_config_modes = [];
/**
*
* The subdirectory inside the web document root where we should cache
* web assets.
*
* @var array
*
*/
protected $web_cache_dir;
/**
*
* The path to vendor useful for testing
*
* @var string $vendor_path
*
*/
protected $vendor_path;
/**
*
* Constructor.
*
* @param Request $request A web request object.
*
* @param Response $response A web response object.
*
*/
public function __construct(
Request $request,
Response $response,
FormatTypes $format_types,
$config_mode = 'prod',
$cache_config_modes,
$web_cache_dir = dirname(dirname(dirname(dirname(dirname(__DIR__))))) . DIRECTORY_SEPRATOR . 'web',
$vendor_path = dirname(dirname(dirname(dirname(__DIR__))))
) {
$this->request = $request;
$this->response = $response;
$this->format_types = $format_types;
$this->config_mode = $config_mode;
$this->cache_config_modes = $cache_config_modes;
$this->web_cache_dir = $web_cache_dir;
$this->vendor_path = $vendor_path;
}
public function setVendorPath($vendor_path)
{
$this->vendor_path= $vendor_path;
}
/**
*
* Sets the config modes in which caching should take place.
*
* @param array $modes An array of mode names.
*
* @return void
*
*/
public function setCacheConfigModes(array $modes = [])
{
$this->cache_config_modes = $modes;
}
/**
*
* Sets the current config mode
*
* @param string $config_mode The current mode
*
* @return void
*
*/
public function setCacheConfigMode($config_mode)
{
$this->config_mode = $config_mode;
}
/**
*
* Sets the subdirectory in the web document root where web assets should
* be cached.
*
* @param string $dir
*
* @return void
*
*/
public function setWebCacheDir($dir)
{
$this->web_cache_dir = $dir;
}
public function __invoke($vendor = null, $package = null, $file = null, $format = null)
{
$fakepath = $this->vendor_path . DIRECTORY_SEPARATOR .
$vendor . DIRECTORY_SEPARATOR .
$package . "/web/{$file}{$format}";
$realpath = realpath($fakepath);
// does the asset file exist?
if (! file_exists($realpath) || ! is_readable($realpath)) {
$content = "Asset not found: "
. htmlspecialchars($fakepath, ENT_QUOTES, 'UTF-8');
$this->response->status->set('404', 'Not Found', '1.1');
$this->response->content->set($content);
return;
}
// are we in a config mode that wants us to cache?
// $this->context->getEnv('AURA_CONFIG_MODE', 'default');
if (in_array($this->config_mode, $this->cache_config_modes)) {
// copy source to this target cache location
$path = $this->web_cache_dir . DIRECTORY_SEPARATOR
. $vendor . DIRECTORY_SEPARATOR
. $package . DIRECTORY_SEPARATOR
. $file . $format;
$webcache = dirname(dirname(__DIR__)) . $path;
// make sure we have a dir for it
$dir = dirname($webcache);
if (! is_dir($dir)) {
@mkdir($dir, 0755, true);
}
// copy from the source package to the target cache dir for the
// next time this package asset is requested
copy($realpath, $webcache);
}
// open the asset file using a shared (read) lock
$fh = fopen($realpath, 'rb');
$size = filesize($realpath);
$contents = fread($fh, $size);
fclose($fh);
$this->response->headers->set('Content-Type', $this->format_types->getContentType($format));
// set the response content to the file handle
$this->response->content->set($contents);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment