1. Go to this page and download the library: Download ray/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/ */
ray / aop example snippets
#[Attribute(Attribute::TARGET_METHOD)]
final class NotOnWeekends
{
}
class RealBillingService
{
#[NotOnWeekends]
public function chargeOrder(PizzaOrder $order, CreditCard $creditCard)
{
class WeekendBlocker implements MethodInterceptor
{
public function invoke(MethodInvocation $invocation)
{
$today = getdate();
if ($today['weekday'][0] === 'S') {
throw new \RuntimeException(
$invocation->getMethod()->getName() . " not allowed on weekends!"
);
}
return $invocation->proceed();
}
}
use Ray\Aop\Aspect;
use Ray\Aop\Matcher;
$aspect = new Aspect();
$aspect->bind(
(new Matcher())->any(),
(new Matcher())->annotatedWith(NotOnWeekends::class),
[new WeekendBlocker()]
);
$billing = $aspect->newInstance(RealBillingService::class);
try {
echo $billing->chargeOrder(); // Interceptors applied
} catch (\RuntimeException $e) {
echo $e->getMessage() . "\n";
exit(1);
}
$aspect = new Aspect();
$aspect->bind(
(new Matcher())->any(),
(new Matcher())->annotatedWith(NotOnWeekends::class),
[new WeekendBlocker()]
);
// Weave aspects into all matching classes in the source directory
$aspect->weave('/path/to/src');
// Or weave into specific target directory
$aspect->weave('/path/to/target');
$billing = new RealBillingService();
echo $billing->chargeOrder(); // Interceptors applied
$aspect = new Aspect('/path/to/tmp/dir');
use Ray\Aop\AbstractMatcher;
use Ray\Aop\Matcher;
class IsContainsMatcher extends AbstractMatcher
{
/**
* {@inheritdoc}
*/
public function matchesClass(\ReflectionClass $class, array $arguments) : bool
{
[$contains] = $arguments;
return (strpos($class->name, $contains) !== false);
}
/**
* {@inheritdoc}
*/
public function matchesMethod(\ReflectionMethod $method, array $arguments) : bool
{
[$contains] = $arguments;
return (strpos($method->name, $contains) !== false);
}
}
class MyInterceptor implements MethodInterceptor
{
public function invoke(MethodInvocation $invocation)
{
// Before method invocation
$result = $invocation->proceed();
// After method invocation
return $result;
}
}