diff --git a/composer.json b/composer.json index 529c7ac9..9477d9d8 100644 --- a/composer.json +++ b/composer.json @@ -17,16 +17,12 @@ }, "require-dev": { "phpunit/phpunit": "3.*", - "mockery/mockery": "0.9.*", - "illuminate/support": "4.*", - "illuminate/config": "4.*" + "mockery/mockery": "0.9.*" }, "suggest": { "ext-gd": "to use GD library based image processing.", "ext-imagick": "to use Imagick based image processing.", - "intervention/imagecache": "Caching extension for the Intervention Image library", - "illuminate/support": "Used for Laravel integration", - "illuminate/config": "Used for Laravel integration" + "intervention/imagecache": "Caching extension for the Intervention Image library" }, "autoload": { "psr-0": { diff --git a/src/Intervention/Image/ImageManager.php b/src/Intervention/Image/ImageManager.php index 06cdf380..d3cc3f2f 100644 --- a/src/Intervention/Image/ImageManager.php +++ b/src/Intervention/Image/ImageManager.php @@ -3,60 +3,38 @@ namespace Intervention\Image; use Closure; -use Illuminate\Config\Repository as Config; -use Illuminate\Config\FileLoader; -use Illuminate\Filesystem\Filesystem; class ImageManager { /** - * Instance of Illuminate Config respository + * Config * - * @var \Illuminate\Config\Repository + * @var array */ - public $config; + public $config = array( + 'driver' => 'gd' + ); /** * Creates new instance of Image Manager * - * @param \Illuminate\Config\Repository $config + * @param array $config */ - public function __construct(\Illuminate\Config\Repository $config = null) + public function __construct(array $config = array()) { - // create configurator - if (is_a($config, 'Illuminate\\Config\\Repository')) { - - $this->config = $config; - - } else { - - $config_path = __DIR__.'/../../config'; - $env = 'production'; - - $file = new Filesystem; - $loader = new FileLoader($file, $config_path); - $this->config = new Config($loader, $env); - $this->config->package('intervention/image', $config_path, 'image'); - } + $this->configure($config); } /** - * Creates a driver instance according to config settings + * Overrides configuration settings * - * @return Intervention\Image\AbstractDriver + * @param array $config */ - private function createDriver() + public function configure(array $config = array()) { - $drivername = ucfirst($this->config->get('image::driver')); - $driverclass = sprintf('Intervention\\Image\\%s\\Driver', $drivername); + $this->config = array_replace($this->config, $config); - if (class_exists($driverclass)) { - return new $driverclass; - } - - throw new \Intervention\Image\Exception\NotSupportedException( - "Driver ({$drivername}) could not be instantiated." - ); + return $this; } /** @@ -113,4 +91,23 @@ class ImageManager "Please install package intervention/imagecache before running this function." ); } + + /** + * Creates a driver instance according to config settings + * + * @return Intervention\Image\AbstractDriver + */ + private function createDriver() + { + $drivername = ucfirst($this->config['driver']); + $driverclass = sprintf('Intervention\\Image\\%s\\Driver', $drivername); + + if (class_exists($driverclass)) { + return new $driverclass; + } + + throw new \Intervention\Image\Exception\NotSupportedException( + "Driver ({$drivername}) could not be instantiated." + ); + } } diff --git a/src/Intervention/Image/ImageManagerBasic.php b/src/Intervention/Image/ImageManagerBasic.php deleted file mode 100644 index 9ec10470..00000000 --- a/src/Intervention/Image/ImageManagerBasic.php +++ /dev/null @@ -1,108 +0,0 @@ - 'gd', - ); - - /** - * Creates new instance of Image Manager - * - * @param array $config - */ - public function __construct(array $config = array()) - { - $this->config = $this->defaultConfig + $config; - } - - /** - * Creates a driver instance according to config settings - * - * @return Intervention\Image\AbstractDriver - */ - private function createDriver() - { - $drivername = ucfirst($this->config['driver']); - $driverclass = sprintf('Intervention\\Image\\%s\\Driver', $drivername); - - if (class_exists($driverclass)) { - return new $driverclass; - } - - throw new \Intervention\Image\Exception\NotSupportedException( - "Driver ({$drivername}) could not be instantiated." - ); - } - - /** - * Initiates an Image instance from different input types - * - * @param mixed $data - * - * @return Intervention\Image\Image - */ - public function make($data) - { - return $this->createDriver()->init($data); - } - - /** - * Creates an empty image canvas - * - * @param integer $width - * @param integer $height - * @param mixed $background - * - * @return Intervention\Image\Image - */ - public function canvas($width, $height, $background = null) - { - return $this->createDriver()->newImage($width, $height, $background); - } - - /** - * Create new cached image and run callback - * (requires additional package intervention/imagecache) - * - * @param Closure $callback - * @param integer $lifetime - * @param boolean $returnObj - * - * @return Image - */ - public function cache(Closure $callback, $lifetime = null, $returnObj = false) - { - if (class_exists('Intervention\\Image\\ImageCache')) { - // create imagecache - $imagecache = new ImageCache($this); - - // run callback - if (is_callable($callback)) { - $callback($imagecache); - } - - return $imagecache->get($lifetime, $returnObj); - } - - throw new \Intervention\Image\Exception\NotSupportedException( - "Please install package intervention/imagecache before running this function." - ); - } -} diff --git a/src/Intervention/Image/ImageManagerStatic.php b/src/Intervention/Image/ImageManagerStatic.php index 14e2f515..b0d76b2a 100644 --- a/src/Intervention/Image/ImageManagerStatic.php +++ b/src/Intervention/Image/ImageManagerStatic.php @@ -7,30 +7,42 @@ use Closure; class ImageManagerStatic { /** - * Instance of Intervention\Image\ImageManagerBasic + * Instance of Intervention\Image\ImageManager * - * @var ImageManagerBasic + * @var ImageManager */ - public $manager; + public static $manager; /** * Creates a new instance * - * @param ImageManagerBasic $manager + * @param ImageManager $manager */ - public function __construct(ImageManagerBasic $manager = null) + public function __construct(ImageManager $manager = null) { - $this->manager = $manager ? $manager : new ImageManagerBasic; + self::$manager = $manager ? $manager : new ImageManager; } /** - * Creates a new instance + * Get or create new ImageManager instance * - * @return Intervention\Image\ImageManagerStatic + * @return ImageManager */ - public static function newInstance() + public static function getManager() { - return new self; + return self::$manager ? self::$manager : new ImageManager; + } + + /** + * Statically create new custom configured image manager + * + * @param array $config + * + * @return ImageManager + */ + public static function configure(array $config = array()) + { + return self::$manager = self::getManager()->configure($config); } /** @@ -42,7 +54,7 @@ class ImageManagerStatic */ public static function make($data) { - return self::newInstance()->manager->make($data); + return self::getManager()->make($data); } /** @@ -56,7 +68,7 @@ class ImageManagerStatic */ public static function canvas($width, $height, $background = null) { - return self::newInstance()->manager->canvas($width, $height, $background); + return self::getManager()->canvas($width, $height, $background); } /** @@ -70,6 +82,6 @@ class ImageManagerStatic */ public static function cache(Closure $callback, $lifetime = null, $returnObj = false) { - return self::newInstance()->manager->cache($callback, $lifetime, $returnObj); + return self::getManager()->cache($callback, $lifetime, $returnObj); } } diff --git a/src/Intervention/Image/ImageServiceProvider.php b/src/Intervention/Image/ImageServiceProvider.php index 68383701..711abab9 100644 --- a/src/Intervention/Image/ImageServiceProvider.php +++ b/src/Intervention/Image/ImageServiceProvider.php @@ -100,7 +100,7 @@ class ImageServiceProvider extends ServiceProvider $app = $this->app; $app['image'] = $app->share(function ($app) { - return new ImageManager($app['config']); + return new ImageManager($app['config']->get('image::config')); }); } diff --git a/tests/GdSystemTest.php b/tests/GdSystemTest.php index 28790b1c..64932284 100644 --- a/tests/GdSystemTest.php +++ b/tests/GdSystemTest.php @@ -1557,9 +1557,8 @@ class GdSystemTest extends PHPUnit_Framework_TestCase private function manager() { - $manager = new \Intervention\Image\ImageManager; - $manager->config->set('image::driver', 'gd'); - - return $manager; + return new \Intervention\Image\ImageManager(array( + 'driver' => 'gd' + )); } } diff --git a/tests/ImageManagerStaticTest.php b/tests/ImageManagerStaticTest.php index d42301de..66bc5959 100644 --- a/tests/ImageManagerStaticTest.php +++ b/tests/ImageManagerStaticTest.php @@ -9,27 +9,27 @@ class ImageManagerStaticTest extends PHPUnit_Framework_TestCase Mockery::close(); } - public function testNewInstance() + public function testGetManager() { - $manager = Mockery::mock('Intervention\Image\ImageManagerBasic'); + $manager = Mockery::mock('Intervention\Image\ImageManager'); $managerStatic = new ImageManagerStatic($manager); - $m = $managerStatic->newInstance(); - $this->assertInstanceOf('Intervention\Image\ImageManagerStatic', $m); + $m = $managerStatic->getManager(); + $this->assertInstanceOf('Intervention\Image\ImageManager', $m); } public function testMake() { - $manager = Mockery::mock('Intervention\Image\ImageManagerBasic'); + $manager = Mockery::mock('Intervention\Image\ImageManager'); + $manager->shouldReceive('make')->with('foo')->once(); $managerStatic = new ImageManagerStatic($manager); - $img = $managerStatic->make('tests/images/tile.png'); - $this->assertInstanceOf('Intervention\Image\Image', $img); + $managerStatic->make('foo'); } public function testCanvas() { - $manager = Mockery::mock('Intervention\Image\ImageManagerBasic'); + $manager = Mockery::mock('Intervention\Image\ImageManager'); + $manager->shouldReceive('canvas')->with(100, 100, null)->once(); $managerStatic = new ImageManagerStatic($manager); - $img = $managerStatic->canvas(100, 100); - $this->assertInstanceOf('Intervention\Image\Image', $img); + $managerStatic->canvas(100, 100); } } diff --git a/tests/ImageManagerTest.php b/tests/ImageManagerTest.php new file mode 100644 index 00000000..d28d0c4c --- /dev/null +++ b/tests/ImageManagerTest.php @@ -0,0 +1,29 @@ + 'foo', 'bar' => 'baz'); + $manager = new ImageManager($config); + $this->assertEquals('foo', $manager->config['driver']); + $this->assertEquals('baz', $manager->config['bar']); + } + + public function testConfigure() + { + $overwrite = array('driver' => 'none', 'bar' => 'none'); + $config = array('driver' => 'foo', 'bar' => 'baz'); + $manager = new ImageManager($overwrite); + $manager->configure($config); + $this->assertEquals('foo', $manager->config['driver']); + $this->assertEquals('baz', $manager->config['bar']); + } +} diff --git a/tests/ImagickSystemTest.php b/tests/ImagickSystemTest.php index ab7cda00..25b28db6 100644 --- a/tests/ImagickSystemTest.php +++ b/tests/ImagickSystemTest.php @@ -1538,9 +1538,8 @@ class ImagickSystemTest extends PHPUnit_Framework_TestCase private function manager() { - $manager = new \Intervention\Image\ImageManager; - $manager->config->set('image::driver', 'imagick'); - - return $manager; + return new \Intervention\Image\ImageManager(array( + 'driver' => 'imagick' + )); } }