Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Info] Accessing Internal and External filesystem with Storage facade #403

Open
hugovntr opened this issue Apr 5, 2022 · 0 comments
Open

Comments

@hugovntr
Copy link

hugovntr commented Apr 5, 2022

Hi everyone 馃憢

I started using Laravel Zero recently and I faced an issue that I could not, for the life of me, seem to fix. And that is accessing "internal" files with the Storage facade. It was working well when the app was not bundled into a Phar. I saw this issue #177 where people were experimenting with the Phar::runing() function and after a lot of debugging I think I found a viable solution.

My problem was that I wanted to access files that were bundled with the Phar, at the same level as the app folder for example. It was working well with the dirname(app_path()) trick when not bundled, but it completely failed when it was bundled into a Phar. So I came up with the following steps and it worked!

Step 1: Filesystem configuration

Create a new config/filesystems.php file with the following content:

<?php

return [
    'default' => 'local',
    'disks' => [
        'local' => [
            'driver' => 'local',
            // I usually place binaries in /bin, hence why the string manipulation
            // This part can be replace by `getcwd()`
            'root' => str(getcwd())->split('/\/bin/')->first()
        ],
        'internal' => [
            'driver' => 'local',
            'root' => (Phar::running(false))
                ? Phar::running() . DIRECTORY_SEPARATOR // phar://SOMETHING/app/
                : dirname(app_path())
        ]
    ]
];

If the Phar is running, it will return it's url + a directory separator, otherwise it will return the root folder of your Laravel Zero app.

Step 2: Include the folder in the box

Then you got to add the directory (in my case stubs) that you want to bundle with your app in the box.json:

{
    "chmod": "0755",
    "directories": [
        "app",
        "bootstrap",
        "config",
        "stubs", // Here
        "vendor"
    ],
    "files": [
        "composer.json"
    ],
    "exclude-composer-files": false,
    "compression": "GZ",
    "compactors": [
        "KevinGH\\Box\\Compactor\\Php",
        "KevinGH\\Box\\Compactor\\Json"
    ]
}

This way, when bundling the app, it will automatically include all the files included in that directory.

Step 3: Use the appropriate Storage Disk

Now, when you want to access a file in that folder (stubs) you can simply use the Storage facade with the internal disk:

Storage::disk('internal')->get('stubs/mystub.conf');

And it will work whether the app is bundled or not.


I know it's probably not the best way to share this, but I wanted other people to be able to experiment with it and see if it works for them before opening a pull request on the documentation.
All I know is I would have been very happy to find something like this when I was looking for it

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

No branches or pull requests

1 participant