-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from swiggles/master
Laravel 5 service provider with backwards compatibility
- Loading branch information
Showing
4 changed files
with
154 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,74 @@ | ||
<?php namespace Bkwld\Croppa; | ||
|
||
// Dependencies | ||
use Illuminate\Support\Facades\Response; | ||
|
||
class ServiceProvider extends \Illuminate\Support\ServiceProvider { | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() { | ||
|
||
// Bind a new singleton instance of Croppa to the app | ||
$this->app->singleton('croppa', function($app) { | ||
|
||
// Inject dependencies | ||
return new Croppa(array_merge(array( | ||
'host' => '//'.$app->make('request')->getHttpHost(), | ||
'public' => $app->make('path.public'), | ||
), $app->make('config')->get('croppa::config'))); | ||
}); | ||
} | ||
|
||
/** | ||
* Bootstrap the application events. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() { | ||
$this->package('bkwld/croppa'); | ||
|
||
// Listen for Cropa style URLs, these are how Croppa gets triggered | ||
$croppa = $this->app['croppa']; | ||
$this->app->make('router')->get('{path}', function($path) use ($croppa) { | ||
$image = $croppa->generate($path); | ||
return Response::stream(function() use ($image) { | ||
return $image->show(); | ||
}); | ||
})->where('path', $croppa->directoryPattern()); | ||
} | ||
|
||
/** | ||
* Get the services provided by the provider. | ||
* | ||
* @return array | ||
*/ | ||
public function provides() { | ||
return array('croppa'); | ||
} | ||
class ServiceProvider extends \Illuminate\Support\ServiceProvider | ||
{ | ||
/** | ||
* Indicates if loading of the provider is deferred. | ||
* | ||
* @var bool | ||
*/ | ||
protected $defer = false; | ||
|
||
/** | ||
* Actual provider | ||
* | ||
* @var \Illuminate\Support\ServiceProvider | ||
*/ | ||
protected $provider; | ||
|
||
/** | ||
* Create a new service provider instance. | ||
* | ||
* @param \Illuminate\Contracts\Foundation\Application $app | ||
* @return void | ||
*/ | ||
public function __construct($app) | ||
{ | ||
parent::__construct($app); | ||
$this->provider = $this->getProvider(); | ||
} | ||
|
||
/** | ||
* Bootstrap the application events. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
return $this->provider->boot(); | ||
} | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
return $this->provider->register(); | ||
} | ||
|
||
/** | ||
* Return ServiceProvider according to Laravel version | ||
*/ | ||
private function getProvider() | ||
{ | ||
$app = $this->app; | ||
$version = intval($app::VERSION); | ||
$provider = sprintf( | ||
'\Bkwld\Croppa\ServiceProviderLaravel%d', $version | ||
); | ||
|
||
return new $provider($app); | ||
} | ||
|
||
/** | ||
* Get the services provided by the provider. | ||
* | ||
* @return array | ||
*/ | ||
public function provides() | ||
{ | ||
return array('croppa'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php namespace Bkwld\Croppa; | ||
|
||
class ServiceProviderLaravel4 extends \Illuminate\Support\ServiceProvider { | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() { | ||
|
||
// Bind a new singleton instance of Croppa to the app | ||
$this->app->singleton('croppa', function($app) { | ||
|
||
// Inject dependencies | ||
return new Croppa(array_merge($app->make('config')->get('croppa::config'), array( | ||
'host' => '//'.$this->app->make('request')->getHttpHost(), | ||
'public' => $app->make('path.public'), | ||
))); | ||
}); | ||
} | ||
|
||
/** | ||
* Bootstrap the application events. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() { | ||
$this->package('bkwld/croppa'); | ||
|
||
// Listen for Cropa style URLs, these are how Croppa gets triggered | ||
$croppa = $this->app['croppa']; | ||
$this->app->make('router')->get('{path}', function($path) use ($croppa) { | ||
$croppa->generate($path); | ||
})->where('path', $croppa->directoryPattern()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php namespace Bkwld\Croppa; | ||
|
||
class ServiceProviderLaravel5 extends \Illuminate\Support\ServiceProvider { | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() { | ||
// merge default config | ||
if (app()->environment('local')) { | ||
$config_file = __DIR__.'/../../config/local/config.php'; | ||
$this->mergeConfigFrom($config_file, 'croppa'); | ||
} | ||
$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'croppa'); | ||
|
||
// Bind a new singleton instance of Croppa to the app | ||
$this->app->singleton('croppa', function($app) { | ||
// Inject dependencies | ||
return new Croppa(array_merge($app['config']->get('croppa'), array( | ||
'host' => '//'.$app->make('request')->getHttpHost(), | ||
'public' => $app->make('path.public'), | ||
))); | ||
}); | ||
} | ||
|
||
/** | ||
* Bootstrap the application events. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() { | ||
|
||
$this->publishes(array( | ||
__DIR__.'/../../config/config.php' => config_path('croppa.php') | ||
)); | ||
|
||
// Listen for Cropa style URLs, these are how Croppa gets triggered | ||
$croppa = $this->app['croppa']; | ||
$this->app->make('router')->get('{path}', function($path) use ($croppa) { | ||
$croppa->generate($path); | ||
})->where('path', $croppa->directoryPattern()); | ||
} | ||
} |