1 | <?php |
||
15 | class DoctrineExtension extends AbstractExtension |
||
16 | { |
||
17 | /** @var SqlFormatter */ |
||
18 | private $sqlFormatter; |
||
19 | |||
20 | /** |
||
21 | * Define our functions |
||
22 | * |
||
23 | * @return TwigFilter[] |
||
24 | */ |
||
25 | public function getFilters() |
||
26 | { |
||
27 | return [ |
||
28 | new TwigFilter('doctrine_pretty_query', [$this, 'formatQuery'], ['is_safe' => ['html'], 'deprecated' => true]), |
||
29 | new TwigFilter('doctrine_prettify_sql', [$this, 'prettifySql'], ['is_safe' => ['html']]), |
||
30 | new TwigFilter('doctrine_format_sql', [$this, 'formatSql'], ['is_safe' => ['html']]), |
||
31 | new TwigFilter('doctrine_replace_query_parameters', [$this, 'replaceQueryParameters']), |
||
32 | ]; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Get the possible combinations of elements from the given array |
||
37 | */ |
||
38 | private function getPossibleCombinations(array $elements, int $combinationsLevel) : array |
||
76 | |||
77 | /** |
||
78 | * Escape parameters of a SQL query |
||
79 | * DON'T USE THIS FUNCTION OUTSIDE ITS INTENDED SCOPE |
||
80 | * |
||
81 | * @internal |
||
82 | * |
||
83 | * @param mixed $parameter |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public static function escapeFunction($parameter) |
||
88 | { |
||
89 | $result = $parameter; |
||
90 | |||
91 | switch (true) { |
||
92 | // Check if result is non-unicode string using PCRE_UTF8 modifier |
||
93 | case is_string($result) && ! preg_match('//u', $result): |
||
94 | $result = '0x' . strtoupper(bin2hex($result)); |
||
95 | break; |
||
96 | |||
97 | case is_string($result): |
||
98 | $result = "'" . addslashes($result) . "'"; |
||
99 | break; |
||
100 | |||
101 | case is_array($result): |
||
102 | foreach ($result as &$value) { |
||
103 | $value = static::escapeFunction($value); |
||
104 | } |
||
105 | |||
106 | $result = implode(', ', $result) ?: 'NULL'; |
||
107 | break; |
||
108 | |||
109 | case is_object($result): |
||
110 | $result = addslashes((string) $result); |
||
111 | break; |
||
112 | |||
113 | case $result === null: |
||
114 | $result = 'NULL'; |
||
115 | break; |
||
116 | |||
117 | case is_bool($result): |
||
118 | $result = $result ? '1' : '0'; |
||
119 | break; |
||
120 | } |
||
121 | |||
122 | return $result; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Return a query with the parameters replaced |
||
127 | * |
||
128 | * @param string $query |
||
129 | * @param array|Data $parameters |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | public function replaceQueryParameters($query, $parameters) |
||
163 | |||
164 | /** |
||
165 | * Formats and/or highlights the given SQL statement. |
||
166 | * |
||
167 | * @param string $sql |
||
168 | * @param bool $highlightOnly If true the query is not formatted, just highlighted |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | public function formatQuery($sql, $highlightOnly = false) |
||
187 | |||
188 | public function prettifySql(string $sql) : string |
||
194 | |||
195 | public function formatSql(string $sql, bool $highlight) : string |
||
201 | |||
202 | private function setUpSqlFormatter(bool $highlight = true, bool $legacy = false) : void |
||
217 | |||
218 | /** |
||
219 | * Get the name of the extension |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | public function getName() |
||
227 | } |
||
228 |