public static function createFromToken(FunctionDefine $aFunctionDefine, Aspect $aAspect) { if (!($aClassDefine = $aFunctionDefine->belongsClass())) { throw new Exception("传入的 \$aFunctionDefine 参数无效,必须是一个类方法的定义Token"); } if (!($aDocToken = $aFunctionDefine->docToken())) { throw new Exception("传入了无效Advice %s::%s() :没有DocComment申明。", array($aClassDefine->fullName(), $aFunctionDefine->name())); } $aDocComment = $aDocToken->docComment(); $sPosition = null; if ($aDocComment->hasItem('advice')) { $sPosition = $aDocComment->item('advice'); $sPosition = trim($sPosition); $sPosition = strtolower($sPosition); } if (!$sPosition) { $sPosition = self::after; } $aAdvice = new self($aFunctionDefine->name(), $aFunctionDefine->bodySource(), $sPosition, $aFunctionDefine); $aAdvice->aDefineAspect = $aAspect; // for pointcut foreach ($aDocComment->itemIterator('for') as $sPointcutName) { if (!($aPointcut = $aAspect->pointcuts()->getByName($sPointcutName))) { throw new Exception("定义Aspect %s 的 Advice %s 时,申明了一个不存在的 Pointcut: %s 。", array($sAspectName, $aAdvice->name(), $sPointcutName)); } $aAdvice->arrForPointcuts[] = $sPointcutName; } return $aAdvice; }
private function parseAspectClass($sAspectClass) { if (!($sClassFile = ClassLoader::singleton()->searchClass($sAspectClass, Package::nocompiled))) { throw new Exception("注册到AOP中的Aspace(%s)不存在; Aspace必须是一个有效的类", $sAspectClass); } $aClassCompiler = CompilerFactory::singleton()->create(); $aTokenPool = $aClassCompiler->scan($sClassFile); $aClassCompiler->interpret($aTokenPool); if (!($aClassToken = $aTokenPool->findClass($sAspectClass))) { throw new Exception("根据 class path 搜索到class的定义文件:%s,但是该文件中没有定义:%s", array($sAspectClass, $sClassFile, $sAspectClass)); } $this->aspects()->add(Aspect::createFromToken($aClassToken, $sClassFile)); }