Skip to content

Commit

Permalink
Add fallback support to Laravel 5 configuration.
Browse files Browse the repository at this point in the history
Signed-off-by: crynobone <crynobone@gmail.com>
  • Loading branch information
crynobone committed Mar 18, 2015
1 parent 77915b6 commit 11a14d7
Showing 4 changed files with 76 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,10 @@ title: Imagine Change Log

## Version 3.0 {#v3-0}

### v3.0.1 {#v3.0.1}

* Add fallback support to Laravel 5 configuration.

### v3.0.0 {#v3-0-0}

* Update support to Laravel Framework v5.0.
36 changes: 34 additions & 2 deletions src/ImagineManager.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<?php namespace Orchestra\Imagine;

use Illuminate\Support\Arr;
use Imagine\Gd\Imagine as Gd;
use Illuminate\Support\Manager;
use Imagine\Gmagick\Imagine as Gmagick;
use Imagine\Imagick\Imagine as Imagick;

class ImagineManager extends Manager
{
/**
* Configuration values.
*
* @var array
*/
protected $config = [];

/**
* Create an instance of the GD driver.
*
@@ -44,7 +52,7 @@ protected function createImagickDriver()
*/
public function getDefaultDriver()
{
return $this->app['config']->get('orchestra/imagine::driver', 'gd');
return Arr::get($this->config, 'driver', 'gd');
}

/**
@@ -56,6 +64,30 @@ public function getDefaultDriver()
*/
public function setDefaultDriver($name)
{
$this->app['config']->set('orchestra/imagine::driver', $name);
$this->config['driver'] = $name;
}

/**
* Get configuration values.
*
* @return array
*/
public function getConfig()
{
return $this->config;
}

/**
* Set configuration.
*
* @param array $config
*
* @return $this
*/
public function setConfig($config)
{
$this->config = $config;

return $this;
}
}
27 changes: 26 additions & 1 deletion src/ImagineServiceProvider.php
Original file line number Diff line number Diff line change
@@ -19,7 +19,12 @@ class ImagineServiceProvider extends ServiceProvider
public function register()
{
$this->app->singleton('orchestra.imagine', function ($app) {
return new ImagineManager($app);
$manager = new ImagineManager($app);
$namespace = $this->hasPackageRepository() ? 'orchestra/imagine::' : 'orchestra.imagine';

$manager->setConfig($app['config'][$namespace]);

return $manager;
});

$this->registerCoreContainerAliases();
@@ -49,6 +54,26 @@ public function boot()
$path = realpath(__DIR__.'/../resources');

$this->addConfigComponent('orchestra/imagine', 'orchestra/imagine', $path.'/config');

if (! $this->hasPackageRepository()) {
$this->bootUsingLaravel($path);
}
}

/**
* Boot using Laravel setup.
*
* @param string $path
*
* @return void
*/
protected function bootUsingLaravel($path)
{
$this->mergeConfigFrom("{$path}/config/config.php", 'orchestra.imagine');

$this->publishes([
"{$path}/config/config.php" => config_path('orchestra/imagine.php'),
]);
}

/**
17 changes: 12 additions & 5 deletions tests/ImagineServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -23,6 +23,9 @@ public function tearDown()
public function testRegisterMethod()
{
$app = new Container();
$app['config'] = $config = m::mock('\Illuminate\Contracts\Config\Repository', '\ArrayAccess');

$config->shouldReceive('offsetGet')->once()->with('orchestra.imagine')->andReturn([]);

$stub = new ImagineServiceProvider($app);
$stub->register();
@@ -39,15 +42,19 @@ public function testBootMethod()
{
$app = new Container();

$app['config'] = $config = m::mock('\Illuminate\Contracts\Config\Repository');
$app['files'] = $files = m::mock('\Illuminate\Filesystem\Filesystem');
$app['path'] = __DIR__.'/../';
$app['config'] = $config = m::mock('\Illuminate\Contracts\Config\Repository', '\ArrayAccess');
$app['files'] = $files = m::mock('\Illuminate\Filesystem\Filesystem');
$app['path'] = __DIR__.'/../';
$app['path.base'] = __DIR__.'/../';

$files->shouldReceive('isDirectory')->andReturn(false);
$config->shouldReceive('get')->with('orchestra/imagine::driver', 'gd')->andReturn('gd');
$config->shouldReceive('offsetGet')->once()->with('orchestra.imagine')->andReturn(['driver' => 'gd']);

$stub = m::mock('\Orchestra\Imagine\ImagineServiceProvider[bootUsingLaravel]', [$app])
->shouldAllowMockingProtectedMethods();

$stub->shouldReceive('bootUsingLaravel')->once()->with(realpath($app['path.base'].'/resources'))->andReturnNull();

$stub = new ImagineServiceProvider($app);
$stub->register();
$stub->boot();

0 comments on commit 11a14d7

Please sign in to comment.