Development

/plugins/sfAmfPlugin/lib/sfAmfService.php

You must first sign up to be able to contribute.

root/plugins/sfAmfPlugin/lib/sfAmfService.php

Revision 30449, 4.7 kB (checked in by stefbach, 3 years ago)

r48@new-host: stef | 2010-07-28 13:01:44 +0200
Removed Service dependency

Line 
1 <?php
2 /**
3  * This file is part of the sfAmfPlugin package.
4  * (c) 2008-2009 Timo Haberkern <timo.haberkern@gmail.com>
5  *
6  * For the full copyright and license information, please view the LICENSE
7  * file that was distributed with this source code.
8  */
9
10 abstract class sfAmfService {
11   public function __construct() {
12     $class_name = get_class($this);
13     $reflector = new ReflectionClass($class_name);
14
15     foreach ($reflector->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
16       if (!$method->isConstructor() &&
17         !$method->isStatic() &&
18         !$method->isAbstract() ) {
19
20         // getting the annotation information for this method
21         $method_reflector = new ReflectionAnnotatedMethod($class_name, $method->name);
22
23         if ($method_reflector->hasAnnotation('AmfClassMapping')) {
24           $as_class_name = $method_reflector->getAnnotation('AmfClassMapping')->name;
25           $this->method_info[$method->name]['class_mapping'] = $as_class_name;
26         }
27
28         if ($method_reflector->hasAnnotation('AmfIgnore')) {
29           $this->method_info[$method->name]['ignore'] = 1;
30         }
31
32         if ($method_reflector->hasAnnotation('AmfReturnType')) {
33           $type = $method_reflector->getAnnotation('AmfReturnType')->value;
34
35           // At the moment we can use ByteArray and ArrayCollection as
36           // types
37           if ($type == 'ArrayCollection' || $type == 'ByteArray') {
38             $this->method_info[$method->name]['return_type'] = $type;
39           }
40           else {
41             throw new Exception('AmfReturnType can only be one of the following values: '.
42                   'ByteArray, ArrayCollection');
43           }
44         }
45       }
46     }
47   }
48
49   public function run($method_name, $arguments) {
50
51     if (array_key_exists($method_name, $this->method_info)) {
52       if (array_key_exists('ignore', $this->method_info[$method_name])) {
53         if ($this->method_info[$method_name]['ignore'] == 1) {
54           throw new Exception('There is no callable method with the name '.
55               $method_name.
56               '. Access restricted by @AmfIgnore.');
57         }
58       }
59     }
60     
61     $result = call_user_func_array(
62       array($this, $method_name),
63       $arguments
64     );
65     $adapter_proxy = new sfAdapterDelegate();
66     $data = $adapter_proxy->convert($result);
67
68     if (array_key_exists($method_name, $this->method_info)) {
69       if (array_key_exists('class_mapping', $this->method_info[$method_name])) {
70         if ($this->method_info[$method_name]['class_mapping'] == '') {
71           throw new Exception('Class mapping can not be done. Empty class name. '.
72                 'Please use a format like @AmfClassMapping(name="de.rtx.mipmap")');
73         }
74         else {
75           return new SabreAMF_TypedObject(
76           $this->method_info[$method_name]['class_mapping'],
77           $data);
78         }
79       }
80       else if (array_key_exists('return_type', $this->method_info[$method_name])) {
81         switch ($this->method_info[$method_name]['return_type']) {
82           case 'ArrayCollection':
83             return new SabreAMF_ArrayCollection($data);
84             break;
85           case 'ByteArray':
86             return new SabreAMF_ByteArray($data);
87             break;
88         }
89       }
90     }
91
92     return $data;
93   }
94   /**
95    * create a record in database from ValueObject
96    *
97    * @param ValueObject Remote object
98    * @return boolean True if creation succeeds
99    */   
100   abstract public function create($valueObject);
101  
102   /**
103    * create a record in database from ValueObject
104    *
105    * @param int Limit of ValueObject to return, 0 being no limit (default: 0)
106    * @return Array of ValueObject
107    */   
108   abstract public function retrieveAll($limit = 0);
109
110   /**
111    * get a record from database
112    *
113    * @param int Object id
114    * @return ValueObject Object from atabase record
115    */   
116   abstract public function retrieveById($id);
117
118   /**
119    * update a record in database from ValueObject
120    *
121    * @param ValueObject Remote object
122    * @return boolean True if update succeeds
123    */   
124   abstract public function update($valueObject);
125
126   /**
127    * delete a record in database from ValueObject
128    *
129    * @param ValueObject Remote object
130    * @return boolean True if delete succeeds
131    */   
132   abstract public function delete($valueObject);
133
134   /**
135    * get initialized BaseObject from ValueObject
136    *
137    * @param ValueObject Remote object
138    * @return BaseObject The model object
139    */   
140   abstract protected function fromValueObject($valueObject);
141
142   /**
143    * get ValueObject from (Propel) BaseObject
144    *
145    * @param BaseObject The model object
146    * @return ValueObject The object to be used remotely
147    */   
148   abstract protected function toValueObject($baseObject);
149
150   private $method_info = array();
151 }
152 ?>
Note: See TracBrowser for help on using the browser.