-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from puleeno/ft/improve-hook-with-priority-and-…
…params Improve the hook manager: filter hooks and action hooks
- Loading branch information
Showing
8 changed files
with
173 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Constracts; | ||
|
||
interface HookConstract | ||
{ | ||
public function setPriority(int $priority); | ||
|
||
public function setCallable($callable); | ||
|
||
public function setParamsQuantity(int $quantity); | ||
|
||
public function getPriority(): int; | ||
|
||
public function getCallable(); | ||
|
||
public function getParamsQuantity(): int; | ||
|
||
public static function create($fn, $priority = 10, $paramQuantity = 1): HookConstract; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Core; | ||
|
||
use App\Constracts\HookConstract; | ||
use App\Exceptions\NotCallableException; | ||
|
||
abstract class Hook implements HookConstract | ||
{ | ||
protected $callable = null; | ||
|
||
protected $priority = 10; | ||
|
||
protected $paramsQuantity = 1; | ||
|
||
public function setPriority(int $priority) | ||
{ | ||
$this->priority = $priority; | ||
} | ||
|
||
public function setCallable($callable) | ||
{ | ||
if (!is_callable($callable)) { | ||
throw new NotCallableException(var_export($callable, true)); | ||
} | ||
$this->callable = $callable; | ||
} | ||
|
||
public function setParamsQuantity(int $quantity) | ||
{ | ||
$this->paramsQuantity = $quantity; | ||
} | ||
|
||
public function getPriority(): int | ||
{ | ||
return $this->priority; | ||
} | ||
|
||
public function getCallable() | ||
{ | ||
return $this->callable; | ||
} | ||
|
||
public function getParamsQuantity(): int | ||
{ | ||
return $this->paramsQuantity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Core\Hooks; | ||
|
||
use App\Core\Hook; | ||
|
||
class ActionHook extends Hook | ||
{ | ||
public static function create($fn, $priority = 10, $paramsQuantity = 1): ActionHook | ||
{ | ||
$hook = new ActionHook(); | ||
|
||
$hook->setCallable($fn); | ||
$hook->setPriority($priority); | ||
$hook->setParamsQuantity($paramsQuantity); | ||
|
||
return $hook; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Core\Hooks; | ||
|
||
use App\Core\Hook; | ||
|
||
class FilterHook extends Hook | ||
{ | ||
public static function create($fn, $priority = 10, $paramsQuantity = 1): FilterHook | ||
{ | ||
$hook = new FilterHook(); | ||
|
||
$hook->setCallable($fn); | ||
$hook->setPriority($priority); | ||
$hook->setParamsQuantity($paramsQuantity); | ||
|
||
return $hook; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters