Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6378584

Browse filesBrowse files
committed
Added a method to unindent the source code of the controller
1 parent 427fe5a commit 6378584
Copy full SHA for 6378584

File tree

Expand file treeCollapse file tree

1 file changed

+30
-2
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+30
-2
lines changed

‎src/AppBundle/Twig/SourceCodeExtension.php

Copy file name to clipboardExpand all lines: src/AppBundle/Twig/SourceCodeExtension.php
+30-2Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ private function getControllerCode()
6666
$class = new \ReflectionClass($className);
6767
$method = $class->getMethod($this->controller[1]);
6868

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);
7072

71-
return ' '.$method->getDocComment()."\n".implode('', array_slice($code, $method->getStartline() - 1, $method->getEndLine() - $method->getStartline() + 1));
73+
return $this->unindentCode($controllerCode);
7274
}
7375

7476
private function getControllerRelativePath()
@@ -106,6 +108,32 @@ private function getTemplateRelativePath()
106108
return 'app/Resources/views/'.$this->template->getTemplateName();
107109
}
108110

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+
109137
// the name of the Twig extension must be unique in the application
110138
public function getName()
111139
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.