paspect-开源

  • d1_737809
    了解作者
  • 8.9KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-06 01:45
    上传日期
php 的 AOP(面向方面​​编程)实现。
paspect1.0.1.zip
  • paspect1.0.1
  • paspect
  • config.php
    998B
  • paspectcompile.php
    82B
  • paspect.php
    41B
  • classes
  • parseAspect.class.php
    4.8KB
  • paspect.class.php
    6.3KB
  • parsePHP.class.php
    2.3KB
  • parse.class.php
    2.1KB
  • debug
  • news.aspect.php
    103B
  • bin
  • news.php
    211B
  • howto.html
    3.8KB
  • src
  • news.aspect.php
    177B
内容介绍
<?php class paspect{ public $classes = array(); public $classes_params = array(); public $aspects = array(); public function parseClass($filename){ $this->parsePHPFile($filename); $this->weave(); $this->save(); } public function paspectDIR($src){ $this->parsedir($src); $this->weave(); $this->save(); } protected function parsePHPFile($filename){ $res = parsePHP::parseFile($filename); $this->classes = $this->classes + $res[0]; $this->classes_params = $this->classes_params + $res[1]; } protected function parsedir($dirname){ if(!$dir = @scandir($dirname)) die ("Can't read directory {$dirname}. Change src directory in config.php"); $aspects = array(); $classes = array(); foreach($dir as $name){ if($name == '.' || $name == '..') continue; if(is_file($dirname . "/" . $name) && preg_match("/^(.*).class.php$/", $name)) $classes[] = $dirname ."/" . $name; else if(is_file($dirname . "/" . $name) && preg_match("/^(.*).aspect.php$/", $name)){ $aspects[] = $dirname . "/" . $name; }else if(is_dir($dirname . "/" . $name)) $this->parsedir($dirname . "/" . $name); } foreach($classes as $file) $this->parsePHPFile($file); foreach($aspects as $file) $this->aspects = $this->aspects + parseAspect::parseFile($file); } protected function weave(){ foreach($this->aspects as $aspmethodname => $aspmethod){ $classes_match = $this->matchClassesbyPredicat($aspmethod, $this->classes); $classes_match = $this->matchClasses($aspmethod, $classes_match); $this->matchClassesbyParamCount($aspmethod["param"]["param_count"], $classes_match); $this->matchMethods($classes_match, $aspmethod); $this->compile($classes_match, $aspmethod); } } protected function matchClassesbyPredicat($aspmethod, $mclasses){ switch($aspmethod["param"]["nonkindedjoin"]["predicat"]){ case "within": $value = $aspmethod["param"]["nonkindedjoin"]["value"]; return array($value => $mclasses[$value]); case "file": $retclasses = array(); $value = $aspmethod["param"]["nonkindedjoin"]["value"]; $value = str_replace("'", "", $value); $value = str_replace('"', "", $value); foreach($this->classes_params as $classname => $classparams){ if($classparams["file"] == $value){ $retclasses = $retclasses + array($classname => $mclasses[$classname]); } } return $retclasses; default: return $mclasses; } } protected function matchClassesbyParamCount($paramCount, &$mclasses){ if ($paramCount == "*") return 1; foreach($mclasses as $classname => $class){ foreach($class as $methodname => $method){ if($paramCount != count(explode(",", $method["parameters"]))){ unset($mclasses[$classname][$methodname]); } } } } //here we search all classes that match some pointcut protected function matchClasses($aspmethod, $mclasses){ $classes = array(); $pattern = $aspmethod["param"]["class"]; if($pattern == "*"){ return $mclasses; } else if (preg_match("/^\*(\w+)\*$/", $pattern, $match)){ //*aaa* foreach($mclasses as $classname => $class){ if(preg_match("/^\w*" . $match[1] . "\w*$/", $classname)){ $classes = $classes + array($classname => $class); } } } else if (preg_match("/^(\w+)\*$/", $pattern, $match)){ //aaa* foreach($mclasses as $classname => $class){ if(preg_match("/^" . $match[1] . "\w*$/", $classname)){ $classes = $classes + array($classname => $class); } } } else if (preg_match("/^\*(\w+)$/", $pattern, $match)){ //*aaa foreach($mclasses as $classname => $class){ if(preg_match("/^\w*" . $match[1] . "$/", $classname)){ $classes = $classes + array($classname => $class); } } } else if (preg_match("/^(\w+?)$/", $pattern, $match)){ //aaa foreach($mclasses as $classname => $class){ if($classname == $match[1]){ $classes = $classes + array($classname => $class); } } } return $classes; } //and here we search all methods in class that matches some pointcut protected function matchMethods(&$classes, $aspmethod){ foreach($classes as $classname => &$class){ if(!is_array($class)) continue; foreach($class as $methodname => &$method){ if(!$this->pregMethods($methodname, $aspmethod["param"]["method"])){ unset($classes[$classname][$methodname]); } } } } protected function pregMethods($methodname, $pattern){ if($pattern == "*") return 1; else if (preg_match("/^\*(\w+)\*$/", $pattern, $match)){ if(preg_match("/^\w*" . $match[1] . "\w*$/", $methodname)) return 1; } else if (preg_match("/^(\w+)\*$/", $pattern, $match)){ if(preg_match("/^" . $match[1] . "\w*$/", $methodname)) return 1; } else if (preg_match("/^\*(\w+)$/", $pattern, $match)){ if(preg_match("/^\w*" . $match[1] . "$/", $methodname)) return 1; } else if($methodname == $pattern) return 1; else return 0; } //and here some additional magic protected function compile(&$classes, $aspmethod){ foreach($classes as $classname => $class){ if(!is_array($class)) continue; foreach($class as $methodname => $method){ switch($aspmethod["advice"]){ case "before": $this->classes[$classname][$methodname]["code"] = $aspmethod["phpcode"] . $this->classes[$classname][$methodname]["code"]; break; case "after": $this->classes[$classname][$methodname]["code"] .= $aspmethod["phpcode"]; break; case "around": $this->classes[$classname][$methodname]["code"] = $aspmethod["phpcode"]; break; } } } } //saving results in files protected function save(){ foreach($this->classes as $classname => $class){ $code = $this->classes_params[$classname]["codeBefore"]; $code .= "\r\n" . $this->classes_params[$classname]["param"] . "{\r\n"; foreach($class as $methodname => $method){ $code .= $method["modifier"] . " function " . $methodname . "(" . $method["parameters"] . "){\r\n"; $code .= $method["code"] . "\r\n"; $code .= "}\r\n\r\n"; } $code .= "}" . $this->classes_params[$classname]["codeAfter"]; $file = fopen(BIN . "/" . $classname . ".php", "w"); fputs($file, "<?php " . $code); fclose($file); } } }
评论