phirschybar
phirschybar

Reputation: 8579

Laravel - Possible to detect if package is running from workbench or vendor?

I am sure with some hacking I could find a way to do this but I am hoping there is an elegant solution. I need to be able to detect when my package is running from the workbench of if the package is installed and running as a 3rd party.

Upvotes: 1

Views: 1071

Answers (1)

mpj
mpj

Reputation: 5367

You can create a method in your main package class to do the check, like this:

MyClass {

    protected function getPackageLocation()
    {
        // Check if the path to this file contains 'workbench'
        if (strpos(realpath(__FILE__), 'workbench') !== false) {
            return 'workbench';
        }

        // If not, it's in vendor folder
        return 'vendor';
    }

}

If you need to check it from outside your package, you can always make the function public.

To make it more reliable, you can check for workbench/your_vendor/your_package/in the conditional, or even make it dynamic with something like:

// untested: translate namespace to path format
$needle = 'workspace/' . strtolower(str_replace("_", "/", __NAMESPACE__));

if (strpos(realpath(__FILE__), $needle) !== false) {
    ...

Upvotes: 2

Related Questions