Skip to content
Newer
Older
100644 138 lines (123 sloc) 3.36 KB
24e9d28 @fzaninotto [1.4] Added a new behavior to illustrate filter hooks
fzaninotto authored Oct 7, 2009
1 <?php
2
343586b @fzaninotto [1.5] Changed software license to MIT (refs #881)
fzaninotto authored Mar 16, 2010
3 /**
4 * This file is part of the Propel package.
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
24e9d28 @fzaninotto [1.4] Added a new behavior to illustrate filter hooks
fzaninotto authored Oct 7, 2009
7 *
343586b @fzaninotto [1.5] Changed software license to MIT (refs #881)
fzaninotto authored Mar 16, 2010
8 * @license MIT License
24e9d28 @fzaninotto [1.4] Added a new behavior to illustrate filter hooks
fzaninotto authored Oct 7, 2009
9 */
aaff75d @willdurand Removed trailing whitespace, unwanted tabs/spaces and so on...
willdurand authored Aug 11, 2011
10
24e9d28 @fzaninotto [1.4] Added a new behavior to illustrate filter hooks
fzaninotto authored Oct 7, 2009
11 /**
12 * Changes the coding standard of Propel generated Model classes
13 * - Opening brackets always use newline, e.g.
14 * if ($foo) {
15 * ...
16 * } else {
17 * ...
18 * }
19 * Becomes:
20 * if ($foo)
21 * {
22 * ...
23 * }
24 * else
25 * {
26 * ...
27 * }
28 * - closing comments are removed, e.g.
29 * } // save()
30 * Becomes:
31 * }
32 * - tabs are replaced by 2 whitespaces
33 * - comments are stripped (optional)
34 *
35 * @author François Zaninotto
96c2065 @fzaninotto [1.5] Updated svn:eol-style and svn:keywords properties (again)
fzaninotto authored Dec 3, 2009
36 * @version $Revision$
3fba55a @fzaninotto [1.5] updated classes package according to location in the project (r…
fzaninotto authored Nov 11, 2009
37 * @package propel.generator.behavior
24e9d28 @fzaninotto [1.4] Added a new behavior to illustrate filter hooks
fzaninotto authored Oct 7, 2009
38 */
39 class AlternativeCodingStandardsBehavior extends Behavior
40 {
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
41 // default parameters value
b209784 @willdurand Fix CS
willdurand authored Mar 31, 2013
42 protected $parameters = array(
43 'brackets_newline' => 'true',
44 'remove_closing_comments' => 'true',
45 'use_whitespace' => 'true',
46 'tab_size' => 2,
47 'strip_comments' => 'false'
48 );
aaff75d @willdurand Removed trailing whitespace, unwanted tabs/spaces and so on...
willdurand authored Aug 11, 2011
49
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
50 public function objectFilter(&$script)
51 {
52 return $this->filter($script);
53 }
aaff75d @willdurand Removed trailing whitespace, unwanted tabs/spaces and so on...
willdurand authored Aug 11, 2011
54
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
55 public function extensionObjectFilter(&$script)
56 {
57 return $this->filter($script);
58 }
24e9d28 @fzaninotto [1.4] Added a new behavior to illustrate filter hooks
fzaninotto authored Oct 7, 2009
59
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
60 public function queryFilter(&$script)
61 {
62 return $this->filter($script);
63 }
01d3145 @fzaninotto [1.5] Fixed alternative_coding_standards behavior was not applied to …
fzaninotto authored Mar 15, 2010
64
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
65 public function extensionQueryFilter(&$script)
66 {
67 return $this->filter($script);
68 }
aaff75d @willdurand Removed trailing whitespace, unwanted tabs/spaces and so on...
willdurand authored Aug 11, 2011
69
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
70 public function peerFilter(&$script)
71 {
72 return $this->filter($script);
73 }
24e9d28 @fzaninotto [1.4] Added a new behavior to illustrate filter hooks
fzaninotto authored Oct 7, 2009
74
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
75 public function extensionPeerFilter(&$script)
76 {
77 return $this->filter($script);
78 }
aaff75d @willdurand Removed trailing whitespace, unwanted tabs/spaces and so on...
willdurand authored Aug 11, 2011
79
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
80 public function tableMapFilter(&$script)
81 {
82 return $this->filter($script);
83 }
24e9d28 @fzaninotto [1.4] Added a new behavior to illustrate filter hooks
fzaninotto authored Oct 7, 2009
84
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
85 /**
86 * Transform the coding standards of a PHP sourcecode string
87 *
88 * @param string $script A script string to be filtered, passed as reference
89 */
90 protected function filter(&$script)
91 {
92 $filter = array();
b209784 @willdurand Fix CS
willdurand authored Mar 31, 2013
93 if ($this->getParameter('brackets_newline') == 'true') {
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
94 $filter['#^(\t*)\}\h(else|elseif|catch)(.*)\h\{$#m'] = "$1}
1bb574b @fzaninotto [1.4][1.5] Fixed alternatice_coding_standards behavior newline on Win…
fzaninotto authored Nov 25, 2009
95 $1$2$3
96 $1{";
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
97 $filter['#^(\t*)(\w.*)\h\{$#m'] = "$1$2
aeaa21a @fzaninotto [1.4][1.5] Fixed alternative_coding_standards behavior newline on Win…
fzaninotto authored Nov 25, 2009
98 $1{";
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
99 }
100 if ($this->getParameter('remove_closing_comments') == 'true') {
101 $filter['#^(\t*)} //.*$#m'] = "$1}";
102 }
103 if ($this->getParameter('use_whitespace') == 'true') {
104 $filter['#\t#'] = str_repeat(' ', $this->getParameter('tab_size'));
105 }
aaff75d @willdurand Removed trailing whitespace, unwanted tabs/spaces and so on...
willdurand authored Aug 11, 2011
106
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
107 $script = preg_replace(array_keys($filter), array_values($filter), $script);
aaff75d @willdurand Removed trailing whitespace, unwanted tabs/spaces and so on...
willdurand authored Aug 11, 2011
108
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
109 if ($this->getParameter('strip_comments') == 'true') {
110 $script = self::stripComments($script);
111 }
112 }
aaff75d @willdurand Removed trailing whitespace, unwanted tabs/spaces and so on...
willdurand authored Aug 11, 2011
113
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
114 /**
115 * Remove inline and codeblock comments from a PHP code string
fbc1b3a @havvg fix some introspections, coding style
havvg authored Apr 9, 2013
116 *
b209784 @willdurand Fix CS
willdurand authored Mar 31, 2013
117 * @param string $code The input code
fbc1b3a @havvg fix some introspections, coding style
havvg authored Apr 9, 2013
118 *
b209784 @willdurand Fix CS
willdurand authored Mar 31, 2013
119 * @return string The input code, without comments
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
120 */
121 public static function stripComments($code)
122 {
fbc1b3a @havvg fix some introspections, coding style
havvg authored Apr 9, 2013
123 $output = '';
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
124 $commentTokens = array(T_COMMENT, T_DOC_COMMENT);
125 foreach (token_get_all($code) as $token) {
126 if (is_array($token)) {
fbc1b3a @havvg fix some introspections, coding style
havvg authored Apr 9, 2013
127 if (in_array($token[0], $commentTokens)) {
128 continue;
129 }
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
130 $token = $token[1];
fbc1b3a @havvg fix some introspections, coding style
havvg authored Apr 9, 2013
131 }
132 $output .= $token;
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
133 }
aaff75d @willdurand Removed trailing whitespace, unwanted tabs/spaces and so on...
willdurand authored Aug 11, 2011
134
afad075 @willdurand Fixed CS according to PSR-1, and PSR-2
willdurand authored May 21, 2012
135 return $output;
136 }
137 }
Something went wrong with that request. Please try again.