Skip to content

Commit

Permalink
feat: main upload of v2.0.0 version
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
josantonius committed Jul 30, 2022
1 parent be22bcb commit 46ce486
Show file tree
Hide file tree
Showing 13 changed files with 847 additions and 230 deletions.
90 changes: 90 additions & 0 deletions src/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

/*
* This file is part of https://github.com/josantonius/php-hook repository.
*
* (c) Josantonius <hello@josantonius.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Josantonius\Hook;

/**
* Action hook instance.
*/
class Action
{
/**
* If the action was done.
*/
private bool $done = false;

/**
* Callback result.
*/
private mixed $result = null;

/**
* Create new action instance.
*/
public function __construct(
private $callback,
private int $priority,
private bool $once
) {
}

/**
* Gets action callback.
*/
public function getCallback(): callable
{
return $this->callback;
}

/**
* Gets action priority.
*/
public function getPriority(): int
{
return $this->priority;
}

/**
* Gets callback result.
*/
public function getResult(): mixed
{
return $this->result;
}

/**
* True if the action is called only once and deleted.
*/
public function isOnce(): bool
{
return $this->once;
}

/**
* Run action callback.
*/
public function runCallback(...$arguments): void
{
$this->result = $this->getCallback()(...$arguments);

$this->done = true;
}

/**
* If the action has already been done.
*/
public function wasDone(): bool
{
return $this->done === true;
}
}
23 changes: 23 additions & 0 deletions src/Exceptions/HookException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of https://github.com/josantonius/php-hook repository.
*
* (c) Josantonius <hello@josantonius.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Josantonius\Hook\Exceptions;

/**
* Hook exception manager.
*/
class HookException extends \Exception
{
public function __construct(string $message = 'Unknown error')
{
parent::__construct(rtrim($message, '.') . '.');
}
}
Loading

0 comments on commit 46ce486

Please sign in to comment.