public function setSrcIteratorInjector(\closure $srcIteratorInjector) { $closure = new \reflectionMethod($srcIteratorInjector, '__invoke'); if ($closure->getNumberOfParameters() != 1) { throw new exceptions\logic('Src iterator injector must take one argument'); } $this->srcIteratorInjector = $srcIteratorInjector; return $this; }
public function setReflectionClassFactory(\closure $factory) { $closure = new \reflectionMethod($factory, '__invoke'); if ($closure->getNumberOfParameters() != 1) { throw new exceptions\logic\invalidArgument('Reflection class factory must take one argument'); } $this->reflectionClassFactory = $factory; return $this; }
public function setReflectionClassInjector(\closure $reflectionClassInjector) { $closure = new \reflectionMethod($reflectionClassInjector, '__invoke'); if ($closure->getNumberOfParameters() != 1) { throw new exceptions\logic\invalidArgument('Reflection class injector must take one argument'); } $this->reflectionClassInjector = $reflectionClassInjector; return $this; }
/** * Set the names of the mandatory and optional arguments to the method * */ public function setArgumentNames() { $methodClass = new reflectionMethod($this->className, $this->methodName); $parameters = $methodClass->getParameters(); foreach ($methodClass->getParameters() as $i => $param) { if ($param->isOptional()) { $this->optionalArgumentNames[] = $param->getName(); } else { $this->mandatoryArgumentNames[] = $param->getName(); } } }
private function render() { $this->prepareOutputDIrectory(); $files = $this->score->getClasses(); $methods = $this->score->getMethods() ?: array(); $branches = $this->score->getBranches() ?: array(); $paths = $this->score->getPaths() ?: array(); $classes = array_unique(array_merge(array_keys($methods), array_keys($branches), array_keys($paths))); $coverageModel = new model\coverage(); $coverageScore = new score\coverage(); foreach ($classes as $className) { $classMethods = array_unique(array_merge(array_keys(isset($methods[$className]) ? $methods[$className] : array()), array_keys(isset($branches[$className]) ? $branches[$className] : array()), array_keys(isset($paths[$className]) ? $paths[$className] : array()))); $classLines = file($files[$className]); $classModel = new model\coverage\klass($className); $classCoverage = new score\coverage(); foreach ($classLines as $number => $line) { $classModel->addLine($number + 1, $line); } foreach ($classMethods as $methodName) { $methodLines = isset($methods[$className][$methodName]) ? $methods[$className][$methodName] : array(); $methodBranches = isset($branches[$className][$methodName]) ? $branches[$className][$methodName] : array(); $methodPaths = isset($paths[$className][$methodName]) ? $paths[$className][$methodName] : array(); $reflectedMethod = new \reflectionMethod($className, $methodName); $startLineNumber = $reflectedMethod->getStartLine(); $methodModel = new model\coverage\method($methodName, $methodBranches, $methodPaths); $methodCoverage = new score\coverage(); $classModel->addLine($startLineNumber, rtrim($classLines[$startLineNumber - 1]), null, $methodName); $methodCoverage->linesAreAvailable($methodLines); $methodCoverage->pathsAreAvailable($methodPaths); foreach ($methodLines as $number => $hit) { $classModel->addLine($number, rtrim($classLines[$number - 1]), $hit); } foreach ($methodBranches as $branch) { $methodCoverage->branchIsAvailable($branch); } $methodCoverage->addToModel($methodModel); $methodModel->addToModel($classModel); $classCoverage = $classCoverage->merge($methodCoverage); } $classCoverage->addToModel($classModel); $classModel->addToModel($coverageModel); $this->classCodeCoverageIsAvailable($className, $classModel); $coverageScore = $coverageScore->merge($classCoverage); } $coverageScore->addToModel($coverageModel); $this->codeCoverageIsAvailable($coverageModel); $fs = new \Symfony\Component\Filesystem\Filesystem(); $fs->mirror(__DIR__ . '/../../resources/html/assets', $this->outputDirectory . DIRECTORY_SEPARATOR . 'assets'); $fs->mirror(__DIR__ . '/../../resources/html/fonts', $this->outputDirectory . DIRECTORY_SEPARATOR . 'fonts'); }
public function addHandler(\closure $handler, array $arguments, $priority = 0) { $invoke = new \reflectionMethod($handler, '__invoke'); if ($invoke->getNumberOfParameters() < 3) { throw new exceptions\runtime('Handler must take three arguments'); } foreach ($arguments as $argument) { if (self::isArgument($argument) === false) { throw new exceptions\runtime('Argument \'' . $argument . '\' is invalid'); } $this->handlers[$argument][] = $handler; $this->priorities[$argument] = (int) $priority; } return $this; }
/** * Returns an array with parameter objects, containing type info etc. * * @return ReflectionParameter[] Associative array with parameter objects */ public function getParameters() { $this->parameters = array(); $ar = parent::getParameters(); $i = 0; foreach ((array) $ar as $parameter) { $parameter->type = $this->params[$i++]->type; $this->parameters[$parameter->name] = $parameter; } return $this->parameters; }
/** * Load a module. * * 1. include the control file or the extension action file. * 2. create the control object. * 3. set the params passed in through url. * 4. call the method by call_user_function_array * * @access public * @return bool|object if the module object of die. */ public function loadModule() { $moduleName = $this->moduleName; $methodName = $this->methodName; /* Include the contror file of the module. */ $file2Included = $this->setActionExtFile() ? $this->extActionFile : $this->controlFile; chdir(dirname($file2Included)); include $file2Included; /* Set the class name of the control. */ $className = class_exists("my{$moduleName}") ? "my{$moduleName}" : $moduleName; if (!class_exists($className)) { $this->triggerError("the control {$className} not found", __FILE__, __LINE__, $exit = true); } /* Create a instance of the control. */ $module = new $className(); if (!method_exists($module, $methodName)) { $this->triggerError("the module {$moduleName} has no {$methodName} method", __FILE__, __LINE__, $exit = true); } $this->control = $module; /* include default value for module*/ $defaultValueFiles = glob($this->getTmpRoot() . "defaultvalue/*.php"); if ($defaultValueFiles) { foreach ($defaultValueFiles as $file) { include $file; } } /* Get the default setings of the method to be called useing the reflecting. */ $defaultParams = array(); $methodReflect = new reflectionMethod($className, $methodName); foreach ($methodReflect->getParameters() as $param) { $name = $param->getName(); $default = '_NOT_SET'; if (isset($paramDefaultValue[$className][$methodName][$name])) { $default = $paramDefaultValue[$className][$methodName][$name]; } elseif ($param->isDefaultValueAvailable()) { $default = $param->getDefaultValue(); } $defaultParams[$name] = $default; } /* Set params according PATH_INFO or GET. */ if ($this->config->requestType == 'PATH_INFO') { $this->setParamsByPathInfo($defaultParams); } elseif ($this->config->requestType == 'GET') { $this->setParamsByGET($defaultParams); } /* Call the method. */ call_user_func_array(array(&$module, $methodName), $this->params); return $module; }
<?php function __autoload($classname) { echo "__autoload({$classname})\n"; eval("class {$classname} {}"); } class B { public function doit(A $a) { } } $ref = new reflectionMethod('B', 'doit'); $parameters = $ref->getParameters(); foreach ($parameters as $parameter) { $class = $parameter->getClass(); echo $class->name . "\n"; } echo "ok\n";
/** * 加载一个模块: * 1. 引入控制器文件或扩展的方法文件; * 2. 创建control对象; * 3. 解析url,得到请求的参数; * 4. 使用call_user_function_array调用相应的方法。 * * Load a module. * 1. include the control file or the extension action file. * 2. create the control object. * 3. set the params passed in through url. * 4. call the method by call_user_function_array * * @access public * @return bool|object if the module object of die. */ public function loadModule() { $moduleName = $this->moduleName; $methodName = $this->methodName; /* * 引入该模块的control文件。 * Include the control file of the module. **/ $file2Included = $this->setActionExtFile() ? $this->extActionFile : $this->controlFile; chdir(dirname($file2Included)); include $file2Included; /* * 设置control的类名。 * Set the class name of the control. **/ $className = class_exists("my{$moduleName}") ? "my{$moduleName}" : $moduleName; if (!class_exists($className)) { $this->triggerError("the control {$className} not found", __FILE__, __LINE__, $exit = true); } /* * 创建control类的实例。 * Create a instance of the control. **/ $module = new $className(); if (!method_exists($module, $methodName)) { $this->triggerError("the module {$moduleName} has no {$methodName} method", __FILE__, __LINE__, $exit = true); } $this->control = $module; /* * 使用反射机制获取函数参数的默认值。 * Get the default settings of the method to be called using the reflecting. * * */ $defaultParams = array(); $methodReflect = new reflectionMethod($className, $methodName); foreach ($methodReflect->getParameters() as $param) { $name = $param->getName(); $default = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : '_NOT_SET'; $defaultParams[$name] = $default; } /** * 根据PATH_INFO或者GET方式设置请求的参数。 * Set params according PATH_INFO or GET. */ if ($this->config->requestType == 'PATH_INFO') { $this->setParamsByPathInfo($defaultParams); } elseif ($this->config->requestType == 'GET') { $this->setParamsByGET($defaultParams); } /* 调用该方法 Call the method. */ call_user_func_array(array(&$module, $methodName), $this->params); return $module; }
protected static function methodNameIsReservedWord(\reflectionMethod $method) { return in_array($method->getName(), self::getMethodNameReservedWordByVersion(), true); }
protected function generateMethodSignature(\reflectionMethod $method) { return ($method->isPublic() === true || $this->isLoosened($method->getName()) ? 'public' : 'protected') . ' function' . ($method->returnsReference() === false ? '' : ' &') . ' ' . $method->getName() . '(' . $this->getParametersSignature($method) . ')' . $this->getReturnType($method); }
/** * Constructor which calls the parent constructor and makes sure the comment * of the method is parsed * * @param string The class name * @param string The method name */ public function __construct($class, $method) { $this->classname = $class; parent::__construct($class, $method); $this->parseComment(); }
protected function getDeclaringClass(\reflectionMethod $method) { $declaringClass = $method->getDeclaringClass(); $traits = $this->adapter->method_exists($declaringClass, 'getTraits') === false ? array() : $declaringClass->getTraits(); if (sizeof($traits) > 0) { $methodFileName = $method->getFileName(); if ($methodFileName !== $declaringClass->getFileName() || $method->getStartLine() < $declaringClass->getStartLine() || $method->getEndLine() > $declaringClass->getEndLine()) { if (sizeof($traits) > 0) { $methodName = $method->getName(); foreach ($traits as $trait) { if ($methodFileName === $trait->getFileName() && $trait->hasMethod($methodName) === true) { return $trait; } } } } } return $declaringClass; }
protected static function getParameters(\reflectionMethod $method, $addMockController = false) { $parameters = array(); foreach ($method->getParameters() as $parameter) { $parameterCode = self::getParameterType($parameter) . ($parameter->isPassedByReference() == false ? '' : '& ') . '$' . $parameter->getName(); if ($parameter->isDefaultValueAvailable() == true) { $parameterCode .= '=' . var_export($parameter->getDefaultValue(), true); } else { if ($parameter->isOptional() === true) { $parameterCode .= '=null'; } } $parameters[] = $parameterCode; } if ($addMockController === true) { $parameters[] = '\\' . __NAMESPACE__ . '\\controller $mockController = null'; } return join(', ', $parameters); }
public function handleAjaxCall() { header("Content-Type: application/json; charset=UTF-8"); if ($_SERVER['REQUEST_METHOD'] != 'POST') { echo json_encode(array('error' => 'only POSTing is allowed')); } else { $input = file_get_contents("php://input"); $json = json_decode($input); $method = $json->method; $params = $json->params; $this->ajaxEndpoint->storage = $this; try { $reflectionMethod = new reflectionMethod($this->ajaxEndpoint, $method); $retval = $reflectionMethod->invokeArgs($this->ajaxEndpoint, $params); echo json_encode(array('result' => $retval)); } catch (Exception $e) { echo json_encode(array('error' => $e->getMessage())); } } }
protected static function methodNameIsReservedWord(\reflectionMethod $method) { switch ($method->getName()) { case '__halt_compiler': case 'abstract': case 'and': case 'array': case 'as': case 'break': case 'callable': case 'case': case 'catch': case 'class': case 'clone': case 'const': case 'continue': case 'declare': case 'default': case 'die': case 'do': case 'echo': case 'else': case 'elseif': case 'empty': case 'enddeclare': case 'endfor': case 'endforeach': case 'endif': case 'endswitch': case 'endwhile': case 'eval': case 'exit': case 'extends': case 'final': case 'for': case 'foreach': case 'function': case 'global': case 'goto': case 'if': case 'implements': case 'include': case 'include_once': case 'instanceof': case 'insteadof': case 'interface': case 'isset': case 'list': case 'namespace': case 'new': case 'or': case 'print': case 'private': case 'protected': case 'public': case 'require': case 'require_once': case 'return': case 'static': case 'switch': case 'throw': case 'trait': case 'try': case 'unset': case 'use': case 'var': case 'while': case 'xor': return true; default: return false; } }
/** * Set the model file of one module. If there's an extension file, merge it with the main model file. * * @param string $moduleName the module name * @static * @access public * @return string the model file */ public static function setModelFile($moduleName) { global $app; /* Set the main model file and extension and hook pathes and files. */ $mainModelFile = $app->getModulePath($moduleName) . 'model.php'; $modelExtPath = $app->getModuleExtPath($moduleName, 'model'); $modelHookPath = $modelExtPath . 'hook/'; $extFiles = helper::ls($modelExtPath, '.php'); $hookFiles = helper::ls($modelHookPath, '.php'); /* If no extension files and no hook files, return the main file directly. */ if (empty($extFiles) and empty($hookFiles)) { return $mainModelFile; } /* Else, judge whether needed update or not .*/ $needUpdate = false; $mergedModelFile = $app->getTmpRoot() . 'model' . $app->getPathFix() . $moduleName . '.php'; $lastTime = file_exists($mergedModelFile) ? filemtime($mergedModelFile) : 0; while (!$needUpdate) { foreach ($extFiles as $extFile) { if (filemtime($extFile) > $lastTime) { break 2; } } foreach ($hookFiles as $hookFile) { if (filemtime($hookFile) > $lastTime) { break 2; } } if (is_dir($modelExtPath) and filemtime($modelExtPath) > $lastTime) { break; } if (is_dir($modelHookPath) and filemtime($modelHookPath) > $lastTime) { break; } if (filemtime($mainModelFile) > $lastTime) { break; } return $mergedModelFile; } /* Update the cache file. */ $modelClass = $moduleName . 'Model'; $extModelClass = 'ext' . $modelClass; $extTmpModelClass = 'tmpExt' . $modelClass; $modelLines = "<?php\n"; $modelLines .= "helper::import('{$mainModelFile}');\n"; $modelLines .= "class {$extTmpModelClass} extends {$modelClass} \n{\n"; /* Cycle all the extension files. */ foreach ($extFiles as $extFile) { $extLines = self::removeTagsOfPHP($extFile); $modelLines .= $extLines . "\n"; } /* Create the merged model file and import it. */ $replaceMark = '//**//'; // This mark is for replacing code using. $modelLines .= "{$replaceMark}\n}"; if (!@file_put_contents($mergedModelFile, $modelLines)) { die("ERROR: {$mergedModelFile} not writable, please make sur the " . dirname($mergedModelFile) . ' directory exists and writable'); } if (!class_exists($extTmpModelClass)) { include $mergedModelFile; } /* Get hook codes need to merge. */ $hookCodes = array(); foreach ($hookFiles as $hookFile) { $fileName = baseName($hookFile); list($method) = explode('.', $fileName); $hookCodes[$method][] = self::removeTagsOfPHP($hookFile); } /* Cycle the hook methods and merge hook codes. */ $hookedMethods = array_keys($hookCodes); $mainModelCodes = file($mainModelFile); $mergedModelCodes = file($mergedModelFile); foreach ($hookedMethods as $method) { /* Reflection the hooked method to get it's defined position. */ $methodRelfection = new reflectionMethod($extTmpModelClass, $method); $definedFile = $methodRelfection->getFileName(); $startLine = $methodRelfection->getStartLine() . ' '; $endLine = $methodRelfection->getEndLine() . ' '; /* Merge hook codes. */ $oldCodes = $definedFile == $mergedModelFile ? $mergedModelCodes : $mainModelCodes; $oldCodes = join("", array_slice($oldCodes, $startLine - 1, $endLine - $startLine + 1)); $openBrace = strpos($oldCodes, '{'); $newCodes = substr($oldCodes, 0, $openBrace + 1) . "\n" . join("\n", $hookCodes[$method]) . substr($oldCodes, $openBrace + 1); /* Replace it. */ if ($definedFile == $mergedModelFile) { $modelLines = str_replace($oldCodes, $newCodes, $modelLines); } else { $modelLines = str_replace($replaceMark, $newCodes . "\n{$replaceMark}", $modelLines); } } /* Save it. */ $modelLines = str_replace($extTmpModelClass, $extModelClass, $modelLines); file_put_contents($mergedModelFile, $modelLines); return $mergedModelFile; }
/** * Set the model file of one module. If there's an extension file, merge it with the main model file. * * @param string $moduleName the module name * @static * @access public * @return string the model file */ public static function setModelFile($moduleName) { global $app; /* Set the main model file and extension and hook pathes and files. */ $mainModelFile = $app->getModulePath($moduleName) . 'model.php'; $modelExtPath = $app->getModuleExtPath($moduleName, 'model'); $modelHookPath = $modelExtPath . 'hook/'; $extFiles = helper::ls($modelExtPath, '.php'); $hookFiles = helper::ls($modelHookPath, '.php'); /* If no extension files and no hook files, return the main file directly. */ if (empty($extFiles) and empty($hookFiles)) { return $mainModelFile; } /* Else, judge whether needed update or not .*/ $needUpdate = false; $mergedModelFile = $app->getTmpRoot() . 'model' . $app->getPathFix() . $moduleName . '.php'; $lastTime = file_exists($mergedModelFile) ? filemtime($mergedModelFile) : 0; while (!$needUpdate) { foreach ($extFiles as $extFile) { if (filemtime($extFile) > $lastTime) { break 2; } } foreach ($hookFiles as $hookFile) { if (filemtime($hookFile) > $lastTime) { break 2; } } if (is_dir($modelExtPath) and filemtime($modelExtPath) > $lastTime) { break; } if (is_dir($modelHookPath) and filemtime($modelHookPath) > $lastTime) { break; } if (filemtime($mainModelFile) > $lastTime) { break; } return $mergedModelFile; } /* If loaded zend opcache module, turn off cache when create tmp model file to avoid the conflics. */ if (extension_loaded('Zend OPcache')) { ini_set('opcache.enable', 0); } /* Update the cache file. */ $modelClass = $moduleName . 'Model'; $extModelClass = 'ext' . $modelClass; $extTmpModelClass = 'tmpExt' . $modelClass; $modelLines = "<?php\n"; $modelLines .= "helper::import('{$mainModelFile}');\n"; $modelLines .= "class {$extTmpModelClass} extends {$modelClass} \n{\n"; /* Cycle all the extension files. */ foreach ($extFiles as $extFile) { $extLines = self::removeTagsOfPHP($extFile); $modelLines .= $extLines . "\n"; } /* Create the merged model file and import it. */ $replaceMark = '//**//'; // This mark is for replacing code using. $modelLines .= "{$replaceMark}\n}"; /* Unset conflic function for model. */ preg_match_all('/.* function\\s+(\\w+)\\s*\\(.*\\)[^\\{]*\\{/Ui', $modelLines, $functions); $functions = $functions[1]; $conflics = array_count_values($functions); foreach ($conflics as $functionName => $count) { if ($count <= 1) { unset($conflics[$functionName]); } } if ($conflics) { $modelLines = explode("\n", $modelLines); $startDel = false; foreach ($modelLines as $line => $code) { if ($startDel and preg_match('/.* function\\s+(\\w+)\\s*\\(.*\\)/Ui', $code)) { $startDel = false; } if ($startDel) { unset($modelLines[$line]); } else { foreach ($conflics as $functionName => $count) { if ($count <= 1) { continue; } if (preg_match('/.* function\\s+' . $functionName . '\\s*\\(.*\\)/Ui', $code)) { $conflics[$functionName] = $count - 1; $startDel = true; unset($modelLines[$line]); } } } } $modelLines = join("\n", $modelLines); } $tmpMergedModelFile = $app->getTmpRoot() . 'model' . $app->getPathFix() . 'tmp.' . $moduleName . '.php'; if (!@file_put_contents($tmpMergedModelFile, $modelLines)) { die("ERROR: {$tmpMergedModelFile} not writable, please make sure the " . dirname($tmpMergedModelFile) . ' directory exists and writable'); } if (!class_exists($extTmpModelClass)) { include $tmpMergedModelFile; } /* Get hook codes need to merge. */ $hookCodes = array(); foreach ($hookFiles as $hookFile) { $fileName = baseName($hookFile); list($method) = explode('.', $fileName); $hookCodes[$method][] = self::removeTagsOfPHP($hookFile); } /* Cycle the hook methods and merge hook codes. */ $hookedMethods = array_keys($hookCodes); $mainModelCodes = file($mainModelFile); $mergedModelCodes = file($tmpMergedModelFile); foreach ($hookedMethods as $method) { /* Reflection the hooked method to get it's defined position. */ $methodRelfection = new reflectionMethod($extTmpModelClass, $method); $definedFile = $methodRelfection->getFileName(); $startLine = $methodRelfection->getStartLine() . ' '; $endLine = $methodRelfection->getEndLine() . ' '; /* Merge hook codes. */ $oldCodes = $definedFile == $tmpMergedModelFile ? $mergedModelCodes : $mainModelCodes; $oldCodes = join("", array_slice($oldCodes, $startLine - 1, $endLine - $startLine + 1)); $openBrace = strpos($oldCodes, '{'); $newCodes = substr($oldCodes, 0, $openBrace + 1) . "\n" . join("\n", $hookCodes[$method]) . substr($oldCodes, $openBrace + 1); /* Replace it. */ if ($definedFile == $tmpMergedModelFile) { $modelLines = str_replace($oldCodes, $newCodes, $modelLines); } else { $modelLines = str_replace($replaceMark, $newCodes . "\n{$replaceMark}", $modelLines); } } unlink($tmpMergedModelFile); /* Save it. */ $modelLines = str_replace($extTmpModelClass, $extModelClass, $modelLines); file_put_contents($mergedModelFile, $modelLines); return $mergedModelFile; }
public function just_do_it() { # 通过反射机制设置函数的参数 $d_params = array(); $method_reflection = new reflectionMethod($this->cur_mname, $this->cur_func); foreach ($method_reflection->getParameters() as $param) { $param_name = $param->getName(); if (isset($this->cur_params[$param_name])) { $tmp = $this->cur_params[$param_name]; } else { if ($param->isDefaultValueAvailable()) { $tmp = $param->getDefaultValue(); } else { throw new Exception("参数'" . $param_name . "'不能为空. file:" . __FILE__ . " line:" . __LINE__); } } $d_params[$param_name] = $tmp; } $this->cur_params = $d_params; # 加载 call_user_func_array(array(&$this->modules[$this->cur_mname], $this->cur_func), $this->cur_params); }