The making of Asset Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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