Home Backend Development PHP Tutorial PHP反照小试: 提取控制器的action方法

PHP反照小试: 提取控制器的action方法

Jun 13, 2016 pm 01:20 PM
action controller finder start

PHP反射小试: 提取控制器的action方法

<?php
/**
 * Acl 资源查询器
 *
 * 在指定的 控制器目录中查找 对应的:
 *  
 * 控制器 以及其 action 列表
 * 并对 控制器 已经action 注释中的 @aclres-finder-desc{ 你的注释 }aclres-finder-desc@
 * 做自动提取
 * 
 * 开发者只需在 控制器类文件中 进行对应的标述,即可... 基本就解决了 手动提取的工作了 :-)
 * 
 * @author 色色
 * @version 0.1
 * 
 */
class Pkg_Reflection_AclResource_Searcher {
	
	static function loadControllerList($basepath){
		$paths = Core_AppUtils::recursion_glob($basepath,'*.php');
		if (empty($paths)) return array();
		
		foreach ($paths as $k => $v){
			// 1. 去掉基准路径
			$v = str_replace($basepath,'',$v);
			// 2. 去掉后缀
			$v = preg_replace('/\.php$/i','',$v);
			// 3. 拆分过滤 
			$v = Core_AppUtils::normalize($v,DIRECTORY_SEPARATOR);
			if (empty($v)) continue;
			
			$paths[$k] = implode('_',$v);			
		}
		
		$d = array();
		foreach ($paths as $controller){
			$d[$controller] = self::getActionListFromControllerClass($controller);
		}
		
		return $d;
	}
	
	static function getActionListFromControllerClass($controller_name){
		
		static $controllerClassPrefix = null;
		if (!$controllerClassPrefix) {
			$controllerClassPrefix = Core_App::ini('mvc/web/dispatcher/controllerClassPrefix','Core_Controller_');
		}
		
		$clazz = "{$controllerClassPrefix}{$controller_name}";
		
		Core_Autoloader::loadClass($clazz,true);
		
		$obj = new ReflectionClass($clazz);
		
		$d = array();
		$publicMethods = $obj->getMethods(ReflectionMethod::IS_PUBLIC);
		
		foreach ($publicMethods as $method){
			
			if (preg_match('/^action/i',$method->name)) {
				$action_name = preg_replace('/^action/i','',$method->name);
				$rmd = Core_Mvc_Router::resourceEncode($controller_name,$action_name);
				$q = array_shift($rmd);
				$d[$q] = self::getAclResourceDescription($method->getDocComment());
			}
		}
		
		return array(
			'description' => self::getAclResourceDescription($obj->getDocComment()),
			'actions' => $d
		);
	}
	
	static function getAclResourceDescription($finder){
		static $tagfinder_start = '@aclres-finder-desc{';
		static $tagfinder_end = '}aclres-finder-desc@';
		
		if (empty($finder)) return '';
		
		$start = stripos($finder,$tagfinder_start);
		
		if ($start){
			$end = stripos($finder,$tagfinder_end);
			
			if ($end && $end > $start){
				// 只有闭合的标签才行
				$start = $start+strlen($tagfinder_start);
				return trim(substr($finder,$start,$end-$start));
			}
			
		}
		return '';
	}
	
}
Copy after login
?

1 楼 vb2005xu 2012-05-31  
http://www.php10086.com/page/3 不错的博客

2 楼 vb2005xu 2012-05-31  
http://opauth.org/

3 楼 vb2005xu 2012-06-01  
http://www.shejidaren.com/category/css
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that SpringBoot cannot scan the Controller How to solve the problem that SpringBoot cannot scan the Controller May 14, 2023 am 08:10 AM

How to solve the problem that SpringBoot cannot scan the Controller

What should I do if docker start cannot start? What should I do if docker start cannot start? Oct 21, 2022 pm 03:43 PM

What should I do if docker start cannot start?

DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 Black DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 Black Sep 04, 2024 am 06:51 AM

DJI Osmo Action 5 Pro: Release date mooted as retailer reveals launch pricing that could undercut GoPro Hero 13 Black

How to add URL prefix to SpringBoot multiple controllers How to add URL prefix to SpringBoot multiple controllers May 12, 2023 pm 06:37 PM

How to add URL prefix to SpringBoot multiple controllers

How to create text files in folders on Mac How to create text files in folders on Mac Apr 15, 2023 pm 01:52 PM

How to create text files in folders on Mac

What to do if node start reports an error What to do if node start reports an error Dec 29, 2022 pm 01:55 PM

What to do if node start reports an error

Insta360 Go 3S: New pocketable 4K action camera released weighing just 39 g with Apple Find My support Insta360 Go 3S: New pocketable 4K action camera released weighing just 39 g with Apple Find My support Jun 14, 2024 pm 06:05 PM

Insta360 Go 3S: New pocketable 4K action camera released weighing just 39 g with Apple Find My support

Share 2 simple ways to downgrade iPadOS 16! Share 2 simple ways to downgrade iPadOS 16! Jan 07, 2024 pm 12:02 PM

Share 2 simple ways to downgrade iPadOS 16!

See all articles