public static function start() { $em = MyEntityManagerFactory::getEntityManager(); /* @var $jp \My\Illusion\AopJoinpoint */ $before = function ($jp) use($em) { TransactionManager::$_stateData[++TransactionManager::$_stateIndex] = 1; $em->flush(); $em->beginTransaction(); }; $after = function ($jp) use($em) { if ($jp->getException() !== null) { TransactionManager::$_stateData[TransactionManager::$_stateIndex] = 2; $em->rollback(); } else { if (TransactionManager::$_stateData[TransactionManager::$_stateIndex] === 1) { $em->flush(); $em->commit(); } else { if (TransactionManager::$_stateData[TransactionManager::$_stateIndex] == 0) { $em->rollback(); } } TransactionManager::$_stateIndex--; } }; $namespace = '*Controller->*Action()'; aop_add_before($namespace, $before); aop_add_after($namespace, $after); $namespace = 'public Models\\Services\\*Service->*()'; aop_add_before($namespace, $before); aop_add_after($namespace, $after); $namespace = 'public Models\\Repositories\\*Repository->sav*()'; aop_add_before($namespace, $before); aop_add_after($namespace, $after); }
public function start() { $dataRepo = $this->dataRepository; foreach ($this->functionSignatures as $signature) { aop_add_before($signature, function (\AopJoinPoint $ajp) use($dataRepo) { $dataRepo->saveParams(1, $ajp->getPointcut(), $ajp->getArguments(), DataFlowDirection::CALLING); }); aop_add_after($signature, function (\AopJoinPoint $ajp) use($dataRepo) { $dataRepo->saveParams(1, $ajp->getPointcut(), $ajp->getReturnedValue(), DataFlowDirection::RETURNING); }); } }
public function registerAspects(MvcEvent $event) { $application = $event->getApplication(); $config = $application->getConfig(); Annotations\AnnotationRegistry::registerAutoloadNamespace(__NAMESPACE__ . '\\Annotation'); foreach ($config['aop']['aspect_class_paths'] as $path) { foreach (new ClassFileLocator($path) as $classInfoFile) { foreach ($classInfoFile->getClasses() as $class) { $aspect = new $class(); $reader = new Annotations\AnnotationReader(); $reflection = new \ReflectionClass($aspect); foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { $annotation = $reader->getMethodAnnotation($method, 'AOP\\Annotation\\Pointcut'); if (!$annotation) { continue; } $advice = $method->getName(); $pointcuts = $annotation->rule; foreach ($pointcuts as $pointcut) { list($trigger, $rule) = sscanf($pointcut, "%s %s"); switch ($trigger) { case 'before': aop_add_before($rule, array($aspect, $advice)); break; case 'after': aop_add_after($rule, array($aspect, $advice)); break; case 'around': aop_add_around($rule, array($aspect, $advice)); break; } } if ($aspect instanceof \Zend\ServiceManager\ServiceLocatorAwareInterface) { $aspect->setServiceLocator($application->getServiceManager()); } } } } } }
/** * @inheritdoc * @see \Aop\Weaver\WeaverInterface::addAfter() */ public function addAfter(Pointcutinterface $pointcut, AdviceInterface $advice, array $options = []) { aop_add_after($pointcut->getSelector(), $this->createBinder($pointcut, $advice, $options)); return $this->lastIndex; }
<?php aop_add_after('redis->get()', function ($jp) { var_dump('aop_add_after', $jp->getArguments()); }); aop_add_after('redis2->get()', function ($jp) { var_dump('aop_add_after', $jp->getArguments()); }); class redis2 { function connect($host) { } function set($key, $val) { } function get($key) { } } class myredis { function __construct($redis) { $this->_redis = $redis; } public function __call($name, $args) { return call_user_func_array(array($this->_redis, $name), $args); } }
function hook() { aop_add_after('foo->bar()', array($this, 'dump')); }
function hook() { if ($this->hooks) { foreach ($this->hooks as $func) { $function = $this->getHookFunc($func); aop_add_before($function, array($this, '_preCallback')); aop_add_after($function, array($this, '_postCallback')); } } }
/** * Enable the time traveler. */ public static function enable() { if (self::$enabled === true) { return; } if (!function_exists('\\aop_add_after')) { throw new \LogicException('Aop extension seems to not be installed.'); } $createDateTimeWithStr = function (\DateTime $date, $str = null) { if (TimeTraveler::getCurrentTime()) { $date->setTimestamp(TimeTraveler::getCurrentTime()); if ($str) { $date->modify($str); } } return $date; }; aop_add_after('time()', function (\AopJoinPoint $joinPoint) { if (TimeTraveler::getCurrentTimeOffset()) { $joinPoint->setReturnedValue($joinPoint->getReturnedValue() + TimeTraveler::getCurrentTimeOffset()); } }); aop_add_after('microtime()', function (\AopJoinPoint $joinPoint) { if (TimeTraveler::getCurrentTimeOffset()) { $returnedValue = $joinPoint->getReturnedValue(); if (is_float($returnedValue)) { $joinPoint->setReturnedValue($joinPoint->getReturnedValue() + TimeTraveler::getCurrentTimeOffset()); } else { list($micro, $seconds) = explode(' ', $joinPoint->getReturnedValue()); $seconds += TimeTraveler::getCurrentTimeOffset(); $joinPoint->setReturnedValue($micro . ' ' . $seconds); } } }); aop_add_after('DateTime->__construct()', function (\AopJoinPoint $joinPoint) use($createDateTimeWithStr) { $args = $joinPoint->getArguments(); $createDateTimeWithStr($joinPoint->getObject(), isset($args[0]) ? $args[0] : null); }); $functionDates = function (\AopJoinPoint $joinPoint) { if (TimeTraveler::getCurrentTimeOffset()) { $args = $joinPoint->getArguments(); if (isset($args[1]) && !empty($args[1])) { return; } $function = $joinPoint->getFunctionName(); $joinPoint->setReturnedValue($function($args[0], time() + TimeTraveler::getCurrentTimeOffset())); } }; aop_add_after('date()', $functionDates); aop_add_after('gmdate()', $functionDates); aop_add_after('date_create()', function (\AopJoinPoint $joinPoint) use($createDateTimeWithStr) { $createDateTimeWithStr($joinPoint->getReturnedValue(), isset($args[0]) ? $args[0] : null); }); aop_add_after('strtotime()', function (\AopJoinPoint $joinPoint) use($createDateTimeWithStr) { $arguments = $joinPoint->getArguments(); if (isset($arguments[1]) && !empty($arguments[1])) { // time is given, we haven't anything to do. return; } if (TimeTraveler::getCurrentTime()) { $date = $createDateTimeWithStr(new \DateTime(), $arguments[0]); $joinPoint->setReturnedValue($date->getTimestamp()); } }); aop_add_after('gettimeofday()', function (\AopJoinPoint $joinPoint) { if (TimeTraveler::getCurrentTime()) { $args = $joinPoint->getArguments(); if (array_key_exists(0, $args) && false !== $args[0]) { $joinPoint->setReturnedValue($joinPoint->getReturnedValue() + TimeTraveler::getCurrentTimeOffset()); } else { $returnedValue = $joinPoint->getReturnedValue(); $returnedValue['sec'] += TimeTraveler::getCurrentTimeOffset(); $joinPoint->setReturnedValue($returnedValue); } } }); self::$enabled = true; }