public function testAddAndRemoveStorage() { $mockStorage = \Mockery::mock('SimplePhoto\\Storage\\StorageInterface'); $this->storageManager->add('mock_storage', $mockStorage); $this->assertTrue($this->storageManager->has('mock_storage')); $this->storageManager->remove('mock_storage'); $this->assertFalse($this->storageManager->has('mock_storage')); }
/** * @param array $photo * @param array $photo Photo data * @param array $options Available options * <pre> * fallback: A fallback photo to use when photo is not found * transform: Transformation options to be applied to photo * </pre> * @return bool|PhotoResult */ public function build($photo, array $options = array()) { $options = array_merge(array('transform' => array(), 'fallback' => null), $options); if (empty($photo)) { // Could not find photo data if ($options['fallback'] == null || !$this->storageManager->has(StorageManager::FALLBACK_STORAGE)) { // No fallback photo is defined or no fallback storage added // We shouldn't probably continue. Seems developer prefers // to handle missing images manually. return false; } // Construct default data $photo = array('photo_id' => 0, 'storage_name' => StorageManager::FALLBACK_STORAGE, 'file_name' => pathinfo($options['fallback'], PATHINFO_FILENAME), 'file_path' => $options['fallback'], 'file_mime' => 'image/png', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')); } $photoResult = new PhotoResult($photo); $storage = $this->storageManager->get($photo['storage_name']); if (!empty($options['transform'])) { // Transformation options available $modifiedFileName = $this->generateModifiedSaveName($photo['file_path'], $options['transform']); $photoResult->setOriginalFilePath($photo['file_path']); $photoResult->setOriginalPath($storage->getPhotoPath($photo['file_path'])); if (!($info = $storage->getInfo($modifiedFileName))) { // Only do image manipulation once // (ie if file does not exists) list($modifiedFileName, $info['file_size']) = $this->transformPhoto($storage, $storage->getPhotoResource($photoResult->originalFilePath(), tempnam($this->options['tmp_dir'], 'sp')), $modifiedFileName, $photo['file_mime'], $options['transform']); } $photoResult->setFileSize($info['file_size']); // Set the file path to the new modified photo path $photoResult->setFilePath($modifiedFileName); } $photoResult->setPath($storage->getPhotoPath($photoResult->filePath())); $photoResult->setUrl($storage->getPhotoUrl($photoResult->filePath())); return $photoResult; }
protected function registerStorageManager() { $this->app['simple-photo.storage-manager'] = $this->app->share(function ($app) { $storageManager = new StorageManager(); $storageLocations = $app['config']['morrelinko/laravel-simple-photo::storage_locations']; foreach ($storageLocations as $storageName => $storageLocation) { $storageManager->add($storageName, $app['simple-photo.storage-factory']->createStorage($storageLocation['storage'], $storageLocation['options'])); } $storageManager->setDefault($app['config']['morrelinko/laravel-simple-photo::default_storage']); $fallbackStorage = $app['config']['morrelinko/laravel-simple-photo::fallback_storage']; if ($fallbackStorage) { $storageManager->setFallback($storageManager->get($fallbackStorage)); } return $storageManager; }); }