yf_dynamic_assets user module| File | plugins/dynamic/modules/dynamic/yf_dynamic_assets.class.php |
|---|---|
| Use | module('dynamic_assets'), URL ?object=dynamic_assets |
A site sets them in its config with $PROJECT_CONF['dynamic_assets']['NAME'] = ...;
| Property | Default | Description |
|---|---|---|
$_parent |
null |
public function asset($type = '', $exit = true)
$exit mixed false lets a test read the headers and the body public function asset($type = '', $exit = true)
{
session_write_close();
no_graphics(true);
$type = strtolower(preg_replace('~[^a-z0-9_-]+~ims', '', trim($type ?: ($_GET['page'] ?? ''))));
$out = $this->asset_content($_GET['id'] ?? '', $type);
if ($out === null) {
_404();
if ($exit) {
exit();
}
return '';
}
$now = time();
$max_age = 3600;
if ( ! headers_sent()) {
header('Content-Type: ' . ($type === 'css' ? 'text/css' : 'text/javascript'));
header('Content-Length: ' . strlen($out));
header('Cache-Control: max-age=3600, must-revalidate');
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', $now + $max_age));
header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', $now));
header('X-Robots-Tag: noindex,nofollow,noarchive,nosnippet');
header_remove('Pragma');
header_remove('Set-Cookie');
}
echo $out;
if ($exit) {
exit;
}
return $out;
}
public function asset_content($id, $type)
Body of asset(): content of the loaded descriptor named by $id, or null when the id or the type is not valid. The id is only used as a key of the loaded descriptors, no path or URL is built from it.
$type mixed css, js, jquery or ng (the last two are js) public function asset_content($id, $type)
{
$name = strtolower(preg_replace('~[^a-z0-9_-]+~ims', '', trim(is_scalar($id) ? (string) $id : '')));
if ( ! strlen($name) || ! in_array($type, ['css', 'js', 'jquery', 'ng'], true)) {
return null;
}
$class_assets = _class('assets');
if ( ! $class_assets->get_asset_details($name)) {
return null;
}
$asset_type = $type === 'css' ? 'css' : 'js';
$content = (array) $class_assets->get_asset($name, $asset_type);
if (isset($content['content'])) {
$content = [$content];
}
$out = [];
foreach ($content as $v) {
if (is_array($v)) {
$v = $v['content'] ?? '';
}
if ( ! is_string($v) || ! strlen($v)) {
continue;
}
$ctype = $class_assets->detect_content_type($asset_type, $v);
$_out = '';
if ($ctype === 'inline') {
$_out = $v;
} elseif ($ctype === 'file') {
$_out = (string) file_get_contents($v);
} elseif ($ctype === 'url') {
$_out = (string) $class_assets->_url_get_contents(substr($v, 0, 2) === '//' ? 'https:' . $v : $v);
}
if (DEBUG_MODE) {
$out[] = '/* DEBUG: asset: ' . $name . ', type: ' . $type . ', ctype: ' . $ctype . ', length: ' . strlen($_out) . ', src: ' . str_replace('*/', '', substr($v, 0, 200)) . ' */';
}
$out[] = $_out;
}
return implode(PHP_EOL . PHP_EOL, $out);
}
public function asset_css()
public function asset_css()
{
return $this->asset('css');
}
public function asset_jquery()
public function asset_jquery()
{
return $this->asset('jquery');
}
public function asset_js()
public function asset_js()
{
return $this->asset('js');
}
public function asset_ng()
public function asset_ng()
{
return $this->asset('ng');
}
_ hooks and helpers; not routable as page actionspublic function __call($name, $arguments)
Catch missing method call.
public function __call($name, $arguments)
{
trigger_error(__CLASS__ . ': No method ' . $name, E_USER_WARNING);
return false;
}
public function __construct()
public function __construct()
{
$this->_parent = module('dynamic');
}