@@ -66,9 +66,11 @@ private function getControllerCode()
66
66
$ class = new \ReflectionClass ($ className );
67
67
$ method = $ class ->getMethod ($ this ->controller [1 ]);
68
68
69
- $ code = file ($ class ->getFilename ());
69
+ $ classCode = file ($ class ->getFilename ());
70
+ $ methodCode = array_slice ($ classCode , $ method ->getStartline () - 1 , $ method ->getEndLine () - $ method ->getStartline () + 1 );
71
+ $ controllerCode = ' ' .$ method ->getDocComment ()."\n" .implode ('' , $ methodCode );
70
72
71
- return ' ' . $ method -> getDocComment (). "\n" . implode ( '' , array_slice ( $ code , $ method -> getStartline () - 1 , $ method -> getEndLine () - $ method -> getStartline () + 1 ) );
73
+ return $ this -> unindentCode ( $ controllerCode );
72
74
}
73
75
74
76
private function getControllerRelativePath ()
@@ -106,6 +108,32 @@ private function getTemplateRelativePath()
106
108
return 'app/Resources/views/ ' .$ this ->template ->getTemplateName ();
107
109
}
108
110
111
+ /**
112
+ * Utility method that "unindents" the given $code when all its lines start
113
+ * with a tabulation of four white spaces.
114
+ *
115
+ * @param string $code
116
+ * @return string
117
+ */
118
+ private function unindentCode ($ code )
119
+ {
120
+ $ formattedCode = '' ;
121
+ $ codeLines = explode ("\n" , $ code );
122
+
123
+ $ indentedLines = array_filter ($ codeLines , function ($ lineOfCode ) {
124
+ return '' === $ lineOfCode || ' ' === substr ($ lineOfCode , 0 , 4 );
125
+ });
126
+
127
+ if (count ($ indentedLines ) === count ($ codeLines )) {
128
+ $ formattedCode = array_map (function ($ lineOfCode ) { return substr ($ lineOfCode , 4 ); }, $ codeLines );
129
+ $ formattedCode = implode ("\n" , $ formattedCode );
130
+ } else {
131
+ $ formattedCode = $ code ;
132
+ }
133
+
134
+ return $ formattedCode ;
135
+ }
136
+
109
137
// the name of the Twig extension must be unique in the application
110
138
public function getName ()
111
139
{
0 commit comments