Skip to content

Commit

Permalink
Merge pull request #72 from swiggles/master
Browse files Browse the repository at this point in the history
Laravel 5 service provider with backwards compatibility
  • Loading branch information
weotch committed Feb 12, 2015
2 parents 93770d2 + 1030614 commit 1a59d3f
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 50 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": ">=5.3.0",
"illuminate/support": "<4.3,>=4.0",
"illuminate/support": "~5.0",
"weotch/PHPThumb": "~1.0",
"symfony/http-kernel": "~2.0"
},
Expand Down
119 changes: 70 additions & 49 deletions src/Bkwld/Croppa/ServiceProvider.php
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');
}
}
38 changes: 38 additions & 0 deletions src/Bkwld/Croppa/ServiceProviderLaravel4.php
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());
}

}
45 changes: 45 additions & 0 deletions src/Bkwld/Croppa/ServiceProviderLaravel5.php
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());
}
}

0 comments on commit 1a59d3f

Please sign in to comment.