Skip to content

[5.7] Command to create custom filesystem links#26194

Closed
browner12 wants to merge 3 commits intolaravel:5.7from
browner12:links
Closed

[5.7] Command to create custom filesystem links#26194
browner12 wants to merge 3 commits intolaravel:5.7from
browner12:links

Conversation

@browner12
Copy link
Copy Markdown
Contributor

Currently we have the ability to generate a filesystem link from /public/storage to /storage/app/public using the storage:link artisan command. AFAIK, any other links would need to be created with a manual process, or a custom script.

This PR gives the programmer the ability to define all the links they would like created in their application programmatically. Links and their targets are defined in the config/filesystems.php file.

'links' => [
    public_path('storage') => storage_path('app/public'),
    public_path('images') => storage_path('app/images'),
],

These links will be generated using the new links:generate command, which the programmer can make a part of their deployment process.

This change is better for the user because it gives them full control over the generation of links, rather than being stuck with only the default public_path('storage') => storage_path('app/public'). Also, for better or worse, the user can opt to use relative links over absolute links if they so choose ('public/storage' => 'storage/app/public').

Originally I was going to adapt the StorageLinkCommand for these changes, but I felt the naming would not appropriately reflect what it now does, so I opted for a completely new command. Also, this allows us to add it to 5.7 instead of 5.8, because we will not affect current users. My intention is the StorageLinkCommand will be removed in 5.8 in favor of this.

Obviously open to changing any naming.

If this PR is accepted, I would like to go back and add an option to force the deletion and recreation of the link, in case an existing link is updated to point to a new target. Something like:

php artisan links:generate --force

this command will generate OS links in the filesystem, based on a simple configuration array.
@TBlindaruk
Copy link
Copy Markdown
Contributor

Could you also update the documentation?

@browner12
Copy link
Copy Markdown
Contributor Author

once it gets accepted.

if ($links = $this->laravel['config']['filesystems.links']) {
foreach ($links as $link => $target) {
if (file_exists($link)) {
$this->error("The [$link] directory already exists.");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be confusing, if the location is not a directory. Perhaps do an is_file check to provide a more useful error message? That is, to tell the user if there is a file or a directory already there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm a little confused. This is checking for the existence of the link, not the target. Would something like "The [$link] link already exists." make more sense?

@taylorotwell
Copy link
Copy Markdown
Member

Eh, I think this sounds more like a package.

@browner12
Copy link
Copy Markdown
Contributor Author

Sorry, but I would have to disagree with this suggestion. We're taking an existing framework functionality (creating links) and making it user customizable, rather than only working with the one pre-defined link decided on by the framework.

This is very similar to what we've done other places in the FW where we've taken a manual part of deployment and tuned it into code, so we can consistently and automatically deploy it. For example, rather that SSHing into a server and updating CRON entries, we turned it into code. Rather than logging into MySQL and creating tables and fields, we write migrations. This is exactly the same. Rather than SSHing into the server and creating the links, or using something like Envoyer to create the links in a manual and inconsistent manner, we have the links in code and add the command to our deploy process.

This is along the lines of infrastructure as code, which I think is a great thing because it makes things consistent and automated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants