Skip to content

Instantly share code, notes, and snippets.

@ivastly
Last active December 11, 2019 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivastly/ac02b716ad521694ee3e21c55bf03ae7 to your computer and use it in GitHub Desktop.
Save ivastly/ac02b716ad521694ee3e21c55bf03ae7 to your computer and use it in GitHub Desktop.
hello-world aspect
<?php
namespace Ivastly\GoAopHelloWorld\Aop;
use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Annotation\Before;
class BankingAspect implements Aspect
{
/**
* Runs before every money-related (named *Money) method of Bank class.
*
* @Before("execution(public Ivastly\GoAopHelloWorld\BankingSystem\Bank->*Money(*))")
*/
public function beforeMethodExecution(MethodInvocation $invocation): void
{
echo "\n calling {$invocation->getMethod()->getName()}",
' with arguments: ',
json_encode($invocation->getArguments()),
"\n";
if ($invocation->getMethod()->getName() === 'sendMoney')
{
$arguments = $invocation->getArguments();
if ($arguments[1] === 'Developer')
{
$arguments[0] *= 1.1; // some people always get slightly more money, banks should not notice 🤑
$invocation->setArguments($arguments);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment