Skip to content

Commit

Permalink
Merge pull request #18 from publiux/development
Browse files Browse the repository at this point in the history
New version 2. Laravel 5.5 support added.
  • Loading branch information
publiux authored Sep 5, 2017
2 parents 07212db + 1a542fe commit df9b61f
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 379 deletions.
File renamed without changes.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm

matrix:
Expand All @@ -22,4 +23,4 @@ after_script:

notifications:
slack:
secure: GLAz8cc60i+a7DiXrFqoThKPaSOpNbcCiqjSur8LuBPCtxWMfBFEKsmE7iweAe5fLUTffx4TbMbMd38LS/3hwpt5oYOoQBQ8IQDNtFJ7rbLkGtC6SI6lrPTWdBDfTyHevG4aOJZCiFe5+Tajvj2CWD+YAMcGaCn6s8IVLU5NoPw=
secure: GLAz8cc60i+a7DiXrFqoThKPaSOpNbcCiqjSur8LuBPCtxWMfBFEKsmE7iweAe5fLUTffx4TbMbMd38LS/3hwpt5oYOoQBQ8IQDNtFJ7rbLkGtC6SI6lrPTWdBDfTyHevG4aOJZCiFe5+Tajvj2CWD+YAMcGaCn6s8IVLU5NoPw=
22 changes: 16 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
],
"require": {
"php": ">=5.5.9",
"illuminate/support": "~5.0",
"illuminate/config": "~5.0",
"symfony/finder": "2.8.*|3.0.*",
"symfony/console": "2.8.*|3.0.*",
"aws/aws-sdk-php": "~3.0"
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*",
"illuminate/config": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*",
"symfony/finder": "2.*|3.*",
"symfony/console": "2.*|3.*",
"aws/aws-sdk-php": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand All @@ -36,9 +36,19 @@
"tests/TestCase.php",
"src/Publiux/laravelcdn/Exceptions"
],
"psr-0": {
"psr-0": {
"Publiux\\laravelcdn\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Publiux\\laravelcdn\\CDNServiceProvider"
],
"aliases": {
"CDN": "Publiux\\laravelcdn\\Facades\\CdnFacadeAccessor"
}
}
},
"minimum-stability": "stable"
}
29 changes: 21 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

##### Content Delivery Network Package for Laravel

The package provides the developer the ability to upload his assets (or any public file) to a CDN with a single artisan command.
And then it allows him to switch between the local and the online version of the files.
The package provides the developer the ability to upload their assets (or any public file) to a CDN with a single artisan command.
And then it allows them to switch between the local and the online version of the files.

###### Fork From [Vinelab/cdn](https://github.com/Vinelab/cdn)
This project has been forked from https://github.com/Vinelab/cdn. All credit for the original work goes there.

#### Laravel Support
- This fork support Laravel 5.2. (`master`).
- As new versions of Laravel are released, new branches will be created to create compatibility if there are breaking changes in laravel.
- This fork supports Laravel 5.2 up to an including Laravel 5.5 (`master`).
- Laravel 5.5 is supported, as is package auto-discovery.

## Highlights

Expand All @@ -39,24 +39,25 @@ This project has been forked from https://github.com/Vinelab/cdn. All credit for
Require `publiux/laravelcdn` in your project:

```bash
composer require publiux/laravelcdn
composer require "publiux/laravelcdn:~2.0"
```

*Since this is a Laravel package we need to register the service provider:*
*If you are using Laravel 5.4 or below, you need to register the service provider:*

Add the service provider to `config/app.php`:
Laravel 5.4 and below: Add the service provider to `config/app.php`:

```php
'providers' => array(
//...
Publiux\laravelcdn\CdnServiceProvider::class,
),
```
*If you are using Laravel 5.5, there is no need to register the service provider as this package is automatically discovered.*

Publish the package config file:

```bash
php artisan vendor:publish
php artisan vendor:publish --provider 'Publiux\laravelcdn\CdnServiceProvider'
```

## Environment Configuration
Expand Down Expand Up @@ -227,6 +228,18 @@ Use the facade `Cdn` to call the `Cdn::asset()` function.
{{Cdn::asset('assets/css/style.css')}} // example result: https://css-bucket.s3.amazonaws.com/public/assets/css/style.css
```
*Note: the `elixir` works the same as the Laravel `elixir` it loads the manifest.json file from build folder and choose the correct file revision generated by gulp:*
```blade
{{Cdn::elixir('assets/js/main.js')}} // example result: https://js-bucket.s3.amazonaws.com/public/build/assets/js/main-85cafe36ff.js
{{Cdn::elixir('assets/css/style.css')}} // example result: https://css-bucket.s3.amazonaws.com/public/build/assets/css/style-2d558139f2.css
```
*Note: the `mix` works the same as the Laravel 5.4 `mix` it loads the mix-manifest.json file from public folder and choose the correct file revision generated by webpack:*
```blade
{{Cdn::mix('/js/main.js')}} // example result: https://js-bucket.s3.amazonaws.com/public/js/main-85cafe36ff.js
{{Cdn::mix('/css/style.css')}} // example result: https://css-bucket.s3.amazonaws.com/public/css/style-2d558139f2.css
```

To use a file from outside the `public/` directory, anywhere in `app/` use the `Cdn::path()` function:

Expand Down
116 changes: 71 additions & 45 deletions src/Publiux/laravelcdn/CdnFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,48 +65,22 @@ public function __construct(
}

/**
* this function will be called from the 'views' using the
* 'Cdn' facade {{Cdn::asset('')}} to convert the path into
* it's CDN url.
*
* @param $path
*
* @throws Exceptions\EmptyPathException
*
* @return mixed
* Read the configuration file and pass it to the provider factory
* to return an object of the default provider specified in the
* config file.
*/
public function asset($path)
private function init()
{
// if asset always append the public/ dir to the path (since the user should not add public/ to asset)
return $this->generateUrl($path, 'public/');
}
// return the configurations from the config file
$this->configurations = $this->helper->getConfigurations();

/**
* this function will be called from the 'views' using the
* 'Cdn' facade {{Cdn::elixir('')}} to convert the elixir generated file path into
* it's CDN url.
*
* @param $path
*
* @throws Exceptions\EmptyPathException, \InvalidArgumentException
*
* @return mixed
*/
public function elixir($path)
{
static $manifest = null;
if (is_null($manifest)) {
$manifest = json_decode(file_get_contents(public_path('build/rev-manifest.json')), true);
}
if (isset($manifest[$path])) {
return $this->generateUrl('build/'.$manifest[$path], 'public/');
}
throw new \InvalidArgumentException("File {$path} not defined in asset manifest.");
// return an instance of the corresponding Provider concrete according to the configuration
$this->provider = $this->provider_factory->create($this->configurations);
}

/**
* this function will be called from the 'views' using the
* 'Cdn' facade {{Cdn::path('')}} to convert the path into
* 'Cdn' facade {{Cdn::asset('')}} to convert the path into
* it's CDN url.
*
* @param $path
Expand All @@ -115,9 +89,10 @@ public function elixir($path)
*
* @return mixed
*/
public function path($path)
public function asset($path)
{
return $this->generateUrl($path);
// if asset always append the public/ dir to the path (since the user should not add public/ to asset)
return $this->generateUrl($path, 'public/');
}

/**
Expand Down Expand Up @@ -157,16 +132,67 @@ private function generateUrl($path, $prepend = '')
}

/**
* Read the configuration file and pass it to the provider factory
* to return an object of the default provider specified in the
* config file.
* this function will be called from the 'views' using the
* 'Cdn' facade {{Cdn::mix('')}} to convert the Laravel 5.4 webpack mix
* generated file path into it's CDN url.
*
* @param $path
*
* @return mixed
*
* @throws Exceptions\EmptyPathException, \InvalidArgumentException
*/
private function init()
public function mix($path)
{
// return the configurations from the config file
$this->configurations = $this->helper->getConfigurations();
static $manifest = null;
if (is_null($manifest)) {
$manifest = json_decode(file_get_contents(public_path('mix-manifest.json')), true);
}
if (isset($manifest['/' . $path])) {
return $this->generateUrl($manifest['/' . $path], 'public/');
}
if (isset($manifest[$path])) {
return $this->generateUrl($manifest[$path], 'public/');
}
throw new \InvalidArgumentException("File {$path} not defined in asset manifest.");
}

// return an instance of the corresponding Provider concrete according to the configuration
$this->provider = $this->provider_factory->create($this->configurations);
/**
* this function will be called from the 'views' using the
* 'Cdn' facade {{Cdn::elixir('')}} to convert the elixir generated file path into
* it's CDN url.
*
* @param $path
*
* @throws Exceptions\EmptyPathException, \InvalidArgumentException
*
* @return mixed
*/
public function elixir($path)
{
static $manifest = null;
if (is_null($manifest)) {
$manifest = json_decode(file_get_contents(public_path('build/rev-manifest.json')), true);
}
if (isset($manifest[$path])) {
return $this->generateUrl('build/' . $manifest[$path], 'public/');
}
throw new \InvalidArgumentException("File {$path} not defined in asset manifest.");
}

/**
* 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
*
* @throws Exceptions\EmptyPathException
*
* @return mixed
*/
public function path($path)
{
return $this->generateUrl($path);
}
}
20 changes: 10 additions & 10 deletions src/Publiux/laravelcdn/CdnServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ public function register()

// register the commands:
//-----------------------
$this->app['cdn.push'] = $this->app->share(function () {
return $this->app->make('Publiux\laravelcdn\Commands\PushCommand');
$this->app->singleton('cdn.push', function ($app) {
return $app->make('Publiux\laravelcdn\Commands\PushCommand');
});

$this->commands('cdn.push');

$this->app['cdn.empty'] = $this->app->share(function () {
return $this->app->make('Publiux\laravelcdn\Commands\EmptyCommand');
$this->app->singleton('cdn.empty', function ($app) {
return $app->make('Publiux\laravelcdn\Commands\EmptyCommand');
});

$this->commands('cdn.empty');
Expand All @@ -111,15 +111,15 @@ public function register()
//-----------------

// Register 'CdnFacade' instance container to our CdnFacade object
$this->app['cdn'] = $this->app->share(function () {
return $this->app->make('Publiux\laravelcdn\CdnFacade');
$this->app->singleton('CDN', function ($app) {
return $app->make('Publiux\laravelcdn\CdnFacade');
});

// Shortcut so developers don't need to add an Alias in app/config/app.php
$this->app->booting(function () {
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Cdn', 'Publiux\laravelcdn\Facades\CdnFacadeAccessor');
});
// $this->app->booting(function () {
// $loader = \Illuminate\Foundation\AliasLoader::getInstance();
// $loader->alias('Cdn', 'Publiux\laravelcdn\Facades\CdnFacadeAccessor');
// });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Publiux/laravelcdn/Facades/CdnFacadeAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class CdnFacadeAccessor extends Facade
*/
protected static function getFacadeAccessor()
{
return 'cdn';
return 'CDN';
}
}
Loading

0 comments on commit df9b61f

Please sign in to comment.