1. Go to this page and download the library: Download sarkis-sh/spray-aop 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/ */
namespace App\Aspects;
use Spray\Aop\Aspects\BaseAspect;
class LoggingAspect extends BaseAspect
{
protected function before(array $invocation): void
{
logger()->info('Method called', [
'class' => get_class($invocation['instance']),
'method' => $invocation['method'],
'args' => $invocation['args'],
]);
}
}
namespace App\Attributes;
use Spray\Aop\Attributes\AspectAttribute;
#[\Attribute(\Attribute::TARGET_METHOD)]
class Logging extends AspectAttribute
{
public function __construct()
{
parent::__construct(\App\Aspects\LoggingAspect::class, []);
}
}
namespace App\Aspects;
use Spray\Aop\Aspects\BaseAspect;
class CachingAspect extends BaseAspect
{
protected function after(array $invocation, mixed $result): mixed
{
// Cache the result
cache()->put($invocation['method'], $result, 3600);
return $result;
}
}
namespace App\Aspects;
use Spray\Aop\Aspects\BaseAspect;
use Throwable;
class ErrorTrackingAspect extends BaseAspect
{
protected function onException(array $invocation, Throwable $e): mixed
{
// Log the error
logger()->error('Exception in method', [
'method' => $invocation['method'],
'exception' => $e->getMessage(),
]);
throw $e; // Re-throw the exception
}
}