Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
deprecatedHookManager.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Created by JetBrains PhpStorm.
8  * User: eric
9  * Date: 14/01/13
10  * Time: 08:48
11  * To change this template use File | Settings | File Templates.
12  */
14 {
15  public $deprecatedHooks = array(
16  "postModify" => array(
17  "newName" => "postStore",
18  "call" => '',
19  "declare" => ''
20  ) ,
21  "specRefresh" => array(
22  "newName" => "preRefresh",
23  "call" => '',
24  "declare" => ''
25  ) ,
26  "postCopy" => array(
27  "newName" => "postDuplicate",
28  "declare" => '&$copyfrom',
29  "call" => '$copyfrom'
30  ) ,
31  "preCopy" => array(
32  "newName" => "preDuplicate",
33  "call" => '$copyfrom',
34  "declare" => '&$copyfrom'
35  ) ,
36  "postRevive" => array(
37  "newName" => "postUndelete",
38  "call" => '',
39  "declare" => ''
40  ) ,
41  "preRevive" => array(
42  "newName" => "preUndelete",
43  "call" => '',
44  "declare" => ''
45  ) ,
46  "getSpecTitle" => array(
47  "newName" => "getCustomTitle",
48  "call" => '',
49  "declare" => ''
50  ) ,
51  "postInsertDoc" => array(
52  "newName" => "postInsertDocument",
53  "call" => '$docid, $multiple',
54  "declare" => '$docid, $multiple = false'
55  ) ,
56  "preInsertDoc" => array(
57  "newName" => "preInsertDocument",
58  "call" => '$docid, $multiple',
59  "declare" => '$docid, $multiple = false'
60  ) ,
61  "postUnlinkDoc" => array(
62  "newName" => "postRemoveDocument",
63  "call" => '$docid, $multiple',
64  "declare" => '$docid, $multiple = false'
65  ) ,
66  "preUnlinkDoc" => array(
67  "newName" => "preRemoveDocument",
68  "call" => '$docid, $multiple',
69  "declare" => '$docid, $multiple = false'
70  ) ,
71  "postMInsertDoc" => array(
72  "newName" => "postInsertMultipleDocuments",
73  "call" => '$tdocid',
74  "declare" => '$tdocid'
75  )
76  );
77  private $content = '';
78  private $methods = array();
79 
80  public function inspectContent($phpContent)
81  {
82  $this->methods = array();
83  $this->content = $phpContent;
84  }
85 
86  private function extractMethods()
87  {
88  if (count($this->methods) > 0) return;
89  $tokens = token_get_all($this->content);
90  $funcHunt = false;
91  foreach ($tokens as $token) {
92  if (is_array($token)) {
93  if ($token[0] === T_FUNCTION) {
94  $funcHunt = true;
95  continue;
96  }
97  if ($funcHunt && $token[0] === T_STRING) {
98  $this->methods[] = strtolower($token[1]);
99  $funcHunt = false;
100  }
101  } else {
102  if ($token === ';' || $token === '{') {
103 
104  $funcHunt = false;
105  }
106  }
107  }
108  $this->testDoubleDeclaration();
109  }
110 
111  private function testDoubleDeclaration()
112  {
113  foreach ($this->deprecatedHooks as $dName => $info) {
114  $nTestingName = strtolower($info["newName"]);
115  $dTestingName = strtolower($dName);
116  if (in_array($nTestingName, $this->methods) && in_array($dTestingName, $this->methods)) {
117  throw new \Dcp\Exception("MTHD0003", $dName, $info["newName"]);
118  }
119  }
120  }
121  protected function getDeprecatedHookList()
122  {
123  $dh = array_keys($this->deprecatedHooks);
124  foreach ($dh as $k => $v) {
125  $dh[$k] = strtolower($v);
126  }
127  return $dh;
128  }
129 
130  protected function getNewHookList()
131  {
132  $dh = array_values($this->deprecatedHooks);
133  foreach ($dh as $k => $v) {
134  $dh[$k] = strtolower($v["newName"]);
135  }
136  return $dh;
137  }
138 
139  public function getDeprecatedHooks()
140  {
141  $this->extractMethods();
142  return array_intersect($this->getDeprecatedHookList() , $this->methods);
143  }
144 
145  public function getNewHooks()
146  {
147  $this->extractMethods();
148  return array_intersect($this->getNewHookList() , $this->methods);
149  }
150 
151  protected function getNewHookName($deprecatedName)
152  {
153  foreach ($this->deprecatedHooks as $dName => $nName) {
154  if (strtolower($dName) == strtolower($deprecatedName)) return $nName["newName"];
155  }
156  return '';
157  }
158 
159  protected function getArgCallHook($deprecatedName)
160  {
161  foreach ($this->deprecatedHooks as $dName => $nName) {
162  if (strtolower($dName) == strtolower($deprecatedName)) return $nName["call"];
163  }
164  return '';
165  }
166 
167  protected function getArgDeclareHook($deprecatedName)
168  {
169  foreach ($this->deprecatedHooks as $dName => $nName) {
170  if (strtolower($dName) == strtolower($deprecatedName)) return $nName["declare"];
171  }
172  return '';
173  }
174  protected function getDeprecatedHookName($newName)
175  {
176  foreach ($this->deprecatedHooks as $dName => $nName) {
177  if (strtolower($nName["newName"]) == strtolower($newName)) return $dName;
178  }
179  return '';
180  }
181 
182  protected function getOriginalName($name)
183  {
184  foreach ($this->deprecatedHooks as $dName => $nName) {
185  if (strtolower($nName["newName"]) == strtolower($name)) return $nName["newName"];
186  if (strtolower($dName) == strtolower($name)) return $dName;
187  }
188  return $name;
189  }
190  public function generateCompatibleMethods()
191  {
192  $alias = '';
193  $dh = $this->getDeprecatedHooks();
194  foreach ($dh as $dHook) {
195  $nHook = $this->getNewHookName($dHook);
196  $alias.= "\n/**\n*generated alias : new method name\n";
197  $alias.= sprintf("*@deprecated declare %s instead\n", $nHook);
198  $alias.= sprintf("*/\n");
199  $alias.= sprintf('public function %s(%s) {deprecatedFunction("hook %s");return self::%s(%s);}', $this->getOriginalName($nHook) , $this->getArgDeclareHook($dHook) , $this->getOriginalName($dHook) , $this->getOriginalName($dHook) , $this->getArgCallHook($dHook));
200  $alias.= "\n";
201  }
202 
203  $nh = $this->getNewHooks();
204  foreach ($nh as $nHook) {
205  $dHook = $this->getDeprecatedHookName($nHook);
206  $alias.= "\n/**\n*generated alias : old compatibility\n";
207  $alias.= sprintf("*@deprecated alias for %s\n", $nHook);
208  $alias.= sprintf("*/\n");
209  $alias.= sprintf('public function %s(%s) {deprecatedFunction("hook %s");return self::%s(%s);}', $dHook, $this->getArgDeclareHook($dHook) , $this->getOriginalName($dHook) , $this->getOriginalName($nHook) , $this->getArgCallHook($dHook));
210  $alias.= "\n";
211  }
212  return $alias;
213  }
214 }
getArgDeclareHook($deprecatedName)
$info
Definition: geticon.php:30
← centre documentaire © anakeen