PHP code example of jenryollivierre / php-application-hooks
1. Go to this page and download the library: Download jenryollivierre/php-application-hooks library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jenryollivierre / php-application-hooks example snippets
use JenryOllivierre\Hooks\Hooks;
$hooks = new Hooks;
// The point in our application where we want to allow 3rd party apps to do something.
// Example - Let's allow 3rd party apps to do anything they want before a post is deleted
$postId = getPostId();
$canDelete = canPostBeDeleted($postId);
$hooks->applyActions('before_post_delete', [$postId, $canDelete]);
// Example in the 3rd party app - Let's clean up the database before we delete the post
$hooks->addAction('before_post_delete', [new Post, 'cleanUpDatabase'], 10);
class Post
{
/**
* @param int $postId
* @param bool $canDelete
* @return void
*/
public function cleanUpDatabase($postId, $canDelete)
{
// Do stuff
}
}
// Example of code in our application
// Let's allow 3rd party apps to determine if the post should be deleted
$postId = getPostId();
if ($hooks->applyFilters('user_can_delete_post', false, [$postId])) {
deletePost($postId);
}
// Example in our 3rd party app - Let's check to see if the post should be allowed to be deleted
// Closure Example
$hooks->addFilter('user_can_delete_post', function ($value, $postId) {
// The value to filter is always the first parameter passed to your callback
// Any other parameters passed through the 'applyFilters()' method will be available in the order they were passed to the array
$post = getPost($postId);
$user = getCurrentUser();
if ($user->id === $post->author) {
return true;
}
// Tip:: Always return the original value if your conditionals doesn't match
return $value;
});
// Function name example
$hooks->addFilter('user_can_delete_post', 'someFunctionName', 10, 2);
function someFunctionName($value, $postId)
{
$post = getPost($postId);
$user = getCurrentUser();
if ($user->id === $post->author->id) {
return true;
}
return $value;
}
// Class instance example
$hooks->addFilter('user_can_delete_post', [Post, 'userCanDeletePost'], 10, 1);
// In our Post class
class Post
{
public function userCanDeletePost($canDelete)
{
if (getCurrentUser() === 'super_admin') {
return true;
}
return $canDelete;
}
}
namespace MyNameSpace\Task;
use JenryOllivierre\Hooks\HooksFoundation;
class AppHooks extends HooksFoundation implements Filterable, Actionable
{
use HasActions;
use HasFilters;
}
namespace MyNameSpace\Task;
use JenryOllivierre\Hooks\HooksFoundation;
class Hooks extends HooksFoundation
{
/**
* Add a task hook.
*
* @param string $name
* @param callable $callback
* @param int $priority
* @param int $params
* @return void
*/
public function addAppTask(string $name, callable $callback, int $priority = 100, int $params = 0)
{
$this->storeHook('app_tasks', $name, $callback, $priority, $params);
}
/**
* Apply all the callbacks that was added to a specific task.
*
* @since 1.0
* @param string $name
* @param array $args
* @param bool $return
* @return mixed|void
*/
public function applyAppTasks(string $name, array $args, bool $return = false)
{
return $this->resolveTasks('app_tasks', $name, $args, $return);
}
}
/**
* Add a hook.
* @param string $type
* @param string $name
* @param callable $callback
* @param int $priority
* @param int $params
* @return void
*/
protected function storeHook(string $type, $name, callable $callback, int $priority = 100, int $params = 1);
/**
* Resolve all the values for a given hook.
* @since 1.0
* @param string $hookType
* @param string $name
* @param mixed $value
* @param array $args
* @return mixed
*/
protected function resolveValues(string $hookType, string $name, $value, array $args = []);
/**
* Resolve all the tasks for a given hook.
* @since 1.0
* @param string $hookType
* @param string $name
* @param array $args
* @param bool $return
* @return void
*/
protected function resolveTasks(string $hookType, string $name, array $args = [], bool $return = false);
/**
* Check if anything have been added to a particular hook type.
* @since 1.0
* @param string $type
* @return bool
*/
protected function hookTypeExists(string $type);
/**
* Check if there has been anything set for a specific hook, for a
* particular hook type.
* @since 1.0
* @param string $hookType
* @param string $name
* @return bool
*/
protected function hookExistsByType(string $hookType, string $name);
/**
* Get everything that has been added to a specific hook type.
* @since 1.0
* @param string $type
* @return array
*/
protected function getAllHooksByType(string $type);
/**
* Remove all hooks from a hook type.
* @since 1.0
* @param string $type
* @return void
*/
protected function removeAllHooksFromType(string $type)
/**
* Get everything that have been added to a specific hook, for a
* particular hook type.
* @since 1.0
* @param string $type
* @param string $name
* @return array
*/
protected function getHookByType(string $type, string $name);
/**
* Remove a hook type.
* @since 1.0
* @param string $type
* @return void
*/
protected function removeHookType(string $type);
/**
* Remove a specific hook for a particular hook type.
* @since 1.0
* @param string $hookType
* @param string $name
* @return void
*/
protected function removeHookByType(string $hookType, string $name);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.