Skip to content

Commit

Permalink
Don’t build Flysystem disks until needed
Browse files Browse the repository at this point in the history
Instantiating those disks actually created directories in the filesystem.  This caused problems with `php artisan storage:link` where Croppa would create an empty public/storage immediately before the symlink was created.  It was a side effect of the storage class getting booted because it was a dependecy of many classes that get insantiated with artisan.
  • Loading branch information
weotch committed Nov 1, 2017
1 parent 2f55209 commit c8c73d8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Bkwld/Croppa/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function register() {

// Interact with the disk
$this->app->singleton('Bkwld\Croppa\Storage', function($app) {
return Storage::make($app, $this->getConfig());
return new Storage($app, $this->getConfig());
});

// API for use in apps
Expand Down
57 changes: 44 additions & 13 deletions src/Bkwld/Croppa/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,49 @@ static public function make($app, $config) {
/**
* Set the crops disk
*
* @param League\Flysystem\Filesystem | League\Flysystem\Cached\CachedAdapter
* @param League\Flysystem\Filesystem
* League\Flysystem\Cached\CachedAdapter
*/
public function setCropsDisk($disk) {
$this->crops_disk = $disk;
}

/**
* Get the crops disk or make via the config
*
* @return League\Flysystem\Filesystem
* League\Flysystem\Cached\CachedAdapter
*/
public function getCropsDisk() {
if (empty($this->crops_disk)) {
$this->setCropsDisk($this->makeDisk($this->config['crops_dir']));
}
return $this->crops_disk;
}

/**
* Set the src disk
*
* @param League\Flysystem\Filesystem | League\Flysystem\Cached\CachedAdapter
* @param League\Flysystem\Filesystem
* League\Flysystem\Cached\CachedAdapter
*/
public function setSrcDisk($disk) {
$this->src_disk = $disk;
}

/**
* Get the src disk or make via the config
*
* @return League\Flysystem\Filesystem
* League\Flysystem\Cached\CachedAdapter
*/
public function getSrcDisk() {
if (empty($this->src_disk)) {
$this->setSrcDisk($this->makeDisk($this->config['src_dir']));
}
return $this->src_disk;
}

/**
* "Mount" disks given the config
*
Expand Down Expand Up @@ -107,7 +135,7 @@ public function makeDisk($dir) {
* @return boolean
*/
public function cropsAreRemote() {
$adapter = $this->crops_disk->getAdapter();
$adapter = $this->getCropsDisk()->getAdapter();

// If using a cached adapter, get the actual adapter that is being cached.
if (is_a($adapter, 'League\Flysystem\Cached\CachedAdapter')) {
Expand All @@ -125,7 +153,7 @@ public function cropsAreRemote() {
* @return boolean
*/
public function cropExists($path) {
return $this->crops_disk->has($path);
return $this->getCropsDisk()->has($path);
}

/**
Expand All @@ -136,7 +164,8 @@ public function cropExists($path) {
* @return string
*/
public function readSrc($path) {
if ($this->src_disk->has($path)) return $this->src_disk->read($path);
$disk = $this->getSrcDisk();
if ($disk->has($path)) return $disk->read($path);
else throw new NotFoundHttpException('Croppa: Src image is missing');
}

Expand All @@ -150,7 +179,7 @@ public function readSrc($path) {
*/
public function writeCrop($path, $contents) {
try {
$this->crops_disk->write($path, $contents);
$this->getCropsDisk()->write($path, $contents);
} catch(FileExistsException $e) {
throw new Exception("Croppa: Crop already exists at $path. You probably
have a misconfiguration. Make sure that the URL to your crop can be
Expand All @@ -165,7 +194,7 @@ public function writeCrop($path, $contents) {
* @return string
*/
public function getLocalCropsDirPath() {
return $this->crops_disk->getAdapter()->getPathPrefix();
return $this->getCropsDisk()->getAdapter()->getPathPrefix();
}

/**
Expand All @@ -174,7 +203,7 @@ public function getLocalCropsDirPath() {
* @param string $path Path to src image
*/
public function deleteSrc($path) {
$this->src_disk->delete($path);
$this->getSrcDisk()->delete($path);
}

/**
Expand All @@ -185,7 +214,8 @@ public function deleteSrc($path) {
*/
public function deleteCrops($path) {
$crops = $this->listCrops($path);
foreach($crops as $crop) $this->crops_disk->delete($crop);
$disk = $this->getCropsDisk();
foreach($crops as $crop) $disk->delete($crop);
return $crops;
}

Expand All @@ -198,7 +228,8 @@ public function deleteCrops($path) {
*/
public function deleteAllCrops($filter = null, $dry_run = false) {
$crops = $this->listAllCrops($filter);
if (!$dry_run) foreach($crops as $crop) $this->crops_disk->delete($crop);
$disk = $this->getCropsDisk();
if (!$dry_run) foreach($crops as $crop) $disk->delete($crop);
return $crops;
}

Expand Down Expand Up @@ -228,7 +259,7 @@ public function listCrops($path) {
if ($dir === '.') $dir = ''; // Flysystem doesn't like "." for the dir

// Filter the files in the dir to just crops of the image path
return $this->justPaths(array_filter($this->crops_disk->listContents($dir),
return $this->justPaths(array_filter($this->getCropsDisk()->listContents($dir),
function($file) use ($filename) {

// Don't return the source image, we're JUST getting crops
Expand All @@ -251,7 +282,7 @@ function($file) use ($filename) {
* @return array
*/
public function listAllCrops($filter = null) {
return $this->justPaths(array_filter($this->crops_disk->listContents(null, true),
return $this->justPaths(array_filter($this->getCropsDisk()->listContents(null, true),
function($file) use ($filter) {

// If there was a filter, force it to match
Expand All @@ -263,7 +294,7 @@ function($file) use ($filter) {
$src = $matches[1].'.'.$matches[5];

// Test that the src file exists
return $this->src_disk->has($src);
return $this->getSrcDisk()->has($src);
}));
}

Expand Down

0 comments on commit c8c73d8

Please sign in to comment.