You've already forked Php-Template
Compare commits
2 Commits
v1.5.0
...
e0ba77556d
| Author | SHA1 | Date | |
|---|---|---|---|
|
e0ba77556d
|
|||
|
f8b988ca0d
|
@@ -1,10 +0,0 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
|
||||||
<configuration default="false" name=" Compose Deployment" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
|
|
||||||
<deployment type="docker-compose.yml">
|
|
||||||
<settings>
|
|
||||||
<option name="sourceFilePath" value="docker-compose.yml" />
|
|
||||||
</settings>
|
|
||||||
</deployment>
|
|
||||||
<method v="2" />
|
|
||||||
</configuration>
|
|
||||||
</component>
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
|
||||||
<configuration default="false" name="All" type="ComposerRunConfigurationType" factoryName="Composer Script">
|
|
||||||
<option name="commandLineParameters" value="" />
|
|
||||||
<option name="pathToComposerJson" value="$PROJECT_DIR$/composer.json" />
|
|
||||||
<option name="script" value="tests:all" />
|
|
||||||
<method v="2" />
|
|
||||||
</configuration>
|
|
||||||
</component>
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
|
||||||
<configuration default="false" name="Lint:fix" type="ComposerRunConfigurationType" factoryName="Composer Script">
|
|
||||||
<option name="commandLineParameters" value="" />
|
|
||||||
<option name="pathToComposerJson" value="$PROJECT_DIR$/composer.json" />
|
|
||||||
<option name="script" value="tests:lint:fix" />
|
|
||||||
<method v="2" />
|
|
||||||
</configuration>
|
|
||||||
</component>
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
|
||||||
<configuration default="false" name="Main" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
|
|
||||||
<CommandLine>
|
|
||||||
<PhpTestInterpreterSettings>
|
|
||||||
<option name="interpreterName" value="composer-runtime" />
|
|
||||||
</PhpTestInterpreterSettings>
|
|
||||||
</CommandLine>
|
|
||||||
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml" scope="XML" use_alternative_configuration_file="true" />
|
|
||||||
<method v="2" />
|
|
||||||
</configuration>
|
|
||||||
</component>
|
|
||||||
@@ -51,7 +51,7 @@ return [
|
|||||||
],
|
],
|
||||||
|
|
||||||
'queue' => [
|
'queue' => [
|
||||||
'broker' => Env::get('QUEUE_BROKER', 'redis'),
|
'broker' => Env::get('QUEUE_BROKER', 'kafka'),
|
||||||
|
|
||||||
'broker_config' => [
|
'broker_config' => [
|
||||||
|
|
||||||
@@ -61,7 +61,6 @@ return [
|
|||||||
|
|
||||||
'kafka' => [
|
'kafka' => [
|
||||||
'brokers' => Env::get('QUEUE_KAFKA_BROKERS', 'kafka:9092'),
|
'brokers' => Env::get('QUEUE_KAFKA_BROKERS', 'kafka:9092'),
|
||||||
'consumerGroup' => Env::get('QUEUE_KAFKA_CONSUMER_GROUP', 'default_group'),
|
|
||||||
],
|
],
|
||||||
|
|
||||||
'rabbitmq' => [
|
'rabbitmq' => [
|
||||||
|
|||||||
@@ -83,7 +83,6 @@ services:
|
|||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
environment:
|
environment:
|
||||||
QUEUE_BROKER: redis
|
|
||||||
PHP_IDE_CONFIG: serverName=localhost
|
PHP_IDE_CONFIG: serverName=localhost
|
||||||
WORKERS: 1
|
WORKERS: 1
|
||||||
DEBUG: 1
|
DEBUG: 1
|
||||||
|
|||||||
@@ -6,28 +6,16 @@ namespace Siteworxpro\App\Annotations\Async;
|
|||||||
|
|
||||||
use Attribute;
|
use Attribute;
|
||||||
|
|
||||||
/**
|
|
||||||
* Attribute to mark a class as a handler for a specific message class in an async workflow.
|
|
||||||
*
|
|
||||||
* Repeatable: attach multiple times to handle multiple message classes.
|
|
||||||
*/
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
||||||
readonly class HandlesMessage
|
readonly class HandlesMessage
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Create a new HandlesMessage attribute.
|
|
||||||
*
|
|
||||||
* @param class-string $messageClass Fully-qualified class name of the message handled.
|
|
||||||
*/
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $messageClass,
|
public string $messageClass,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the fully-qualified message class this handler processes.
|
* @return string
|
||||||
*
|
|
||||||
* @return class-string
|
|
||||||
*/
|
*/
|
||||||
public function getMessageClass(): string
|
public function getMessageClass(): string
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,22 +6,9 @@ namespace Siteworxpro\App\Annotations\Events;
|
|||||||
|
|
||||||
use Attribute;
|
use Attribute;
|
||||||
|
|
||||||
/**
|
|
||||||
* Attribute to mark a class as an event listener for a specific event class.
|
|
||||||
*
|
|
||||||
* Apply this attribute to classes that subscribe to domain or application events.
|
|
||||||
* Repeatable: can be attached multiple times to the same class to listen for multiple events.
|
|
||||||
*
|
|
||||||
* Targets: class only.
|
|
||||||
*/
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
|
||||||
readonly class ListensFor
|
readonly class ListensFor
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Initialize the ListensFor attribute.
|
|
||||||
*
|
|
||||||
* @param class-string $eventClass Fully-qualified class name of the event to listen for.
|
|
||||||
*/
|
|
||||||
public function __construct(public string $eventClass)
|
public function __construct(public string $eventClass)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,47 +7,22 @@ namespace Siteworxpro\App\Annotations\Guards;
|
|||||||
use Attribute;
|
use Attribute;
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
|
|
||||||
/**
|
|
||||||
* Attribute to guard classes or methods with JWT claim requirements.
|
|
||||||
*
|
|
||||||
* Apply this attribute to a class or method to declare the expected JWT issuer and/or audience.
|
|
||||||
* If either the issuer or audience is an empty string, the value will be resolved from configuration:
|
|
||||||
* - `jwt.issuer`
|
|
||||||
* - `jwt.audience`
|
|
||||||
*
|
|
||||||
* Targets: class or method.
|
|
||||||
*/
|
|
||||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
|
||||||
readonly class Jwt
|
readonly class Jwt
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Initialize the Jwt attribute with optional overrides for expected JWT claims.
|
|
||||||
*
|
|
||||||
* @param string $issuer Optional expected JWT issuer (`iss`). Empty string uses `Config::get('jwt.issuer')`.
|
|
||||||
* @param string $audience Optional expected JWT audience (`aud`). Empty string uses `Config::get('jwt.audience')`.
|
|
||||||
*/
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private string $issuer = '',
|
private string $issuer = '',
|
||||||
private string $audience = '',
|
private string $audience = '',
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the required audience from configuration, ignoring any local override.
|
|
||||||
*
|
|
||||||
* @return string The globally configured audience or an empty string if not set.
|
|
||||||
*/
|
|
||||||
public function getRequiredAudience(): string
|
public function getRequiredAudience(): string
|
||||||
{
|
{
|
||||||
return Config::get('jwt.audience') ?? '';
|
return Config::get('jwt.audience') ?? '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the expected audience for validation.
|
* @return string
|
||||||
*
|
|
||||||
* Returns the constructor-provided audience when non-empty; otherwise falls back to `jwt.audience` config.
|
|
||||||
*
|
|
||||||
* @return string The audience value to enforce.
|
|
||||||
*/
|
*/
|
||||||
public function getAudience(): string
|
public function getAudience(): string
|
||||||
{
|
{
|
||||||
@@ -58,13 +33,6 @@ readonly class Jwt
|
|||||||
return $this->audience;
|
return $this->audience;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the expected issuer for validation.
|
|
||||||
*
|
|
||||||
* Returns the constructor-provided issuer when non-empty; otherwise falls back to `jwt.issuer` config.
|
|
||||||
*
|
|
||||||
* @return string The issuer value to enforce.
|
|
||||||
*/
|
|
||||||
public function getIssuer(): string
|
public function getIssuer(): string
|
||||||
{
|
{
|
||||||
if ($this->issuer === '') {
|
if ($this->issuer === '') {
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ readonly class Scope
|
|||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private array $scopes = []
|
private array $scopes = []
|
||||||
) {
|
) {}
|
||||||
}
|
|
||||||
|
|
||||||
public function getScopes(): array
|
public function getScopes(): array
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -24,12 +24,12 @@ class Kafka extends Broker
|
|||||||
|
|
||||||
$conf = new Conf();
|
$conf = new Conf();
|
||||||
$conf->set('bootstrap.servers', $config['brokers'] ?? 'localhost:9092');
|
$conf->set('bootstrap.servers', $config['brokers'] ?? 'localhost:9092');
|
||||||
|
|
||||||
$this->producer = new Producer($conf);
|
$this->producer = new Producer($conf);
|
||||||
$this->producer->addBrokers($config['brokers'] ?? 'localhost:9092');
|
$this->producer->addBrokers($config['brokers'] ?? 'localhost:9092');
|
||||||
|
|
||||||
$conf->set('group.id', $config['consumerGroup'] ?? 'default');
|
$conf->set('group.id', $config['consumerGroup'] ?? 'default');
|
||||||
$conf->set('auto.offset.reset', 'earliest');
|
$conf->set('auto.offset.reset', 'earliest');
|
||||||
|
|
||||||
$this->consumer = new KafkaConsumer($conf);
|
$this->consumer = new KafkaConsumer($conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,14 +60,6 @@ class Kafka extends Broker
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($kafkaMessage->err === RD_KAFKA_RESP_ERR_UNKNOWN_TOPIC_OR_PART) {
|
|
||||||
throw new \RuntimeException(
|
|
||||||
"Topic '{$queue->queueName()}' or partition does not exist. Kafka does not auto-create topics" .
|
|
||||||
" unless configured to do so."
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @var string | null $messageData */
|
|
||||||
$messageData = $kafkaMessage->payload;
|
$messageData = $kafkaMessage->payload;
|
||||||
if ($messageData !== null) {
|
if ($messageData !== null) {
|
||||||
/** @var Message $message */
|
/** @var Message $message */
|
||||||
|
|||||||
@@ -5,96 +5,87 @@ declare(ticks=1);
|
|||||||
namespace Siteworxpro\App\Async;
|
namespace Siteworxpro\App\Async;
|
||||||
|
|
||||||
use Siteworxpro\App\Annotations\Async\HandlesMessage;
|
use Siteworxpro\App\Annotations\Async\HandlesMessage;
|
||||||
use Siteworxpro\App\Async\Messages\Message;
|
|
||||||
use Siteworxpro\App\Async\Queues\Queue;
|
use Siteworxpro\App\Async\Queues\Queue;
|
||||||
use Siteworxpro\App\Services\Facades\Broker;
|
|
||||||
use Siteworxpro\App\Services\Facades\Logger;
|
use Siteworxpro\App\Services\Facades\Logger;
|
||||||
|
|
||||||
/**
|
|
||||||
* Long-running process that listens to queues, pops messages, and dispatches them to handlers.
|
|
||||||
*/
|
|
||||||
class Consumer
|
class Consumer
|
||||||
{
|
{
|
||||||
private static bool $shutDown = false;
|
private static bool $shutDown = false;
|
||||||
|
|
||||||
/** @var array<string,string> */
|
|
||||||
private const array QUEUES = [
|
private const array QUEUES = [
|
||||||
'default' => Queues\DefaultQueue::class,
|
'default' => Queues\DefaultQueue::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/** @var Queue[] */
|
|
||||||
private array $queues = [];
|
private array $queues = [];
|
||||||
|
|
||||||
/** @var array<string, string[]> message FQCN => handler FQCNs */
|
|
||||||
private array $handlers = [];
|
private array $handlers = [];
|
||||||
|
|
||||||
private const string HANDLER_NAMESPACE = 'Siteworxpro\\App\\Async\\Handlers\\';
|
private const string HANDLER_NAMESPACE = 'Siteworxpro\\App\\Async\\Handlers\\';
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string[] $queues Optional list of queue names (keys from self::QUEUES)
|
|
||||||
*/
|
|
||||||
public function __construct(array $queues = [])
|
public function __construct(array $queues = [])
|
||||||
{
|
{
|
||||||
$queueClasses = $queues === []
|
if ($queues === []) {
|
||||||
? array_values(self::QUEUES)
|
$queues = self::QUEUES;
|
||||||
: array_map(
|
} else {
|
||||||
static function (string $name): string {
|
$mappedQueues = [];
|
||||||
if (!isset(self::QUEUES[$name])) {
|
foreach ($queues as $queueName) {
|
||||||
throw new \InvalidArgumentException("Queue '$name' is not defined.");
|
if (isset(self::QUEUES[$queueName])) {
|
||||||
|
$mappedQueues[] = self::QUEUES[$queueName];
|
||||||
|
} else {
|
||||||
|
throw new \InvalidArgumentException("Queue '$queueName' is not defined.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$queues = $mappedQueues;
|
||||||
}
|
}
|
||||||
return self::QUEUES[$name];
|
|
||||||
},
|
|
||||||
$queues
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach ($queueClasses as $class) {
|
|
||||||
$this->queues[] = new $class();
|
foreach ($queues as $queueClass) {
|
||||||
|
$this->queues[] = new $queueClass();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->registerHandlers();
|
$this->registerHandlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Discover handler classes under `Handlers` and register them via HandlesMessage attributes.
|
|
||||||
*/
|
|
||||||
private function registerHandlers(): void
|
private function registerHandlers(): void
|
||||||
{
|
{
|
||||||
$it = new \RecursiveIteratorIterator(
|
$recursiveIterator = new \RecursiveIteratorIterator(
|
||||||
new \RecursiveDirectoryIterator(__DIR__ . '/Handlers/')
|
new \RecursiveDirectoryIterator(__DIR__ . '/Handlers/')
|
||||||
);
|
);
|
||||||
|
|
||||||
/** @var \SplFileInfo $file */
|
foreach ($recursiveIterator as $file) {
|
||||||
foreach ($it as $file) {
|
if ($file->isFile() && $file->getExtension() === 'php') {
|
||||||
if (!$file->isFile() || $file->getExtension() !== 'php') {
|
$relativePath = str_replace(__DIR__ . '/Handlers/', '', $file->getPathname());
|
||||||
continue;
|
$className = self::HANDLER_NAMESPACE . str_replace('/', '\\', substr($relativePath, 0, -4));
|
||||||
|
|
||||||
|
|
||||||
|
if (class_exists($className)) {
|
||||||
|
$reflection = new \ReflectionClass($className);
|
||||||
|
$attributes = $reflection->getAttributes(HandlesMessage::class);
|
||||||
|
foreach ($attributes as $attribute) {
|
||||||
|
$instance = $attribute->newInstance();
|
||||||
|
$messageClass = $instance->getMessageClass();
|
||||||
|
$this->handlers[$messageClass][] = $className;
|
||||||
}
|
}
|
||||||
|
|
||||||
$relative = str_replace(__DIR__ . '/Handlers/', '', $file->getPathname());
|
|
||||||
$class = self::HANDLER_NAMESPACE . str_replace('/', '\\', substr($relative, 0, -4));
|
|
||||||
|
|
||||||
if (!class_exists($class)) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$ref = new \ReflectionClass($class);
|
|
||||||
foreach ($ref->getAttributes(HandlesMessage::class) as $attr) {
|
|
||||||
$messageClass = $attr->newInstance()->getMessageClass();
|
|
||||||
$this->handlers[$messageClass][] = $class;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Signal handler used to initiate graceful or immediate shutdown.
|
* @param $signal
|
||||||
*/
|
*/
|
||||||
public static function handleSignal(int $signal): void
|
public static function handleSignal($signal): void
|
||||||
{
|
{
|
||||||
switch ($signal) {
|
switch ($signal) {
|
||||||
|
// Graceful
|
||||||
case SIGINT:
|
case SIGINT:
|
||||||
case SIGTERM:
|
case SIGTERM:
|
||||||
case SIGHUP:
|
case SIGHUP:
|
||||||
self::$shutDown = true;
|
self::$shutDown = true;
|
||||||
return;
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Not Graceful
|
||||||
case SIGKILL:
|
case SIGKILL:
|
||||||
exit(9);
|
exit(9);
|
||||||
}
|
}
|
||||||
@@ -105,9 +96,6 @@ class Consumer
|
|||||||
return self::$shutDown;
|
return self::$shutDown;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Start the consumer main loop.
|
|
||||||
*/
|
|
||||||
public function start(): void
|
public function start(): void
|
||||||
{
|
{
|
||||||
if (!\function_exists('pcntl_signal')) {
|
if (!\function_exists('pcntl_signal')) {
|
||||||
@@ -115,11 +103,10 @@ class Consumer
|
|||||||
}
|
}
|
||||||
|
|
||||||
Logger::info('Starting queue consumer...');
|
Logger::info('Starting queue consumer...');
|
||||||
Logger::info('Using Broker: ' . Broker::getFacadeRoot()::class);
|
|
||||||
|
|
||||||
foreach ([SIGINT, SIGTERM, SIGHUP] as $sig) {
|
\pcntl_signal(SIGINT, [self::class, 'handleSignal']);
|
||||||
\pcntl_signal($sig, [self::class, 'handleSignal']);
|
\pcntl_signal(SIGTERM, [self::class, 'handleSignal']);
|
||||||
}
|
\pcntl_signal(SIGHUP, [self::class, 'handleSignal']);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if ($this->shouldShutDown()) {
|
if ($this->shouldShutDown()) {
|
||||||
@@ -131,43 +118,40 @@ class Consumer
|
|||||||
foreach ($this->queues as $queue) {
|
foreach ($this->queues as $queue) {
|
||||||
Logger::info('Listening to queue: ' . $queue->queueName());
|
Logger::info('Listening to queue: ' . $queue->queueName());
|
||||||
$message = $queue->pop();
|
$message = $queue->pop();
|
||||||
if (!$message) {
|
if ($message) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger::info('Processing message of type: ' . get_class($message));
|
Logger::info('Processing message of type: ' . get_class($message));
|
||||||
|
|
||||||
foreach ($this->getHandlersForMessage($message) as $handler) {
|
$handlers = $this->getHandlerForMessage($message);
|
||||||
|
|
||||||
|
foreach ($handlers as $handler) {
|
||||||
$handler($message);
|
$handler($message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Continue polling from the top of the loop after processing a message.
|
|
||||||
continue 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Avoid busy-looping when no messages are available.
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function getHandlerForMessage($message): array
|
||||||
* @return callable[] Handler instances invokable with the message
|
|
||||||
*/
|
|
||||||
private function getHandlersForMessage(Message $message): array
|
|
||||||
{
|
{
|
||||||
$messageClass = get_class($message);
|
|
||||||
|
|
||||||
if (!isset($this->handlers[$messageClass])) {
|
|
||||||
throw new \RuntimeException("No handler found for message class: $messageClass");
|
|
||||||
}
|
|
||||||
|
|
||||||
$callables = [];
|
$callables = [];
|
||||||
foreach ($this->handlers[$messageClass] as $handlerClass) {
|
|
||||||
|
$messageClass = get_class($message);
|
||||||
|
if (isset($this->handlers[$messageClass])) {
|
||||||
|
$handlerClasses = $this->handlers[$messageClass];
|
||||||
|
|
||||||
|
foreach ($handlerClasses as $handlerClass) {
|
||||||
if (class_exists($handlerClass)) {
|
if (class_exists($handlerClass)) {
|
||||||
$callables[] = new $handlerClass();
|
$handlerInstance = new $handlerClass();
|
||||||
|
|
||||||
|
$callables[] = $handlerInstance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $callables;
|
return $callables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new \RuntimeException("No handler found for message class: $messageClass");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ namespace Siteworxpro\App\Cli;
|
|||||||
use Ahc\Cli\Application;
|
use Ahc\Cli\Application;
|
||||||
use Siteworxpro\App\Cli\Commands\DemoCommand;
|
use Siteworxpro\App\Cli\Commands\DemoCommand;
|
||||||
use Siteworxpro\App\Cli\Commands\Queue\Start;
|
use Siteworxpro\App\Cli\Commands\Queue\Start;
|
||||||
use Siteworxpro\App\Cli\Commands\Queue\TestJob;
|
|
||||||
use Siteworxpro\App\Kernel;
|
use Siteworxpro\App\Kernel;
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
|
|
||||||
@@ -25,7 +24,6 @@ class App
|
|||||||
|
|
||||||
$this->app->add(new DemoCommand());
|
$this->app->add(new DemoCommand());
|
||||||
$this->app->add(new Start());
|
$this->app->add(new Start());
|
||||||
$this->app->add(new TestJob());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run(): int
|
public function run(): int
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ class Start extends Command implements CommandInterface
|
|||||||
$queues = explode(',', $this->values()['queues']);
|
$queues = explode(',', $this->values()['queues']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SayHelloMessage::dispatch("hello from queue consumer!");
|
||||||
|
|
||||||
$consumer = new Consumer($queues);
|
$consumer = new Consumer($queues);
|
||||||
$consumer->start();
|
$consumer->start();
|
||||||
|
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Cli\Commands\Queue;
|
|
||||||
|
|
||||||
use Ahc\Cli\Input\Command;
|
|
||||||
use Siteworxpro\App\Async\Messages\SayHelloMessage;
|
|
||||||
use Siteworxpro\App\Cli\Commands\CommandInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class TestJob
|
|
||||||
*
|
|
||||||
* A CLI command to schedule a demo job that dispatches a SayHelloMessage.
|
|
||||||
*/
|
|
||||||
class TestJob extends Command implements CommandInterface
|
|
||||||
{
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
parent::__construct('queue:demo', 'Schedule a demo job.');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the command to dispatch a SayHelloMessage.
|
|
||||||
*
|
|
||||||
* @return int Exit code
|
|
||||||
*/
|
|
||||||
public function execute(): int
|
|
||||||
{
|
|
||||||
SayHelloMessage::dispatch('World from TestJob Command!');
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,13 +8,6 @@ use League\Route\Http\Exception\NotFoundException;
|
|||||||
use Nyholm\Psr7\ServerRequest;
|
use Nyholm\Psr7\ServerRequest;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Controller
|
|
||||||
*
|
|
||||||
* An abstract base controller providing default implementations for HTTP methods.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Controllers
|
|
||||||
*/
|
|
||||||
abstract class Controller implements ControllerInterface
|
abstract class Controller implements ControllerInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,11 +7,6 @@ namespace Siteworxpro\App\Controllers;
|
|||||||
use Nyholm\Psr7\ServerRequest;
|
use Nyholm\Psr7\ServerRequest;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface ControllerInterface
|
|
||||||
*
|
|
||||||
* Defines the contract for handling HTTP requests in a controller.
|
|
||||||
*/
|
|
||||||
interface ControllerInterface
|
interface ControllerInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -12,13 +12,6 @@ use Siteworxpro\App\Models\Model;
|
|||||||
use Siteworxpro\App\Services\Facades\Redis;
|
use Siteworxpro\App\Services\Facades\Redis;
|
||||||
use Siteworxpro\HttpStatus\CodesEnum;
|
use Siteworxpro\HttpStatus\CodesEnum;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class HealthcheckController
|
|
||||||
*
|
|
||||||
* Handles health check requests to verify database and cache connectivity.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Controllers
|
|
||||||
*/
|
|
||||||
class HealthcheckController extends Controller
|
class HealthcheckController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -10,10 +10,6 @@ use Siteworxpro\App\Annotations\Events\ListensFor;
|
|||||||
use Siteworxpro\App\Events\Listeners\Listener;
|
use Siteworxpro\App\Events\Listeners\Listener;
|
||||||
use Siteworxpro\App\Services\Facades\Logger;
|
use Siteworxpro\App\Services\Facades\Logger;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Connected
|
|
||||||
* @package Siteworxpro\App\Events\Listeners\Database
|
|
||||||
*/
|
|
||||||
#[ListensFor(ConnectionEstablished::class)]
|
#[ListensFor(ConnectionEstablished::class)]
|
||||||
class Connected extends Listener
|
class Connected extends Listener
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,11 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Events\Listeners;
|
namespace Siteworxpro\App\Events\Listeners;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Listener
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Events\Listeners
|
|
||||||
*/
|
|
||||||
abstract class Listener implements ListenerInterface
|
abstract class Listener implements ListenerInterface
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,16 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Events\Listeners;
|
namespace Siteworxpro\App\Events\Listeners;
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface ListenerInterface
|
|
||||||
* @package Siteworxpro\App\Events\Listeners
|
|
||||||
*/
|
|
||||||
interface ListenerInterface
|
interface ListenerInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @param mixed $event
|
|
||||||
* @param array $payload
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function __invoke(mixed $event, array $payload = []): mixed;
|
public function __invoke(mixed $event, array $payload = []): mixed;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Helpers;
|
namespace Siteworxpro\App\Helpers;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Env
|
|
||||||
* @package Siteworxpro\App\Helpers
|
|
||||||
*/
|
|
||||||
abstract class Env
|
abstract class Env
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,17 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Siteworxpro\App\Helpers;
|
namespace Siteworxpro\App\Helpers;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Ulid
|
|
||||||
* @package Siteworxpro\App\Helpers
|
|
||||||
*/
|
|
||||||
class Ulid
|
class Ulid
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Generate a ULID string
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function generate(): string
|
public static function generate(): string
|
||||||
{
|
{
|
||||||
return \Ulid\Ulid::generate()->getRandomness();
|
return \Ulid\Ulid::generate()->getRandomness();
|
||||||
|
|||||||
@@ -27,44 +27,18 @@ use Siteworxpro\App\Http\JsonResponseFactory;
|
|||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
use Siteworxpro\HttpStatus\CodesEnum;
|
use Siteworxpro\HttpStatus\CodesEnum;
|
||||||
|
|
||||||
/**
|
|
||||||
* JWT authorization middleware.
|
|
||||||
*
|
|
||||||
* Applies JWT validation to controller actions annotated with `Jwt` attribute.
|
|
||||||
* Flow:
|
|
||||||
* - Resolve the targeted controller and method for the current route.
|
|
||||||
* - If the method has `Jwt`, read the `Authorization` header and parse the Bearer token.
|
|
||||||
* - Validate signature, time constraints, issuer\(\) and audience\(\) based on attribute and config.
|
|
||||||
* - On success, attach all token claims to the request as attributes.
|
|
||||||
* - On failure, return a 401 JSON response with validation errors.
|
|
||||||
*
|
|
||||||
* Configuration:
|
|
||||||
* - `jwt.signing_key`: key material or `file://` path to key.
|
|
||||||
* - `jwt.strict_validation`: bool toggling strict vs loose time validation.
|
|
||||||
*/
|
|
||||||
class JwtMiddleware extends Middleware
|
class JwtMiddleware extends Middleware
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Process the incoming request.
|
* @throws \JsonException
|
||||||
*
|
* @throws \Exception
|
||||||
* If the matched controller method is annotated with `Jwt`, validates the token and
|
|
||||||
* augments the request with claims on success. Otherwise, just delegates to the next handler.
|
|
||||||
*
|
|
||||||
* @param ServerRequestInterface $request PSR-7 request instance.
|
|
||||||
* @param RequestHandlerInterface|Dispatcher $handler Next middleware or route dispatcher.
|
|
||||||
*
|
|
||||||
* @return ResponseInterface Response produced by the next handler or a 401 JSON response.
|
|
||||||
*
|
|
||||||
* @throws \JsonException On JSON error response encoding issues.
|
|
||||||
* @throws \Exception On unexpected reflection or JWT parsing issues.
|
|
||||||
*/
|
*/
|
||||||
public function process(
|
public function process(
|
||||||
ServerRequestInterface $request,
|
ServerRequestInterface $request,
|
||||||
RequestHandlerInterface|Dispatcher $handler
|
RequestHandlerInterface|Dispatcher $handler
|
||||||
): ResponseInterface {
|
): ResponseInterface {
|
||||||
|
|
||||||
// Resolve the callable \[Controller, method] for the current route.
|
$callable = $this->extractRouteCallable($request, $handler);
|
||||||
$callable = $this->extractRouteCallable($handler);
|
|
||||||
if ($callable === null) {
|
if ($callable === null) {
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
@@ -77,15 +51,12 @@ class JwtMiddleware extends Middleware
|
|||||||
|
|
||||||
if ($reflectionClass->hasMethod($method)) {
|
if ($reflectionClass->hasMethod($method)) {
|
||||||
$reflectionMethod = $reflectionClass->getMethod($method);
|
$reflectionMethod = $reflectionClass->getMethod($method);
|
||||||
// Read `Jwt` attribute on the controller method.
|
|
||||||
$attributes = $reflectionMethod->getAttributes(Jwt::class);
|
$attributes = $reflectionMethod->getAttributes(Jwt::class);
|
||||||
|
|
||||||
// If no `Jwt` attribute, do not enforce auth here.
|
|
||||||
if (empty($attributes)) {
|
if (empty($attributes)) {
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract Bearer token from Authorization header.
|
|
||||||
$token = str_replace('Bearer ', '', $request->getHeaderLine('Authorization'));
|
$token = str_replace('Bearer ', '', $request->getHeaderLine('Authorization'));
|
||||||
|
|
||||||
if (empty($token)) {
|
if (empty($token)) {
|
||||||
@@ -95,7 +66,6 @@ class JwtMiddleware extends Middleware
|
|||||||
], CodesEnum::UNAUTHORIZED);
|
], CodesEnum::UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aggregate required issuers and audience from attributes.
|
|
||||||
$requiredIssuers = [];
|
$requiredIssuers = [];
|
||||||
$requiredAudience = '';
|
$requiredAudience = '';
|
||||||
|
|
||||||
@@ -111,7 +81,6 @@ class JwtMiddleware extends Middleware
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Parse and validate the token with signature, time, issuer and audience constraints.
|
|
||||||
$jwt = new JwtFacade()->parse(
|
$jwt = new JwtFacade()->parse(
|
||||||
$token,
|
$token,
|
||||||
$this->getSignedWith(),
|
$this->getSignedWith(),
|
||||||
@@ -122,7 +91,6 @@ class JwtMiddleware extends Middleware
|
|||||||
new PermittedFor($requiredAudience)
|
new PermittedFor($requiredAudience)
|
||||||
);
|
);
|
||||||
} catch (RequiredConstraintsViolated $exception) {
|
} catch (RequiredConstraintsViolated $exception) {
|
||||||
// Collect human-readable violations to return to the client.
|
|
||||||
$violations = [];
|
$violations = [];
|
||||||
foreach ($exception->violations() as $violation) {
|
foreach ($exception->violations() as $violation) {
|
||||||
$violations[] = $violation->getMessage();
|
$violations[] = $violation->getMessage();
|
||||||
@@ -134,14 +102,12 @@ class JwtMiddleware extends Middleware
|
|||||||
'errors' => $violations
|
'errors' => $violations
|
||||||
], CodesEnum::UNAUTHORIZED);
|
], CodesEnum::UNAUTHORIZED);
|
||||||
} catch (InvalidTokenStructure) {
|
} catch (InvalidTokenStructure) {
|
||||||
// Token could not be parsed due to malformed structure.
|
|
||||||
return JsonResponseFactory::createJsonResponse([
|
return JsonResponseFactory::createJsonResponse([
|
||||||
'status_code' => 401,
|
'status_code' => 401,
|
||||||
'message' => 'Unauthorized: Invalid token',
|
'message' => 'Unauthorized: Invalid token',
|
||||||
], CodesEnum::UNAUTHORIZED);
|
], CodesEnum::UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expose all token claims as request attributes for downstream consumers.
|
|
||||||
foreach ($jwt->claims()->all() as $item => $value) {
|
foreach ($jwt->claims()->all() as $item => $value) {
|
||||||
$request = $request->withAttribute($item, $value);
|
$request = $request->withAttribute($item, $value);
|
||||||
}
|
}
|
||||||
@@ -151,17 +117,6 @@ class JwtMiddleware extends Middleware
|
|||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Build the signature validation constraint from configured key.
|
|
||||||
*
|
|
||||||
* - If the configured key content includes the string `PUBLIC KEY`, use RSA SHA-256.
|
|
||||||
* - Otherwise assume an HMAC SHA-256 shared secret.
|
|
||||||
* - Supports raw key strings or `file://` paths.
|
|
||||||
*
|
|
||||||
* @return SignedWith Signature constraint used during JWT parsing.
|
|
||||||
*
|
|
||||||
* @throws \RuntimeException When no signing key is configured.
|
|
||||||
*/
|
|
||||||
private function getSignedWith(): SignedWith
|
private function getSignedWith(): SignedWith
|
||||||
{
|
{
|
||||||
$key = Config::get('jwt.signing_key');
|
$key = Config::get('jwt.signing_key');
|
||||||
@@ -170,14 +125,12 @@ class JwtMiddleware extends Middleware
|
|||||||
throw new \RuntimeException('JWT signing key is not configured.');
|
throw new \RuntimeException('JWT signing key is not configured.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load key either from file or raw text.
|
|
||||||
if (str_starts_with($key, 'file://')) {
|
if (str_starts_with($key, 'file://')) {
|
||||||
$key = InMemory::file(substr($key, 7));
|
$key = InMemory::file(substr($key, 7));
|
||||||
} else {
|
} else {
|
||||||
$key = InMemory::plainText($key);
|
$key = InMemory::plainText($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Heuristic: if PEM public key content is detected, use RSA; otherwise use HMAC.
|
|
||||||
if (str_contains($key->contents(), 'PUBLIC KEY')) {
|
if (str_contains($key->contents(), 'PUBLIC KEY')) {
|
||||||
return new SignedWith(new Sha256(), $key);
|
return new SignedWith(new Sha256(), $key);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,60 +9,30 @@ use League\Route\Route;
|
|||||||
use Psr\Http\Server\MiddlewareInterface;
|
use Psr\Http\Server\MiddlewareInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
|
|
||||||
/**
|
|
||||||
* Base middleware helper for extracting route callables.
|
|
||||||
*
|
|
||||||
* This abstract middleware provides a utility method to inspect a League\Route
|
|
||||||
* dispatcher and obtain the underlying route callable as a [class, method] tuple.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Http\Middleware
|
|
||||||
*/
|
|
||||||
abstract class Middleware implements MiddlewareInterface
|
abstract class Middleware implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Extract the route callable [class, method] from a League\Route dispatcher.
|
protected function extractRouteCallable($request, RequestHandlerInterface | Dispatcher $handler): array|null
|
||||||
*
|
{
|
||||||
* When the provided handler is a League\Route\Dispatcher, this inspects its
|
|
||||||
* middleware stack, looks at the last segment (the resolved Route), and
|
|
||||||
* attempts to normalize its callable into a [class, method] pair.
|
|
||||||
*
|
|
||||||
* Supported callable forms:
|
|
||||||
* - array callable: [object|class-string, method-string]
|
|
||||||
* - string callable: "ClassName::methodName"
|
|
||||||
*
|
|
||||||
* Returns null when the handler is not a Dispatcher, the stack is empty,
|
|
||||||
* or the callable cannot be parsed.
|
|
||||||
*
|
|
||||||
* @param RequestHandlerInterface|Dispatcher $handler The downstream handler or dispatcher.
|
|
||||||
*
|
|
||||||
* @return array{0: class-string|object|null, 1: string|null}|null Tuple of [class|object, method] or null.
|
|
||||||
*/
|
|
||||||
protected function extractRouteCallable(
|
|
||||||
RequestHandlerInterface|Dispatcher $handler
|
|
||||||
): array|null {
|
|
||||||
// Only proceed if this is a League\Route dispatcher.
|
|
||||||
if (!$handler instanceof Dispatcher) {
|
if (!$handler instanceof Dispatcher) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Route | null $lastSegment */
|
/** @var Route | null $lastSegment */
|
||||||
// Retrieve the last middleware in the stack, which should be the Route.
|
|
||||||
$lastSegment = array_last($handler->getMiddlewareStack());
|
$lastSegment = array_last($handler->getMiddlewareStack());
|
||||||
|
|
||||||
if ($lastSegment === null) {
|
if ($lastSegment === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtain the callable associated with the route.
|
|
||||||
$callable = $lastSegment->getCallable();
|
$callable = $lastSegment->getCallable();
|
||||||
$class = null;
|
$class = null;
|
||||||
$method = null;
|
$method = null;
|
||||||
|
|
||||||
// Handle array callable: [object|class-string, 'method']
|
|
||||||
if (is_array($callable) && count($callable) === 2) {
|
if (is_array($callable) && count($callable) === 2) {
|
||||||
[$class, $method] = $callable;
|
[$class, $method] = $callable;
|
||||||
} elseif (is_string($callable)) {
|
} elseif (is_string($callable)) {
|
||||||
// Handle string callable: 'ClassName::methodName'
|
// Handle the case where the callable is a string (e.g., 'ClassName::methodName')
|
||||||
[$class, $method] = explode('::', $callable);
|
[$class, $method] = explode('::', $callable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,65 +13,36 @@ use Siteworxpro\App\Controllers\Controller;
|
|||||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||||
use Siteworxpro\HttpStatus\CodesEnum;
|
use Siteworxpro\HttpStatus\CodesEnum;
|
||||||
|
|
||||||
/**
|
|
||||||
* Middleware that enforces scope-based access control on controller actions.
|
|
||||||
*
|
|
||||||
* It inspects PHP 8 attributes of type \`Scope\` applied to the resolved controller method,
|
|
||||||
* compares the required scopes with the user scopes provided on the request attribute \`scopes\`,
|
|
||||||
* and returns a 403 JSON response when any required scope is missing.
|
|
||||||
*
|
|
||||||
* If the route callable cannot be resolved, or no scope is required, the request is passed through.
|
|
||||||
*
|
|
||||||
* @see Scope
|
|
||||||
*/
|
|
||||||
class ScopeMiddleware extends Middleware
|
class ScopeMiddleware extends Middleware
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Resolve the route callable, read any \`Scope\` attributes, and enforce required scopes.
|
* @throws \JsonException
|
||||||
*
|
|
||||||
* Expected user scopes are provided on the request under the attribute name \`scopes\`
|
|
||||||
* as an array of strings.
|
|
||||||
*
|
|
||||||
* @param ServerRequestInterface $request Incoming PSR-7 request (expects \`scopes\` attribute).
|
|
||||||
* @param RequestHandlerInterface|Dispatcher $handler Next handler or League\Route dispatcher.
|
|
||||||
*
|
|
||||||
* @return ResponseInterface A 403 JSON response when scopes are insufficient; otherwise the handler response.
|
|
||||||
*
|
|
||||||
* @throws \JsonException If encoding the JSON error response fails.
|
|
||||||
* @throws \ReflectionException If reflection on the controller or method fails.
|
|
||||||
*/
|
*/
|
||||||
public function process(
|
public function process(
|
||||||
ServerRequestInterface $request,
|
ServerRequestInterface $request,
|
||||||
RequestHandlerInterface | Dispatcher $handler
|
RequestHandlerInterface | Dispatcher $handler
|
||||||
): ResponseInterface {
|
): ResponseInterface {
|
||||||
// Attempt to resolve the route's callable [Controller instance, method name].
|
$callable = $this->extractRouteCallable($request, $handler);
|
||||||
$callable = $this->extractRouteCallable($handler);
|
|
||||||
if ($callable === null) {
|
if ($callable === null) {
|
||||||
// If no callable is available, delegate to the next handler.
|
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Controller $class Controller instance resolved from the route. */
|
/** @var Controller $class */
|
||||||
[$class, $method] = $callable;
|
[$class, $method] = $callable;
|
||||||
|
|
||||||
// Ensure the controller exists and the method is defined before reflecting.
|
|
||||||
if (class_exists($class::class)) {
|
if (class_exists($class::class)) {
|
||||||
$reflectionClass = new \ReflectionClass($class);
|
$reflectionClass = new \ReflectionClass($class);
|
||||||
if ($reflectionClass->hasMethod($method)) {
|
if ($reflectionClass->hasMethod($method)) {
|
||||||
$reflectionMethod = $reflectionClass->getMethod($method);
|
$reflectionMethod = $reflectionClass->getMethod($method);
|
||||||
|
|
||||||
// Fetch all Scope attributes declared on the method.
|
|
||||||
$attributes = $reflectionMethod->getAttributes(Scope::class);
|
$attributes = $reflectionMethod->getAttributes(Scope::class);
|
||||||
|
|
||||||
foreach ($attributes as $attribute) {
|
foreach ($attributes as $attribute) {
|
||||||
/** @var Scope $scopeInstance Concrete Scope attribute instance. */
|
/** @var Scope $scopeInstance */
|
||||||
$scopeInstance = $attribute->newInstance();
|
$scopeInstance = $attribute->newInstance();
|
||||||
$requiredScopes = $scopeInstance->getScopes();
|
$requiredScopes = $scopeInstance->getScopes();
|
||||||
|
|
||||||
// Retrieve user scopes from the request (defaults to an empty array).
|
|
||||||
$userScopes = $request->getAttribute('scopes', []);
|
$userScopes = $request->getAttribute('scopes', []);
|
||||||
|
|
||||||
// Deny if any required scope is missing from the user's scopes.
|
|
||||||
if (
|
if (
|
||||||
array_any(
|
array_any(
|
||||||
$requiredScopes,
|
$requiredScopes,
|
||||||
@@ -88,7 +59,6 @@ class ScopeMiddleware extends Middleware
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// All checks passed; continue down the middleware pipeline.
|
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,21 +14,8 @@ use Siteworxpro\App\Services\ServiceProviders\DispatcherServiceProvider;
|
|||||||
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
|
use Siteworxpro\App\Services\ServiceProviders\LoggerServiceProvider;
|
||||||
use Siteworxpro\App\Services\ServiceProviders\RedisServiceProvider;
|
use Siteworxpro\App\Services\ServiceProviders\RedisServiceProvider;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Kernel
|
|
||||||
*
|
|
||||||
* The Kernel class is responsible for bootstrapping the application by
|
|
||||||
* initializing service providers and setting up the database connection.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App
|
|
||||||
*/
|
|
||||||
class Kernel
|
class Kernel
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* List of service providers to be registered during bootstrapping.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private static array $serviceProviders = [
|
private static array $serviceProviders = [
|
||||||
LoggerServiceProvider::class,
|
LoggerServiceProvider::class,
|
||||||
RedisServiceProvider::class,
|
RedisServiceProvider::class,
|
||||||
|
|||||||
@@ -6,51 +6,17 @@ namespace Siteworxpro\App\Log;
|
|||||||
|
|
||||||
use Monolog\Formatter\JsonFormatter;
|
use Monolog\Formatter\JsonFormatter;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Psr\Log\LogLevel;
|
use Psr\Log\LogLevel;
|
||||||
use RoadRunner\Logger\Logger as RRLogger;
|
use RoadRunner\Logger\Logger as RRLogger;
|
||||||
use Siteworxpro\App\Services\Facades\RoadRunnerLogger;
|
use Spiral\Goridge\RPC\RPC;
|
||||||
|
|
||||||
/**
|
|
||||||
* Logger implementation that conforms to PSR-3 (`Psr\Log\LoggerInterface`).
|
|
||||||
*
|
|
||||||
* Behavior:
|
|
||||||
* - If environment indicates RoadRunner RPC (`$_SERVER['RR_RPC']`), logs are forwarded
|
|
||||||
* to a RoadRunner RPC logger (`RoadRunner\Logger\Logger`) created via Goridge RPC.
|
|
||||||
* - Otherwise, logs are written to `php://stdout` using Monolog with a JSON formatter.
|
|
||||||
* - Messages below the configured threshold are ignored (level filtering).
|
|
||||||
*
|
|
||||||
* Supported PSR-3 levels are mapped to an internal numeric ordering in `$levels`.
|
|
||||||
* When using the RPC logger, levels are translated to the respective RPC methods
|
|
||||||
* (debug, info, warning, error). When using Monolog, the numeric mapping is used
|
|
||||||
* as the numeric level passed to Monolog's `log` method.
|
|
||||||
*/
|
|
||||||
class Logger implements LoggerInterface
|
class Logger implements LoggerInterface
|
||||||
{
|
{
|
||||||
/**
|
private ?RRLogger $rpcLogger = null;
|
||||||
* RoadRunner RPC logger instance when running under RoadRunner.
|
|
||||||
*
|
|
||||||
* @var RRLogger | LoggerInterface | null
|
|
||||||
*/
|
|
||||||
private RRLogger | LoggerInterface | null $rpcLogger = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Monolog logger used as a fallback to write JSON-formatted logs to stdout.
|
|
||||||
*
|
|
||||||
* @var \Monolog\Logger
|
|
||||||
*/
|
|
||||||
private \Monolog\Logger $monologLogger;
|
private \Monolog\Logger $monologLogger;
|
||||||
|
|
||||||
/**
|
|
||||||
* Numeric ordering for PSR-3 log levels.
|
|
||||||
*
|
|
||||||
* Lower numbers represent higher severity. This mapping is used for filtering
|
|
||||||
* messages according to the configured minimum level and for Monolog numeric level.
|
|
||||||
*
|
|
||||||
* @var array<string,int>
|
|
||||||
*/
|
|
||||||
private array $levels = [
|
private array $levels = [
|
||||||
LogLevel::EMERGENCY => 0,
|
LogLevel::EMERGENCY => 0,
|
||||||
LogLevel::ALERT => 1,
|
LogLevel::ALERT => 1,
|
||||||
@@ -62,146 +28,61 @@ class Logger implements LoggerInterface
|
|||||||
LogLevel::DEBUG => 7,
|
LogLevel::DEBUG => 7,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
public function __construct(private readonly string $level = LogLevel::DEBUG)
|
||||||
* Create a new Logger.
|
{
|
||||||
*
|
|
||||||
* @param string $level Minimum level to log (PSR-3 level string). Messages with
|
|
||||||
* a higher numeric value in `$levels` will be ignored.
|
|
||||||
*
|
|
||||||
* @param resource | null $streamOutput Optional stream handler for Monolog.
|
|
||||||
*
|
|
||||||
* The default is `LogLevel::DEBUG` (log everything).
|
|
||||||
*
|
|
||||||
* If `$_SERVER['RR_RPC']` is set, an RPC connection will be attempted at
|
|
||||||
* $_SERVER['RR_RPC'] and a RoadRunner RPC logger will be used.
|
|
||||||
*
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
|
||||||
public function __construct(
|
|
||||||
private readonly string $level = LogLevel::DEBUG,
|
|
||||||
$streamOutput = null,
|
|
||||||
) {
|
|
||||||
if (isset($_SERVER['RR_RPC'])) {
|
if (isset($_SERVER['RR_RPC'])) {
|
||||||
$this->rpcLogger = RoadRunnerLogger::getFacadeRoot();
|
$rpc = RPC::create('tcp://127.0.0.1:6001');
|
||||||
|
$this->rpcLogger = new RRLogger($rpc);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->monologLogger = new \Monolog\Logger('app_logger');
|
$this->monologLogger = new \Monolog\Logger('app_logger');
|
||||||
$formatter = new JsonFormatter();
|
$formatter = new JsonFormatter();
|
||||||
$stream = $streamOutput ?? 'php://stdout';
|
$this->monologLogger->pushHandler(new StreamHandler('php://stdout')->setFormatter($formatter));
|
||||||
$this->monologLogger->pushHandler(new StreamHandler($stream)->setFormatter($formatter));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* System is unusable.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function emergency(\Stringable|string $message, array $context = []): void
|
public function emergency(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::EMERGENCY, $message, $context);
|
$this->log(LogLevel::EMERGENCY, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Action must be taken immediately.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function alert(\Stringable|string $message, array $context = []): void
|
public function alert(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::ALERT, $message, $context);
|
$this->log(LogLevel::ALERT, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Critical conditions.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function critical(\Stringable|string $message, array $context = []): void
|
public function critical(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::CRITICAL, $message, $context);
|
$this->log(LogLevel::CRITICAL, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Runtime errors that do not require immediate action but should typically be logged and monitored.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function error(\Stringable|string $message, array $context = []): void
|
public function error(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::ERROR, $message, $context);
|
$this->log(LogLevel::ERROR, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Exceptional occurrences that are not errors.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function warning(\Stringable|string $message, array $context = []): void
|
public function warning(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::WARNING, $message, $context);
|
$this->log(LogLevel::WARNING, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Normal but significant events.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function notice(\Stringable|string $message, array $context = []): void
|
public function notice(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::NOTICE, $message, $context);
|
$this->log(LogLevel::NOTICE, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Interesting events.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function info(\Stringable|string $message, array $context = []): void
|
public function info(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::INFO, $message, $context);
|
$this->log(LogLevel::INFO, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Detailed debug information.
|
|
||||||
*
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function debug(\Stringable|string $message, array $context = []): void
|
public function debug(\Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
$this->log(LogLevel::DEBUG, $message, $context);
|
$this->log(LogLevel::DEBUG, $message, $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Logs with an arbitrary level.
|
|
||||||
*
|
|
||||||
* Behavior details:
|
|
||||||
* - If the provided `$level` maps to a numeric value greater than the configured
|
|
||||||
* minimum level, the message is discarded (filtered).
|
|
||||||
* - If an RPC logger is available, the message is forwarded to the RPC logger
|
|
||||||
* using a method chosen by level (debug, info, warning, error).
|
|
||||||
* - Otherwise, the message is written to Monolog using the numeric mapping.
|
|
||||||
*
|
|
||||||
* Notes:
|
|
||||||
* - `$level` should be a PSR-3 level string (values defined in `Psr\Log\LogLevel`).
|
|
||||||
* - If an unknown level string is passed, accessing `$this->levels[$level]` may
|
|
||||||
* trigger a PHP notice or undefined index. Ensure callers use valid PSR-3 levels.
|
|
||||||
*
|
|
||||||
* @param mixed $level PSR-3 log level (string)
|
|
||||||
* @param \Stringable|string $message
|
|
||||||
* @param array $context
|
|
||||||
*/
|
|
||||||
public function log($level, \Stringable|string $message, array $context = []): void
|
public function log($level, \Stringable|string $message, array $context = []): void
|
||||||
{
|
{
|
||||||
if (isset($this->levels[$level]) && $this->levels[$level] > $this->levels[$this->level]) {
|
if ($this->levels[$level] > $this->levels[$this->level]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,7 +105,7 @@ class Logger implements LoggerInterface
|
|||||||
$this->rpcLogger->error((string)$message, $context);
|
$this->rpcLogger->error((string)$message, $context);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$this->rpcLogger->log($level, (string)$message, $context);
|
$this->rpcLogger->log((string)$message, $context);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
|||||||
namespace Siteworxpro\App\Services;
|
namespace Siteworxpro\App\Services;
|
||||||
|
|
||||||
use Illuminate\Contracts\Container\Container;
|
use Illuminate\Contracts\Container\Container;
|
||||||
use Illuminate\Support\HigherOrderTapProxy;
|
|
||||||
use Illuminate\Support\Testing\Fakes\Fake;
|
use Illuminate\Support\Testing\Fakes\Fake;
|
||||||
use Mockery;
|
use Mockery;
|
||||||
use Mockery\Expectation;
|
use Mockery\Expectation;
|
||||||
@@ -58,9 +57,9 @@ class Facade
|
|||||||
/**
|
/**
|
||||||
* Convert the facade into a Mockery spy.
|
* Convert the facade into a Mockery spy.
|
||||||
*
|
*
|
||||||
* @return HigherOrderTapProxy | MockInterface
|
* @return MockInterface
|
||||||
*/
|
*/
|
||||||
public static function spy(): HigherOrderTapProxy | MockInterface
|
public static function spy(): MockInterface
|
||||||
{
|
{
|
||||||
if (! static::isMock()) {
|
if (! static::isMock()) {
|
||||||
$class = static::getMockableClass();
|
$class = static::getMockableClass();
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\App\Services\Facades;
|
|
||||||
|
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
|
||||||
use RoadRunner\Logger\Logger;
|
|
||||||
use Siteworxpro\App\Services\Facade;
|
|
||||||
use Spiral\Goridge\RPC\RPC;
|
|
||||||
|
|
||||||
class RoadRunnerLogger extends Facade
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
|
||||||
public static function getFacadeRoot(): mixed
|
|
||||||
{
|
|
||||||
$container = static::getFacadeContainer();
|
|
||||||
if ($container && $container->has(Logger::class) === false) {
|
|
||||||
$rpc = RPC::create($_SERVER['RR_RPC']);
|
|
||||||
$logger = new Logger($rpc);
|
|
||||||
$container->bind(static::getFacadeAccessor(), function () use ($logger) {
|
|
||||||
return $logger;
|
|
||||||
});
|
|
||||||
|
|
||||||
return $logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $container->get(Logger::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static function getFacadeAccessor(): string
|
|
||||||
{
|
|
||||||
return Logger::class;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,25 +8,8 @@ use Illuminate\Support\ServiceProvider;
|
|||||||
use Siteworxpro\App\Async\Brokers\Broker;
|
use Siteworxpro\App\Async\Brokers\Broker;
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class BrokerServiceProvider
|
|
||||||
*
|
|
||||||
* This service provider is responsible for binding the Broker implementation
|
|
||||||
* to the Laravel service container based on configuration settings.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Services\ServiceProviders
|
|
||||||
*/
|
|
||||||
class BrokerServiceProvider extends ServiceProvider
|
class BrokerServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Register services.
|
|
||||||
*
|
|
||||||
* This method binds the Broker interface to a specific implementation
|
|
||||||
* based on the configuration defined in 'queue.broker' and 'queue.broker_config'.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
* @throws \RuntimeException if the specified broker class does not exist.
|
|
||||||
*/
|
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
$this->app->singleton(Broker::class, function (): Broker {
|
$this->app->singleton(Broker::class, function (): Broker {
|
||||||
|
|||||||
@@ -7,11 +7,6 @@ namespace Siteworxpro\App\Services\ServiceProviders;
|
|||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Siteworxpro\App\Events\Dispatcher;
|
use Siteworxpro\App\Events\Dispatcher;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class DispatcherServiceProvider
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Services\ServiceProviders
|
|
||||||
*/
|
|
||||||
class DispatcherServiceProvider extends ServiceProvider
|
class DispatcherServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function register(): void
|
public function register(): void
|
||||||
|
|||||||
@@ -7,11 +7,6 @@ namespace Siteworxpro\App\Services\ServiceProviders;
|
|||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Siteworxpro\App\Log\Logger;
|
use Siteworxpro\App\Log\Logger;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class LoggerServiceProvider
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Services\ServiceProviders
|
|
||||||
*/
|
|
||||||
class LoggerServiceProvider extends ServiceProvider
|
class LoggerServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function register(): void
|
public function register(): void
|
||||||
|
|||||||
@@ -8,13 +8,6 @@ use Illuminate\Support\ServiceProvider;
|
|||||||
use Predis\Client;
|
use Predis\Client;
|
||||||
use Siteworxpro\App\Services\Facades\Config;
|
use Siteworxpro\App\Services\Facades\Config;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class RedisServiceProvider
|
|
||||||
*
|
|
||||||
* This service provider registers a Redis client as a singleton in the application container.
|
|
||||||
*
|
|
||||||
* @package Siteworxpro\App\Services\ServiceProviders
|
|
||||||
*/
|
|
||||||
class RedisServiceProvider extends ServiceProvider
|
class RedisServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function register(): void
|
public function register(): void
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Facades;
|
|
||||||
|
|
||||||
use Siteworxpro\App\Services\Facades\Dispatcher;
|
|
||||||
|
|
||||||
class DispatcherTest extends AbstractFacade
|
|
||||||
{
|
|
||||||
protected function getFacadeClass(): string
|
|
||||||
{
|
|
||||||
return Dispatcher::class;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getConcrete(): string
|
|
||||||
{
|
|
||||||
return \Siteworxpro\App\Events\Dispatcher::class;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Facades;
|
|
||||||
|
|
||||||
use Siteworxpro\App\Services\Facades\Logger;
|
|
||||||
|
|
||||||
class LoggerTest extends AbstractFacade
|
|
||||||
{
|
|
||||||
protected function getFacadeClass(): string
|
|
||||||
{
|
|
||||||
return Logger::class;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getConcrete(): string
|
|
||||||
{
|
|
||||||
return \Siteworxpro\App\Log\Logger::class;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Helpers;
|
|
||||||
|
|
||||||
use Siteworxpro\App\Helpers\Env;
|
|
||||||
use Siteworxpro\App\Helpers\Ulid;
|
|
||||||
use Siteworxpro\Tests\Unit;
|
|
||||||
|
|
||||||
class UlidTest extends Unit
|
|
||||||
{
|
|
||||||
public function testGetString(): void
|
|
||||||
{
|
|
||||||
$ulid = Ulid::generate();
|
|
||||||
$this->assertIsString($ulid);
|
|
||||||
$this->assertEquals(16, strlen($ulid));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,167 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Log;
|
|
||||||
|
|
||||||
use Mockery;
|
|
||||||
use Psr\Container\ContainerExceptionInterface;
|
|
||||||
use Psr\Container\NotFoundExceptionInterface;
|
|
||||||
use Psr\Log\LoggerInterface;
|
|
||||||
use Psr\Log\LogLevel;
|
|
||||||
use Siteworxpro\App\Log\Logger;
|
|
||||||
use Siteworxpro\Tests\Unit;
|
|
||||||
|
|
||||||
class LoggerRpcTest extends Unit
|
|
||||||
{
|
|
||||||
protected function tearDown(): void
|
|
||||||
{
|
|
||||||
parent::tearDown();
|
|
||||||
unset($_SERVER['RR_RPC']);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
* @throws \Throwable
|
|
||||||
*/
|
|
||||||
public function testLogsDebugMessageWhenLevelIsDebug(): void
|
|
||||||
{
|
|
||||||
$this->expectNotToPerformAssertions();
|
|
||||||
|
|
||||||
$_SERVER['RR_RPC'] = 'tcp://127.0.0.1:6001';
|
|
||||||
|
|
||||||
$mock = Mockery::mock(LoggerInterface::class);
|
|
||||||
$mock->expects('debug')
|
|
||||||
->with('message', ['key' => 'value'])
|
|
||||||
->once();
|
|
||||||
|
|
||||||
\Siteworxpro\App\Services\Facades\Logger::getFacadeContainer()
|
|
||||||
->bind(\RoadRunner\Logger\Logger::class, function () use ($mock) {
|
|
||||||
return $mock;
|
|
||||||
});
|
|
||||||
|
|
||||||
$inputBuffer = fopen('php://memory', 'r+');
|
|
||||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
|
||||||
$logger->debug('message', ['key' => 'value']);
|
|
||||||
|
|
||||||
$mock->shouldHaveReceived('debug');
|
|
||||||
|
|
||||||
Mockery::close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
|
||||||
public function testLogsDebugMessageWhenLevelIsInfoNotice(): void
|
|
||||||
{
|
|
||||||
$this->expectNotToPerformAssertions();
|
|
||||||
|
|
||||||
$_SERVER['RR_RPC'] = 'tcp://127.0.0.1:6001';
|
|
||||||
|
|
||||||
$mock = Mockery::mock(LoggerInterface::class);
|
|
||||||
$mock->expects('info')
|
|
||||||
->with('message', ['key' => 'value'])
|
|
||||||
->times(2);
|
|
||||||
|
|
||||||
\Siteworxpro\App\Services\Facades\Logger::getFacadeContainer()
|
|
||||||
->bind(\RoadRunner\Logger\Logger::class, function () use ($mock) {
|
|
||||||
return $mock;
|
|
||||||
});
|
|
||||||
|
|
||||||
$inputBuffer = fopen('php://memory', 'r+');
|
|
||||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
|
||||||
$logger->info('message', ['key' => 'value']);
|
|
||||||
$logger->notice('message', ['key' => 'value']);
|
|
||||||
|
|
||||||
$mock->shouldHaveReceived('info')->times(2);
|
|
||||||
Mockery::close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
|
||||||
public function testLogsDebugMessageWhenLevelIsInfoWarning(): void
|
|
||||||
{
|
|
||||||
$this->expectNotToPerformAssertions();
|
|
||||||
|
|
||||||
$_SERVER['RR_RPC'] = 'tcp://127.0.0.1:6001';
|
|
||||||
|
|
||||||
$mock = Mockery::mock(LoggerInterface::class);
|
|
||||||
$mock->expects('warning')
|
|
||||||
->with('message', ['key' => 'value'])
|
|
||||||
->times(1);
|
|
||||||
|
|
||||||
\Siteworxpro\App\Services\Facades\Logger::getFacadeContainer()
|
|
||||||
->bind(\RoadRunner\Logger\Logger::class, function () use ($mock) {
|
|
||||||
return $mock;
|
|
||||||
});
|
|
||||||
|
|
||||||
$inputBuffer = fopen('php://memory', 'r+');
|
|
||||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
|
||||||
$logger->warning('message', ['key' => 'value']);
|
|
||||||
|
|
||||||
$mock->shouldHaveReceived('warning');
|
|
||||||
Mockery::close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
|
||||||
public function testLogsDebugMessageWhenLevelIsInfoError(): void
|
|
||||||
{
|
|
||||||
$this->expectNotToPerformAssertions();
|
|
||||||
|
|
||||||
$_SERVER['RR_RPC'] = 'tcp://127.0.0.1:6001';
|
|
||||||
|
|
||||||
$mock = Mockery::mock(LoggerInterface::class);
|
|
||||||
$mock->expects('error')
|
|
||||||
->with('message', ['key' => 'value'])
|
|
||||||
->times(4);
|
|
||||||
|
|
||||||
\Siteworxpro\App\Services\Facades\Logger::getFacadeContainer()
|
|
||||||
->bind(\RoadRunner\Logger\Logger::class, function () use ($mock) {
|
|
||||||
return $mock;
|
|
||||||
});
|
|
||||||
|
|
||||||
$inputBuffer = fopen('php://memory', 'r+');
|
|
||||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
|
||||||
$logger->error('message', ['key' => 'value']);
|
|
||||||
$logger->critical('message', ['key' => 'value']);
|
|
||||||
$logger->alert('message', ['key' => 'value']);
|
|
||||||
$logger->emergency('message', ['key' => 'value']);
|
|
||||||
|
|
||||||
$mock->shouldHaveReceived('error')->times(4);
|
|
||||||
Mockery::close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ContainerExceptionInterface
|
|
||||||
* @throws NotFoundExceptionInterface
|
|
||||||
*/
|
|
||||||
public function testLogsLog(): void
|
|
||||||
{
|
|
||||||
$this->expectNotToPerformAssertions();
|
|
||||||
|
|
||||||
$_SERVER['RR_RPC'] = 'tcp://127.0.0.1:6001';
|
|
||||||
|
|
||||||
$mock = Mockery::mock(LoggerInterface::class);
|
|
||||||
$mock->expects('log')
|
|
||||||
->with('notaloglevel', 'message', ['key' => 'value']);
|
|
||||||
|
|
||||||
\Siteworxpro\App\Services\Facades\Logger::getFacadeContainer()
|
|
||||||
->bind(\RoadRunner\Logger\Logger::class, function () use ($mock) {
|
|
||||||
return $mock;
|
|
||||||
});
|
|
||||||
|
|
||||||
$inputBuffer = fopen('php://memory', 'r+');
|
|
||||||
$logger = new Logger(LogLevel::DEBUG, $inputBuffer);
|
|
||||||
$logger->log('notaloglevel', 'message', ['key' => 'value']);
|
|
||||||
|
|
||||||
$mock->shouldHaveReceived('log')->times(1);
|
|
||||||
Mockery::close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,155 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Siteworxpro\Tests\Log;
|
|
||||||
|
|
||||||
use Psr\Log\LogLevel;
|
|
||||||
use Siteworxpro\App\Log\Logger;
|
|
||||||
use Siteworxpro\Tests\Unit;
|
|
||||||
|
|
||||||
class LoggerTest extends Unit
|
|
||||||
{
|
|
||||||
private function getLoggerWithBuffer(string $logLevel): array
|
|
||||||
{
|
|
||||||
$inputBuffer = fopen('php://memory', 'r+');
|
|
||||||
return [new Logger($logLevel, $inputBuffer), $inputBuffer];
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getContents($inputBuffer): string
|
|
||||||
{
|
|
||||||
return stream_get_contents($inputBuffer, -1, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function testLogLevel(string $level): void
|
|
||||||
{
|
|
||||||
[$logger, $inputBuffer] = $this->getLoggerWithBuffer($level);
|
|
||||||
$logger->$level('message', ['key' => 'value']);
|
|
||||||
$output = $this->getContents($inputBuffer);
|
|
||||||
|
|
||||||
$this->assertNotEmpty($output);
|
|
||||||
$decoded = json_decode($output, true);
|
|
||||||
$this->assertEquals('message', $decoded['message']);
|
|
||||||
$this->assertEquals('value', $decoded['context']['key']);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function testLogLevelEmpty(string $configLevel, string $logLevel): void
|
|
||||||
{
|
|
||||||
[$logger, $inputBuffer] = $this->getLoggerWithBuffer($configLevel);
|
|
||||||
$logger->$logLevel('message', ['key' => 'value']);
|
|
||||||
$output = $this->getContents($inputBuffer);
|
|
||||||
|
|
||||||
$this->assertEmpty($output);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsDebugMessageWhenLevelIsDebug(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevel(LogLevel::DEBUG);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsInfoMessageWhenLevelIsInfo(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevel(LogLevel::INFO);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsWarningMessageWhenLevelIsWarning(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevel(LogLevel::WARNING);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsErrorMessageWhenLevelIsError(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevel(LogLevel::ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsCriticalMessageWhenLevelIsCritical(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevel(LogLevel::CRITICAL);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsAlertMessageWhenLevelIsAlert(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevel(LogLevel::ALERT);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsEmergencyMessageWhenLevelIsEmergency(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevel(LogLevel::EMERGENCY);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsNoticeMessageWhenLevelIsNotice(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevel(LogLevel::NOTICE);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testDoesNotLogWhenMinimumLevelIsInfo(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevelEmpty(LogLevel::INFO, LogLevel::DEBUG);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testDoesNotLogWhenMinimumLevelIsWarning(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::INFO);
|
|
||||||
$this->testLogLevelEmpty(LogLevel::WARNING, LogLevel::DEBUG);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testDoesNotLogWhenMinimumLevelIsError(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::DEBUG);
|
|
||||||
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::INFO);
|
|
||||||
$this->testLogLevelEmpty(LogLevel::ERROR, LogLevel::WARNING);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testDoesNotLogWhenMinimumLevelIsNotice(): void
|
|
||||||
{
|
|
||||||
$this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::DEBUG);
|
|
||||||
$this->testLogLevelEmpty(LogLevel::NOTICE, LogLevel::INFO);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsMessageWithEmptyContext(): void
|
|
||||||
{
|
|
||||||
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
|
||||||
|
|
||||||
$logger->info('Message without context');
|
|
||||||
$output = $this->getContents($buffer);
|
|
||||||
|
|
||||||
$this->assertNotEmpty($output);
|
|
||||||
$decoded = json_decode($output, true);
|
|
||||||
$this->assertEquals('Message without context', $decoded['message']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsMessageWithComplexContext(): void
|
|
||||||
{
|
|
||||||
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
|
||||||
|
|
||||||
$logger->info('Complex context', [
|
|
||||||
'user_id' => 123,
|
|
||||||
'nested' => ['key' => 'value'],
|
|
||||||
'array' => [1, 2, 3]
|
|
||||||
]);
|
|
||||||
$output = $this->getContents($buffer);
|
|
||||||
|
|
||||||
$this->assertNotEmpty($output);
|
|
||||||
$decoded = json_decode($output, true);
|
|
||||||
$this->assertEquals(123, $decoded['context']['user_id']);
|
|
||||||
$this->assertEquals('value', $decoded['context']['nested']['key']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testLogsStringableMessage(): void
|
|
||||||
{
|
|
||||||
[$logger, $buffer] = $this->getLoggerWithBuffer(LogLevel::INFO);
|
|
||||||
|
|
||||||
$stringable = new class implements \Stringable {
|
|
||||||
public function __toString(): string
|
|
||||||
{
|
|
||||||
return 'Stringable message';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
$logger->info($stringable);
|
|
||||||
$output = $this->getContents($buffer);
|
|
||||||
$this->assertNotEmpty($output);
|
|
||||||
$decoded = json_decode($output, true);
|
|
||||||
$this->assertEquals('Stringable message', $decoded['message']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user