statamic / eloquent-driver

Provides support for storing your Statamic data in a database, rather than flat files.
https://statamic.dev/tips/storing-content-in-a-database
MIT License
104 stars 71 forks source link

Separator for namespace and handle for Blueprints #237

Closed andreas-eisenmann closed 5 months ago

andreas-eisenmann commented 5 months ago

In statamic/cms whe have :: as separator for namespace and handle when loading a blueprint, see this line.

So we can call:

\Statamic\Facades\Blueprint::find("foo::bar");

Now "foo" is treated as namespace and "bar" as the handle.

Let's peek into the counterpart in statamic/eloquent-driver at this line. Here we are not able to use :: as a separator, but instead we have to use the dot . es separator.

We use statamic-rad-pack/runway in combination with statamic/eloquent-driver, so we store our blueprints in the database.

This code won't work with the eloquent driver:

    public function blueprint(): Blueprint
    {
        $blueprint = Blueprint::find("runway::{$this->handle}");

        if (! $blueprint) {
            $blueprint = Blueprint::make($this->handle)->setNamespace('runway')->save();
        }

        return $blueprint;
    }

$blueprint will always be null. If we call Blueprint::find("runway.{$this->handle}"); (so with a dot as the separator), everything works fine.

Just spent a few seconds to think about in which project to report this issue, but for me it seems clear that if statamic/cms supports :: as a separator to distinguish between namespace and handle when loading a blueprint, the eloquent driver should also support this behaviour to stay compatible? Developers should rely on that if they use a Facade of statamic (e.g. \Statamic\Facades\Blueprint) in their app code and switch to the eloquent driver that everything works the same as before.

ryanmitchell commented 5 months ago

I've opened a PR here - https://github.com/statamic/eloquent-driver/pull/238 let me know if this works as expected

andreas-eisenmann commented 5 months ago

Thanks for the fast reply! 🚀

Yes, this would work generally, but there's a little syntax error () instead of ]).

And I think you could shrink it to:

        if (str_contains($blueprint, '::')) {
            return explode('::', $blueprint);
        }
ryanmitchell commented 5 months ago

Already fixed that