8000 [9.x] Unify trait set up/tear down and other hooks under a `Hook` implementation by inxilpro · Pull Request #39947 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[9.x] Unify trait set up/tear down and other hooks under a Hook implementation #39947

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement hook support on Models
  • Loading branch information
inxilpro committed Dec 8, 2021
commit 40a91568c7a2d3cc001a15a07e801de79cd057cd
8000
14 changes: 14 additions & 0 deletions src/Illuminate/Contracts/Support/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,18 @@ public function run($instance, array $arguments = []);
* @param array $arguments
*/
public function cleanup($instance, array $arguments = []);

/**
* Get the name of the hook.
*
* @return string
*/
public function getName();

/**
* Get the priority of the hook.
*
* @return int
*/
public function getPriority();
}
54 changes: 26 additions & 28 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Hookable;
use Illuminate\Support\Hooks\TraitHook;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use JsonSerializable;
Expand All @@ -32,7 +34,8 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
Concerns\HasTimestamps,
Concerns\HidesAttributes,
Concerns\GuardsAttributes,
ForwardsCalls;
ForwardsCalls,
Hookable;

/**
* The connection name for the model.
Expand Down Expand Up @@ -252,37 +255,34 @@ protected static function boot()
static::bootTraits();
}

/**
* Register hook for `bootX` trait methods.
*
* @return \Illuminate\Support\Hooks\TraitHook
*/
public static function registerBootHook(): TraitHook
{
return new TraitHook('boot');
}

/**
* Register hook for `initializeX` trait methods.
*
* @return \Illuminate\Support\Hooks\TraitHook
*/
public static function registerInitializeHook(): TraitHook
{
return new TraitHook('initialize');
}

/**
* Boot all of the bootable traits on the model.
*
* @return void
*/
protected static function bootTraits()
{
$class = static::class;

$booted = [];

static::$traitInitializers[$class] = [];

// TODO: Switch to Hookable
foreach (class_uses_recursive($class) as $trait) {
$method = 'boot'.class_basename($trait);

if (method_exists($class, $method) && ! in_array($method, $booted)) {
forward_static_call([$class, $method]);

$booted[] = $method;
}

if (method_exists($class, $method = 'initialize'.class_basename($trait))) {
static::$traitInitializers[$class][] = $method;

static::$traitInitializers[$class] = array_unique(
static::$traitInitializers[$class]
);
}
}
static::runStaticHooks('boot');
}

/**
Expand All @@ -292,9 +292,7 @@ protected static function bootTraits()
*/
protected function initializeTraits()
{
foreach (static::$traitInitializers[static::class] as $method) {
$this->{$method}();
}
$this->runHooks('initialize');
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/Illuminate/Support/Hooks/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ public function cleanup($instance, array $arguments = [])
$this->runCallback($this->cleanup, $instance, $arguments);
}

/**
* @inheritdoc
*/
public function getName()
{
return $this->name;
}

/**
* @inheritDoc
*/
public function getPriority()
{
return $this->priority;
}

/**
* Run a callback that may or may not be static.
*
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Hooks/HookCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Support\Hooks;

use Closure;
use Illuminate\Contracts\Support\Hook;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use ReflectionClass;
Expand Down Expand Up @@ -102,8 +103,8 @@ public function run($name, $instance, $arguments = [], Closure $callback = null)

$hooks = $this->onlyStatic(! is_object($instance))
->resolve($instance)
->where('name', $name)
->sortBy('priority');
->filter(fn(Hook $hook) => $hook->getName() === $name)
->sortBy(fn(Hook $hook) => $hook->getPriority());

$hooks->each(fn (Hook $hook) => $hook->run($instance, $arguments));

Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Hooks/PendingHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Support\Hooks;

use Closure;
use Illuminate\Contracts\Support\Hook;
use RuntimeException;

class PendingHook
Expand Down
16 changes: 16 additions & 0 deletions src/Illuminate/Support/Hooks/TraitHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,20 @@ public function cleanup($instance, array $arguments = [])
{
// No cleanup is necessary
}

/**
* @inheritdoc
*/
public function getName()
{
return $this->prefix;
}

/**
* @inheritDoc
*/
public function getPriority()
{
return $this->priority;
}
}
0