-
-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[4.x] This adds support for tenancy aware Storage::url() method #689
Closed
+448
−9
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
adf3daa
This adds support for tenancy aware Storage::url() method
dontfreakout 717b834
Trigger CI build
stancl 5ed5aea
Fixed Link command for Laravel v6, added StorageLink Events, more Sto…
dontfreakout accbf5b
Merge branch 'master' into feature/storage-url
stancl 14c87ec
fix duplicate use statemenet
stancl 5002b30
Update FilesystemTenancyBootstrapper
lukinovec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Stancl\Tenancy\Concerns\HasATenantsOption; | ||
use Stancl\Tenancy\Contracts\Tenant; | ||
|
||
class Link extends Command | ||
{ | ||
use HasATenantsOption; | ||
|
||
/** | ||
* The console command signature. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'tenants:link | ||
{--tenants=* : The tenant(s) to run the command for. Default: all} | ||
{--relative : Create the symbolic link using relative paths} | ||
{--force : Recreate existing symbolic links} | ||
{--remove : Remove symbolic links}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create the symbolic links configured for the tenancy applications'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
$relative = $this->option('relative'); | ||
|
||
if ($this->option('remove')) { | ||
foreach ($this->links() as $link => $target) { | ||
if (is_link($link)) { | ||
$this->laravel->make('files')->delete($link); | ||
|
||
$this->info("The [$link] link has been removed."); | ||
} | ||
} | ||
|
||
$this->info('The links have been removed.'); | ||
|
||
return; | ||
} | ||
|
||
foreach ($this->links() as $link => $target) { | ||
if (file_exists($link) && ! $this->isRemovableSymlink($link, $this->option('force'))) { | ||
$this->error("The [$link] link already exists."); | ||
continue; | ||
} | ||
|
||
if (is_link($link)) { | ||
$this->laravel->make('files')->delete($link); | ||
} | ||
|
||
if ($relative) { | ||
$this->laravel->make('files')->relativeLink($target, $link); | ||
} else { | ||
$this->laravel->make('files')->link($target, $link); | ||
} | ||
|
||
$this->info("The [$link] link has been connected to [$target]."); | ||
} | ||
|
||
$this->info('The links have been created.'); | ||
} | ||
|
||
/** | ||
* Get the symbolic links that are configured for the application. | ||
* | ||
* @return array | ||
*/ | ||
protected function links() | ||
{ | ||
$disk_urls = config('tenancy.filesystem.url_override'); | ||
$disks = config('tenancy.filesystem.root_override'); | ||
$suffix_base = config('tenancy.filesystem.suffix_base'); | ||
|
||
$tenants = $this->option('remove') && filled($this->option('tenants')) | ||
? collect($this->option('tenants')) | ||
: $this->getTenants()->map(function(Tenant $tenant) { return $tenant->getTenantKey(); }); | ||
|
||
return $tenants | ||
->map(function ($tenant_key) use ($suffix_base, $disk_urls, $disks) { | ||
|
||
$map = []; | ||
|
||
foreach ($disk_urls as $disk => $public_path) { | ||
$storage_path = str_replace('%storage_path%', $suffix_base . $tenant_key, $disks[$disk]); | ||
$storage_path = storage_path($storage_path); | ||
|
||
$public_path = str_replace('%tenant_id%', $tenant_key, $public_path); | ||
$public_path = public_path($public_path); | ||
|
||
// make sure storage path exist before we create symlink | ||
if (! is_dir($storage_path)) { | ||
mkdir($storage_path, 0777, true); | ||
} | ||
|
||
$map[] = [$public_path => $storage_path]; | ||
} | ||
|
||
return $map; | ||
|
||
})->flatten(1) | ||
->mapWithKeys(function ($item) {return $item; }) | ||
->all(); | ||
} | ||
|
||
/** | ||
* Determine if the provided path is a symlink that can be removed. | ||
* | ||
* @param string $link | ||
* @param bool $force | ||
* @return bool | ||
*/ | ||
protected function isRemovableSymlink(string $link, bool $force): bool | ||
{ | ||
return is_link($link) && $force; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Events; | ||
|
||
class CreatingStorageSymlink extends Contracts\TenantEvent | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Events; | ||
|
||
class RemovingStorageSymlink extends Contracts\TenantEvent | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Events; | ||
|
||
class StorageSymlinkCreated extends Contracts\TenantEvent | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Events; | ||
|
||
class StorageSymlinkRemoved extends Contracts\TenantEvent | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\Artisan; | ||
use Stancl\Tenancy\Contracts\Tenant; | ||
use Stancl\Tenancy\Events\CreatingStorageSymlink; | ||
use Stancl\Tenancy\Events\StorageSymlinkCreated; | ||
|
||
class CreateStorageSymlinks implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* @var \Stancl\Tenancy\Contracts\Tenant | ||
*/ | ||
public Tenant $tenant; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Tenant $tenant) | ||
{ | ||
$this->tenant = $tenant; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
event(new CreatingStorageSymlink($this->tenant)); | ||
|
||
Artisan::call('tenants:link', [ | ||
'--tenants' => [$this->tenant->getTenantKey()], | ||
]); | ||
|
||
event(new StorageSymlinkCreated($this->tenant)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getDiskConfig()
method doesn't make much sense. If you're returning null, but using['driver']
on the return value, that will return in exceptions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, there should be better error handling (would the exception be different from the ones that pieces of the original code would throw?). But if there's no disk with the passed name, the method should return null, that makes sense to me.