Skip to content

Commit

Permalink
allow registration of guesser callback'
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 18, 2019
1 parent dee225f commit 150a63d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Illuminate/Auth/Access/Gate.php
Expand Up @@ -64,6 +64,13 @@ class Gate implements GateContract
*/ */
protected $stringCallbacks = []; protected $stringCallbacks = [];


/**
* The callback to be used to guess policy names.
*
* @var callable|null
*/
protected $guessPolicyNamesUsingCallback;

/** /**
* Create a new gate instance. * Create a new gate instance.
* *
Expand Down Expand Up @@ -550,11 +557,28 @@ public function getPolicyFor($class)
*/ */
protected function guessPolicyName($class) protected function guessPolicyName($class)
{ {
if ($this->guessPolicyNamesUsingCallback) {
return call_user_func($this->guessPolicyNamesUsingCallback, $class);
}

$classDirname = str_replace('/', '\\', dirname(str_replace('\\', '/', $class))); $classDirname = str_replace('/', '\\', dirname(str_replace('\\', '/', $class)));


return $classDirname.'\\Policies\\'.class_basename($class).'Policy'; return $classDirname.'\\Policies\\'.class_basename($class).'Policy';
} }

This comment has been minimized.

Copy link
@troccoli

troccoli Feb 20, 2019

Contributor

If my models are in the App\Models namespace, does it mean I need to put the policies in the App\Models\Policies namespace?

And that if I want the policies in the App\Policies namespace, do I need to use my own guessPolicyName?

This comment has been minimized.

Copy link
@simonschaufi

simonschaufi Feb 23, 2019

I also put my Models in a Models folder which just makes the root folder a lot cleaner. I would also like to know how to do the auto mapping magic @taylorotwell

This comment has been minimized.

Copy link
@andriihorpenko

andriihorpenko Mar 8, 2019

Ya, that would be a nice option



/**
* Specify a callback to be used to guess policy names.
*
* @param callable $callback
* @return $this
*/
public function guessPolicyNamesUsing(callable $callback)
{
$this->guessPolicyNamesUsingCallback = $callback;

return $this;
}

/** /**
* Build a policy class instance of the given type. * Build a policy class instance of the given type.
* *
Expand Down

3 comments on commit 150a63d

@devcircus
Copy link
Contributor

Choose a reason for hiding this comment

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

Just look at the last few lines of the guessPolicyName method for inspiration then assign your own callback to `guessPolicyNamesUsingCallback```.

@ludo237
Copy link

@ludo237 ludo237 commented on 150a63d Mar 8, 2019

Choose a reason for hiding this comment

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

An example would be nice for this.

@wolfuser99
Copy link

Choose a reason for hiding this comment

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

this works for me @ludo237

    public function boot()
    {
        Gate::guessPolicyNamesUsing(function ($class) {
            $pre = str_replace('/', '\\', 'App\\Policies\\' .
                class_basename(
                    str_replace('App\Models\\', '', $class)) . 'Policy');
//            dd($pre, $class);
            return $pre;
        });
        $this->registerPolicies();

        //
    }

Please sign in to comment.