Skip to content

Commit

Permalink
Merge pull request #21 from Vinelab/update/add-path-function
Browse files Browse the repository at this point in the history
add a path() function
  • Loading branch information
Mulkave committed Sep 22, 2014
2 parents 5463810 + 7b577a7 commit 2f1d8ae
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 39 deletions.
33 changes: 24 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ the `Cdn` class to `CdnFacadeProvider` so you can simply use the `Cdn` facade an

## Configuration

Publish the default config using `php artisan config:publish vinelab/cdn` and check it out at `app/config/packages/vinelab/cdn/cdn.php`
Publish the package config file:
```dos
php artisan config:publish vinelab/cdn
```
and check it out at `app/config/packages/vinelab/cdn/cdn.php`

In case you would like to have environment-based configuration `app/config/packages/vinelab/cdn/[ENV]/cdn.php`

Expand Down Expand Up @@ -138,23 +142,34 @@ not to exceed what you have allowed in your PHP configuration.

### Push

Upload your assets with `php artisan cdn:push`

Upload your assets with
```dos
php artisan cdn:push
```
### Load Assets

Since the service provider of this package aliases itself as the facade `Cdn` you may use it as such:
Now you can use the facade `Cdn` to call the `Cdn::asset()` function.
Note: the `asset` works the same as the Laravel `asset` it start looking for assets in the `public/` directory:

```blade
{{Cdn::asset('public/index.php')}}
// https://default-bucket.s3.amazonaws.com/public/index.php
{{Cdn::asset('public/assets/js/main.js')}}
{{Cdn::asset('assets/js/main.js')}}
// https://js-bucket.s3.amazonaws.com/public/assets/js/main.js
{{Cdn::asset('public/assets/css/main.css')}}
{{Cdn::asset('assets/css/main.css')}}
// https://css-bucket.s3.amazonaws.com/public/assets/css/main.css
```

If you want to use a file from outside the `public/` directory anywhere in `app/` you can use the `Cdn::path()` function to do that:

```blade
{{Cdn::path('public/assets/js/main.js')}}
// https://js-bucket.s3.amazonaws.com/public/assets/js/main.js
{{Cdn::path('private/something/file.txt')}}
// https://css-bucket.s3.amazonaws.com/private/something/file.txt
```


## Contributing

Please see [CONTRIBUTING](https://github.com/Vinelab/cdn/blob/master/CONTRIBUTING.md) for details.
Expand Down
2 changes: 1 addition & 1 deletion src/Vinelab/Cdn/Cdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function push()
// return the configurations from the config file
$configurations = $this->helper->getConfigurations();

// Initialize an instance of the asset holder to read the config urations
// Initialize an instance of the asset holder to read the configurations
// then call the read(), to return all the allowed assets as a collection of files objects
$assets = $this->finder->read($this->asset_holder->init($configurations));
// store the returned assets in the instance of the asset holder as collection of paths
Expand Down
48 changes: 35 additions & 13 deletions src/Vinelab/Cdn/CdnFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class CdnFacade implements CdnFacadeInterface{
* @internal param \Vinelab\Cdn\Repository $configurations
*/
public function __construct(
ProviderFactoryInterface $provider_factory,
CdnHelperInterface $helper,
CdnFacadeValidator $cdn_facade_validator
ProviderFactoryInterface $provider_factory,
CdnHelperInterface $helper,
CdnFacadeValidator $cdn_facade_validator
) {
$this->provider_factory = $provider_factory;
$this->helper = $helper;
Expand All @@ -58,7 +58,7 @@ public function __construct(
}

/**
* This function will be called from the 'views' using the
* this function will be called from the 'views' using the
* 'Cdn' facade {{Cdn::asset('')}} to convert the path into
* it's CDN url
*
Expand All @@ -69,24 +69,46 @@ public function __construct(
*/
public function asset($path)
{
if ( ! isset($path))
throw new EmptyPathException('Path does not exist.');
// if asset always append the public/ dir to the path (since the user should not add public/ to asset)
return $this->generateUrl($path, 'public/');
}

// remove slashes from begging and ending of the path then call the
// url generator of the provider
return $this->provider->urlGenerator($this->cleanPath($path));

/**
* this function will be called from the 'views' using the
* 'Cdn' facade {{Cdn::path('')}} to convert the path into
* it's CDN url
*
* @param $path
*
* @return mixed
* @throws Exceptions\EmptyPathException
*/
public function path($path)
{
return $this->generateUrl($path);
}

/**
* remove any extra slashes '/' from the path
* responsible of preparing the path before generating the url
*
* @param $path
* @param $prepend
*
* @return string
* @throws Exceptions\EmptyPathException
* @return
*/
private function cleanPath($path)
private function generateUrl($path, $prepend = '')
{
return rtrim(ltrim($path, '/'), '/');
if ( ! isset($path))
throw new EmptyPathException('Path does not exist.');

// remove slashes from begging and ending of the path
// and append directories if needed
$clean_path = $prepend . $this->helper->cleanPath($path);

// call the provider specific url generator
return $this->provider->urlGenerator($clean_path);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Vinelab/Cdn/CdnHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,29 @@ public function parseUrl($url)
return parse_url($url);
}

/**
* check if a string starts with a string
*
* @param $with
* @param $str
*
* @return bool
*/
public function startsWith($with, $str)
{
return (substr($str, 0, strlen($with)) === $with);
}

/**
* remove any extra slashes '/' from the path
*
* @param $path
*
* @return string
*/
public function cleanPath($path)
{
return rtrim(ltrim($path, '/'), '/');
}

}
3 changes: 3 additions & 0 deletions src/Vinelab/Cdn/Contracts/CdnHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ public function validate($configuration, $required);

public function parseUrl($url);

public function startsWith($haystack, $needle);

public function cleanPath($path);
}
35 changes: 19 additions & 16 deletions tests/Vinelab/Cdn/CdnFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public function setUp()
{
parent::setUp();

$this->asset_path = 'public/foo/bar.php';
$this->cdn_url = 'https://amazon.foo.bar.com';
$this->asset_path = 'foo/bar.php';
$this->path_path = 'public/foo/bar.php';
$this->asset_url = 'https://bucket.s3.amazonaws.com/public/foo/bar.php';

$this->provider = M::mock('Vinelab\Cdn\Providers\AwsS3Provider');

Expand All @@ -18,6 +19,8 @@ public function setUp()

$this->helper = M::mock('Vinelab\Cdn\Contracts\CdnHelperInterface');
$this->helper->shouldReceive('getConfigurations')->once()->andReturn([]);
$this->helper->shouldReceive('cleanPath')->andReturn($this->asset_path);
$this->helper->shouldReceive('startsWith')->andReturn(true);

$this->validator = new \Vinelab\Cdn\Validators\CdnFacadeValidator;

Expand All @@ -34,31 +37,31 @@ public function tearDown()
public function testAssetIsCallingUrlGenerator()
{
$this->provider->shouldReceive('urlGenerator')
->with($this->asset_path)
->once()
->andReturn($this->cdn_url);
->once()
->andReturn($this->asset_url);

$result = $this->facade->asset($this->asset_path);

$result = $this->facade->asset($this->asset_path);
// assert is calling the url generator
assertEquals($result, $this->cdn_url);
assertEquals($result, $this->asset_url);
}

public function testCleanPathIsCleaning()
public function testPathIsCallingUrlGenerator()
{
$path = '/foo/bar/';
$cleaned_path = 'foo/bar';
// invoke the private function cleanPath()
$result = $this->invokeMethod($this->facade, 'cleanPath', array($path));
assertEquals($result, $cleaned_path);
$this->provider->shouldReceive('urlGenerator')
->once()
->andReturn($this->asset_url);

$result = $this->facade->asset($this->path_path);
// assert is calling the url generator
assertEquals($result, $this->asset_url);
}

/**
* @expectedException \Vinelab\Cdn\Exceptions\EmptyPathException
*/
public function testAssetThrowsExceptionWhenEmptyParameter()
public function testUrlGeneratorThrowsException()
{
$this->facade->asset(null);
$this->invokeMethod($this->facade, 'generateUrl', array(null, null));
}

}

0 comments on commit 2f1d8ae

Please sign in to comment.