Exemple #1
0
 /**
  * Runs a screen.
  * 
  * @param string $which Which screen to run, "before" or "after"
  * @param Controller $controller The controller
  * @param AttributeReader $method_meta The method metadata
  * @param Array $data Array of data that the screen(s) can add to.
  */
 public static function Run($which, $controller, $method_meta, &$data, &$args)
 {
     $c = $controller->metadata->{$which};
     $m = $method_meta->{$which};
     if (!$c) {
         $c = new AttributeReader();
     }
     if (!$m) {
         $m = new AttributeReader();
     }
     $screens = $c->merge($m);
     foreach ($screens as $name => $screen) {
         if (!$screen->ignore) {
             if (isset(self::$_screens[$name])) {
                 $s = self::$_screens[$name];
             } else {
                 uses('screen.' . $name);
                 $class = $name . 'Screen';
                 $s = new $class();
                 self::$_screens[$name] = $s;
             }
             $s->{$which}($controller, $screen, $data, $args);
         }
     }
 }
Exemple #2
0
 public function __construct($request)
 {
     $this->session = Session::Get();
     $this->request = $request;
     $this->metadata = AttributeReader::ClassAttributes($this);
     $this->setup();
 }
Exemple #3
0
 /**
  * @throws BadRequestException
  *
  */
 public function __construct()
 {
     $meta = AttributeReader::ClassAttributes($this);
     $apiconf = Config::Get('api');
     $this->conf = $apiconf->{$meta->api};
     if (!$this->conf) {
         throw new BadRequestException('Missing api info');
     }
 }
Exemple #4
0
	/**
	 * 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;
	}
 /**
  * Fetches the metadata for a property of a class
  *
  * @param mixed $class An instance of the class or it's name as a string
  * @param string $prop The name of the property 
  * @return AttributeReader An attribute reader instance
  */
 public static function PropertyAttributes($class, $prop_name)
 {
     if (!$class instanceof ReflectionClass) {
         $class = new ReflectionClass($class);
     }
     $prop = new ReflectionProperty($class->getName(), $prop_name);
     $cache = Cache::GetCache('attributes');
     $key = 'cp-' . $class->getFileName() . '-' . $prop_name;
     $reader = $cache->get($key);
     if (!$reader) {
         $yaml = AttributeReader::ParseDocComments($prop->getDocComment());
         if (trim($yaml) == '') {
             $reader = new AttributeReader();
         } else {
             $reader = new AttributeReader(yaml_parse($yaml));
         }
         $parent = $class->getParentClass();
         if ($parent) {
             $pmeta = AttributeReader::PropertyAttributes($parent, $prop_name);
             $reader = $pmeta->merge($reader);
         }
         $cache->set($key, $reader);
     }
     return $reader;
 }
Exemple #6
0
 /**
  * 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');
 }
Exemple #7
0
 /**
  * Sets up serialization
  * 
  * @param mixed $object The object to serialize
  */
 protected function setup_serialization($object)
 {
     if (!$this->node_conf) {
         $attrs = AttributeReader::ClassAttributes($object);
         if (!$attrs || !$attrs->serializer) {
             throw new Exception('Class is missing necessary metadata for serialization.');
         }
         $this->node_config = self::GetNodeConf($attrs->serializer);
         $this->root = $attrs->serializer->node;
     }
     $this->object = $object;
     $this->content = $this->do_serialize();
 }