1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270:
<?php
namespace Coast;
class Resolver implements \Coast\App\Access
{
use \Coast\App\Access\Implementation;
protected $_baseUrl;
protected $_cdnUrl;
protected $_baseDir;
protected $_router;
protected $_cacheBust;
public function __construct(array $options = array())
{
foreach ($options as $name => $value) {
if ($name[0] == '_') {
throw new \Coast\Exception("Access to '{$name}' is prohibited");
}
$this->$name($value);
}
}
public function __invoke()
{
$args = func_get_args();
if (!isset($args[0])) {
$func = array($this, 'string');
} else if (is_array($args[0])) {
$func = array($this, 'route');
} else if ($args[0] instanceof \Coast\Url) {
$func = array($this, 'url');
} else if ($args[0] instanceof \Coast\Dir) {
$func = array($this, 'dir');
} else if ($args[0] instanceof \Coast\File) {
$func = array($this, 'file');
} else if ($args[0] instanceof \Coast\Path) {
$func = array($this, 'path');
} else {
$func = array($this, 'string');
}
return call_user_func_array($func, $args);
}
public function baseUrl(\Coast\Url $baseUrl = null)
{
if (func_num_args() > 0) {
$this->_baseUrl = $baseUrl;
return $this;
}
return $this->_baseUrl;
}
public function cdnUrl(\Coast\Url $cdnUrl = null)
{
if (func_num_args() > 0) {
$this->_cdnUrl = $cdnUrl;
return $this;
}
return $this->_cdnUrl;
}
public function baseDir(\Coast\Dir $baseDir = null)
{
if (func_num_args() > 0) {
$this->_baseDir = $baseDir;
return $this;
}
return $this->_baseDir;
}
public function router(\Coast\Router $router = null)
{
if (func_num_args() > 0) {
$this->_router = $router;
return $this;
}
return $this->_router;
}
public function cacheBust(\Closure $cacheBust = null)
{
if (func_num_args() > 0) {
$this->_cacheBust = $cacheBust->bindTo($this);
return $this;
}
return $this->_cacheBust;
}
public function string($string = null, $base = true)
{
$path = (string) $string;
return new \Coast\Url($base
? $this->_baseUrl . $path
: $path);
}
public function data($string = null, $mimeType = null, $base64 = false)
{
if ($string instanceof File) {
$string = $string->readAll();
}
$url = "data:{$mimeType}";
if ($base64) {
$string = base64_encode($string);
$url .= ";base64";
} else {
$string = rawurlencode($string);
}
$url .= ",{$string}";
return new \Coast\Url($url);
}
public function route(array $params = array(), $name = null, $reset = false, $base = true)
{
if (!isset($this->_router)) {
throw new Resolver\Exception("Router has not been set");
}
$route = isset($this->req)
? $this->req->param('route')
: null;
if (!isset($name)) {
if (!isset($route)) {
throw new Resolver\Exception("Route not specified and no previous route is avaliable");
}
$name = $route['name'];
}
if (!$reset && isset($route)) {
$params = array_merge(
$route['params'],
$params
);
}
$path = ltrim($this->_router->reverse($name, $params), '/');
return new \Coast\Url($base
? $this->_baseUrl . $path
: $path);
}
public function routeData(array $params = array(), $name = null, $reset = false)
{
if (!isset($this->_router)) {
throw new Resolver\Exception("Router has not been set");
}
$route = isset($this->req)
? $this->req->param('route')
: null;
if (!isset($name)) {
if (!isset($route)) {
throw new Resolver\Exception("Route not specified and no previous route is avaliable");
}
$name = $route['name'];
}
if (!$reset && isset($route)) {
$params = array_merge(
$route['params'],
$params
);
}
$data = $this->_router->reverseData($name, $params);
return $data;
}
public function url($url)
{
$url = !$url instanceof \Coast\Url
? new \Coast\Url("{$url}")
: $url;
return $url;
}
public function dir($dir, $base = true, $cdn = true, $cacheBust = true)
{
$dir = !$dir instanceof \Coast\Dir
? new \Coast\Dir("{$dir}")
: $dir;
return $this->path($dir, $base, $cdn, $cacheBust);
}
public function file($file, $base = true, $cdn = true, $cacheBust = true)
{
$file = !$file instanceof \Coast\File
? new \Coast\File("{$file}")
: $file;
return $this->path($file, $base, $cdn, $cacheBust);
}
public function path($path, $base = true, $cdn = true, $cacheBust = true)
{
if (!isset($this->_baseDir)) {
throw new Resolver\Exception("Base directory has not been set");
}
$path = !$path instanceof \Coast\Path
? new \Coast\Path("{$path}")
: $path;
$class = get_class($path);
$path = $path->isRelative()
? new $class("{$this->_baseDir}/{$path}")
: $path;
$path = $path->toReal();
$url = $path->toRelative($this->_baseDir);
$url = implode('/', array_map('rawurlencode', explode('/', $url)));
if ($base) {
if ($cdn && isset($this->_cdnUrl)) {
$url = $this->_cdnUrl . $url;
} else {
$url = $this->_baseUrl . $url;
}
}
$url = new \Coast\Url($url);
if ($cacheBust && isset($this->_cacheBust) && $path instanceof \Coast\File && $path->isReadable()) {
call_user_func($this->_cacheBust, $url, $path);
}
return $url;
}
public function query(array $params = array(), $reset = false)
{
$url = new \Coast\Url();
$url->queryParams($this->_parseQueryParams($params, $reset));
return $url;
}
public function queryInputs(array $params = array(), $reset = false)
{
$params = $this->_parseQueryParams($params, $reset);
$inputs = array();
foreach ($params as $name => $value) {
$inputs[] = '<input type="hidden" name="' . $name . '" value="' . $value . '">';
}
return implode($inputs);
}
protected function _parseQueryParams(array $params = array(), $reset = false)
{
if (!$reset && isset($this->req)) {
$params = \Coast\array_merge_smart(
$this->req->queryParams(),
$params
);
}
return \Coast\array_filter_null_recursive($params);
}
public function fragment($string)
{
$url = new \Coast\Url();
$url->fragment($string);
return $url;
}
}