Skip to content

Commit

Permalink
Add a global asset() helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Feb 25, 2024
1 parent afc233d commit 08cc208
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/digging-deeper/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,26 @@ hyde()->routes()) === Hyde::routes(); // true
It's up to you if you want to use the facade or the global function, or a mix of both.
A benefit of using the global function is that it may have better IDE support.

#### `asset`

This is an alias of the `Hyde::asset()` facade method and allows you to get a relative link or URL to an asset in the media directory.

```php
asset('image.png'); // Returns a relative web link to the given image
```

Gets a relative web link to the given image stored in the `_site/media` folder.
If the image is remote (starts with http) it will be returned as is.

If `true` is passed as the second argument, and a base URL is set,
the image will be returned with a qualified absolute URL.

**Example usage:**

```blade
<img src="{{ asset('image.png') }}" alt="My image">
```

### Namespaced functions

HydePHP also comes with a functions that are under the `Hyde` namespace,
Expand Down
10 changes: 10 additions & 0 deletions packages/framework/src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ function unslash(string $string): string
return trim($string, '/\\');
}
}

if (! function_exists('asset')) {
/**
* Get a relative link or URL to an asset in the media directory.
*/
function asset(string $name, bool $preferQualifiedUrl = false): string
{
return hyde()->asset($name, $preferQualifiedUrl);
}
}
}

namespace Hyde {
Expand Down
53 changes: 53 additions & 0 deletions packages/framework/tests/Feature/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Symfony\Component\Yaml\Yaml;
use Hyde\Support\Facades\Render;

/**
* Covers the helpers in helpers.php.
Expand Down Expand Up @@ -64,6 +65,58 @@ public function testUnslashFunctionTrimsTrailingSlashes()
}
}

/** @covers ::asset */
public function testAssetFunction()
{
$this->assertSame(Hyde::asset('foo'), asset('foo'));
$this->assertSame('media/foo', asset('foo'));
}

/** @covers ::asset */
public function testAssetFunctionWithQualifiedUrl()
{
$this->assertSame(Hyde::asset('foo', true), asset('foo', true));
$this->assertSame('http://localhost/media/foo', asset('foo', true));
}

/** @covers ::asset */
public function testAssetFunctionWithExternalUrl()
{
$this->assertSame('https://example.com/foo', asset('https://example.com/foo'));
$this->assertSame('https://example.com/foo', asset('https://example.com/foo', true));
}

/** @covers ::asset */
public function testAssetFunctionWithQualifiedUrlAndNoBaseUrl()
{
$this->app['config']->set(['hyde.url' => null]);
$this->assertSame('media/foo', asset('foo', true));
}

/** @covers ::asset */
public function testAssetFunctionFromNestedPage()
{
Render::shouldReceive('getRouteKey')->andReturn('foo/bar');

$this->assertSame('../media/foo', asset('foo'));
}

/** @covers ::asset */
public function testAssetFunctionFromDeeplyNestedPage()
{
Render::shouldReceive('getRouteKey')->andReturn('foo/bar/baz');

$this->assertSame('../../media/foo', asset('foo'));
}

/** @covers ::asset */
public function testAssetFunctionWithCustomMediaDirectory()
{
Hyde::setMediaDirectory('custom');

$this->assertSame('custom/foo', asset('foo'));
}

/** @covers ::\Hyde\hyde */
public function testHydeFunctionExistsInHydeNamespace()
{
Expand Down

0 comments on commit 08cc208

Please sign in to comment.