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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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

When a SpringBoot novice creates a project, the Controller cannot be scanned for a series of problems 1.2.3.4.5.6. Another way is to add @ComponentScan(basePackages={"xxx.xxx.xx","xxx.xxx" when starting the service class) .xx”}) is the fully qualified name of the package, which can be used for multiple SpringBoot custom controllers. The SpringBoot custom controller route cannot be scanned and cannot be found because the startup class and the custom Controller package are not in the same directory. Officially recommended placement of application.java

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

Solution to docker start failure: 1. Check the running status, and then release the occupied memory through the "echo 3 &gt; /proc/sys/vm/drop_caches" command; 2. Use "$netstat -nltp|grep .. ." command to check whether the port has been occupied. If it is found to be occupied after going online, change it to an available port and restart.

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 has not confirmed any plans to introduce a new action camera yet. Instead, it seems that GoPro will get ahead of its rival this year, having teased that it will introduce two new action cameras on September 4. For context, these are expected to a

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

Preface In some cases, the prefixes in the service controller are consistent. For example, the prefix of all URLs is /context-path/api/v1, and a unified prefix needs to be added to some URLs. The conceivable solution is to modify the context-path of the service and add api/v1 to the context-path. Modifying the global prefix can solve the above problem, but there are disadvantages. If the URL has multiple prefixes, for example, some URLs require prefixes. If it is api/v2, it cannot be distinguished. If you do not want to add api/v1 to some static resources in the service, it cannot be distinguished. The following uses custom annotations to uniformly add certain URL prefixes. one,

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

Create new text files in folders on Mac using Automator Automator is a powerful application that allows you to write scripts and automate. In this case, we will create an Automator Quick Action that can be run from anywhere in the Finder to create a new text file in the current folder location. So with a little setup, you can easily access the ability to create new text files anytime, anywhere. Open the Automator app on your Mac and select Create a new "Quick Action" Use the search function and search for "AppleScript" then double click or will run AppleScri

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

Solution to node start error: 1. Execute "node xx.js" directly in the terminal; 2. Add start startup item "scripts": {"test": "echo \"Error: no test specified\" && exit 1 ","start":"node service.js"}"; 3. Re-execute "npm start".

New DJI Osmo action camera spotted before probable summer 2024 launch to rival recent GoPro and Insta360 releases New DJI Osmo action camera spotted before probable summer 2024 launch to rival recent GoPro and Insta360 releases Jul 01, 2024 am 09:49 AM

Almost a year has passed since DJI released the Osmo Action 4 (curr. $299 on Amazon). Since then, the company has focused on its other divisions, including new RS camera gimbals. On top of that, it has introduced various drones as well like the Avata

Solution to PHP Fatal error: Class 'Controller' not found Solution to PHP Fatal error: Class 'Controller' not found Jun 22, 2023 pm 03:13 PM

When using the PHP framework, you often encounter errors such as "PHPFatalerror: Class'Controller'notfound". This kind of error is usually related to the naming, location or loading of files in the framework, especially when you try to use controllers. This article will introduce several common processing methods to solve this problem. Confirm file location First, you need to confirm that the controller file is located in the correct directory for the framework. For example, if you are using the Laravel framework

See all articles