Skip to content

Commit

Permalink
asset url update
Browse files Browse the repository at this point in the history
  • Loading branch information
donwilson committed Jan 18, 2024
1 parent b0f3f06 commit ca31784
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
58 changes: 53 additions & 5 deletions src/Magnetar/Router/URLBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ class URLBuilder {
*/
protected string $path = '';

/**
* Path prefix
* @var string|null
*/
protected string|null $pathPrefix = null;

/**
* Path suffix
* @var string|null
*/
protected string|null $pathSuffix = null;

/**
* Named parameters (?param=value&param2=value2)
* @var array
Expand Down Expand Up @@ -160,6 +172,28 @@ public function path(string $path): self {
return $this;
}

/**
* Prefix the path
* @param string $path The path to prefix with
* @return self
*/
public function prefixPath(string|null $path=null): self {
$this->pathPrefix = $path;

return $this;
}

/**
* Suffix the path
* @param string $path The path to suffix with
* @return self
*/
public function suffixPath(string|null $path=null): self {
$this->pathSuffix = $path;

return $this;
}

/**
* Store an array of named parameters
* @param array $params Associative array of parameters
Expand Down Expand Up @@ -217,11 +251,15 @@ public function fragment(string|null $fragment): self {
*/
public function getCachedScheme(): string {
if(null === static::$cachedScheme) {
$scheme = parse_url($this->app['config']->get('app.url', ''), PHP_URL_SCHEME);

if((false === $scheme) || (null === $scheme)) {
// default to http
$scheme = $_SERVER['REQUEST_SCHEME'] ?? $_SERVER['SERVER_PROTOCOL'] ?? 'http';
if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && (('https' === $_SERVER['HTTP_X_FORWARDED_PROTO']) || ('http' === $_SERVER['HTTP_X_FORWARDED_PROTO']))) {
$scheme = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']);
} else {
//$scheme = parse_url($this->app['config']->get('app.url', ''), PHP_URL_SCHEME);
//
//if((false === $scheme) || (null === $scheme)) {
// default to http if we can't get the scheme from the environment
$scheme = $_SERVER['REQUEST_SCHEME'] ?? $_SERVER['SERVER_PROTOCOL'] ?? 'http';
//}
}

static::$cachedScheme = $scheme;
Expand Down Expand Up @@ -281,11 +319,21 @@ public function build(): string {
// add a trailing slash
$url .= '/';

// attach the path prefix
if(null !== $this->pathPrefix) {
$url .= ltrim($this->pathPrefix, '/');
}

// add the path
if('' !== $this->path) {
$url .= ltrim($this->path, '/');
}

// attach the path suffix
if(null !== $this->pathSuffix) {
$url .= $this->pathSuffix;
}

// add params
if(!empty($this->params)) {
if(false !== strpos($this->path, '?')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Magnetar/Router/URLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function to(string $path, array $params=[]): string {
/**
* Generate a URLBuilder instance from a URL
* @param string $url The URL to use
* @return URLBuilder
* @return \Magnetar\Router\URLBuilder
*/
public function from(string $url): URLBuilder {
return (new URLBuilder($this->app))->make($url);
Expand Down
6 changes: 4 additions & 2 deletions src/Magnetar/_autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ function app_path(string $rel_path=''): string {
*
* @todo implement
*/
function asset(string $rel_path=''): string {
return URL::to('/static/' . $rel_path);
function asset(string $rel_path='', array $params=[]): string {
return (string)URL::from( app('config')->get('app.asset_url') ?? app('config')->get('app.url') )
->path($rel_path)
->params($params);
}
}

Expand Down

0 comments on commit ca31784

Please sign in to comment.