コード例 #1
0
 /**
  * Fetches the metadata for a method of a class
  *
  * @param mixed $class An instance of the class or it's name as a string
  * @param string $method The name of the method 
  * @return AttributeReader An attribute reader instance
  */
 public static function MethodAttributes($class, $method_name)
 {
     if (!$class instanceof ReflectionClass) {
         $class = new ReflectionClass($class);
     }
     $method = new ReflectionMethod($class->getName(), $method_name);
     $cache = Cache::GetCache('attributes');
     $key = 'ca-' . $class->getFileName() . '-' . $method_name;
     $reader = $cache->get($key);
     if (!$reader) {
         $yaml = AttributeReader::ParseDocComments($method->getDocComment());
         if (trim($yaml) == '') {
             $reader = new AttributeReader();
         } else {
             $reader = new AttributeReader(yaml_parse($yaml));
         }
         $parent = $class->getParentClass();
         if ($parent) {
             try {
                 $pmeta = AttributeReader::MethodAttributes($parent, $method_name);
                 $reader = $pmeta->merge($reader);
             } catch (ReflectionException $ex) {
             }
         }
         $cache->set($key, $reader);
     }
     return $reader;
 }
コード例 #2
0
ファイル: dispatcher.php プロジェクト: nikels/HeavyMetal
	/**
	 * Executes a controller, returning the data
	 *
	 * @return array The data from the executed controller.
	 */
	public function call()
	{
		$data = array(); // any data to return to the view from the controller
		
		$cfound=$this->find();
		$request_method=$cfound['request_method'];
		$classname=$cfound['classname'];
		$found_action=$cfound['found_action'];
		$root=$cfound['root'];
		
		$this->action=$found_action;
		
		if (!file_exists($this->controller_root.$this->controller_path.$this->controller.EXT))
			throw new ControllerNotFoundException("Could not find a suitable controller: ".$this->controller_root.$this->controller_path.$this->controller.EXT);
			
		require_once($this->controller_root.$this->controller_path.$this->controller.EXT);
		$classname=str_replace('/','',$this->controller_path).$this->controller.'Controller';
		
		if (!class_exists($classname))
			throw new ControllerNotFoundException("'$classname' can not be found in '".$this->controller."'.");

		$request_method = Request::get_request_method();
		$found_action=find_methods($classname, $request_method."_".str_replace('-','_',$this->action), str_replace('-','_',$this->action));

		if (!$found_action)
		{
			$found_action=find_methods($classname, $request_method."_index", 'index');
   			array_unshift($this->segments,$this->action);  // so here we put that mistakenly stripped parameter back on.
		}
		
		if (!$found_action)
		{
			throw new ControllerMethodNotFoundException("Could not find an action to call.");
		}
		
		$this->action=$found_action;
		
		// Handle the fact that some URIs contain extra segments that are not part of the controller/action root
		$root = $this->controller_path . 
			(($this->controller!='index') ? $this->controller . "/" : "") .
			(($this->action!='index' && $found_action!='index') ? $this->action : "");

		$root = rtrim($root,'/');		
			
		$request=$this->build_request($root);
		$class=new $classname($request);
		
		if ((isset ($class->ignored)) && (in_array($this->action, $class->ignored)))
			throw new IgnoredMethodCalledException("Ignored method called.");
		
		$meta=AttributeReader::MethodAttributes($class,$found_action);
			
		// Call the before screens	
		$screen_data=array();
		$method_args=$this->segments;
		Screen::Run('before',$class,$meta,$screen_data,$method_args);
		// call the method and pass the segments (add returned data to any initially returned by screens)
		$data = call_user_func_array(array(&$class, $found_action), $method_args);
		if (is_array($data))
			$data=array_merge($screen_data,$data);
		else
			$data=$screen_data;
		// Call the after screens
		Screen::Run('after',$class,$meta,$data,$method_args);
				
		$class->session->save();
		
		if ($class->view)
			$this->view=$class->view;
		else if ($meta->view)
			$this->view=$meta->view;
			
		$data['controller']=&$class;
		$data['session']=&$class->session;
		return $data;
	}
コード例 #3
0
ファイル: attributetest.php プロジェクト: jawngee/HeavyMetal
 /**
  * Test the attribute reading meta attributes inheritance.
  */
 public function testInheritance()
 {
     $class = new AttributeChildClass();
     $classmeta = AttributeReader::ClassAttributes($class);
     $this->assertTrue($classmeta->grand == '123');
     $this->assertTrue($classmeta->parent == '456');
     $this->assertTrue($classmeta->child == '789');
     $methodmeta = AttributeReader::MethodAttributes($class, 'method');
     $this->assertTrue($methodmeta->grand == 'ABC');
     $this->assertTrue($methodmeta->parent == 'DEF');
     $this->assertTrue($methodmeta->child == 'GHI');
     $propmeta = AttributeReader::PropertyAttributes($class, 'property');
     $this->assertTrue($propmeta->grand == 'abc');
     $this->assertTrue($propmeta->parent == 'def');
     $this->assertTrue($propmeta->child == 'ghi');
 }