Skip to content

Commit db75f6c

Browse files
committed
minor symfony#28 Added a method to unindent the source code of the controller (javiereguiluz)
This PR was merged into the master branch. Discussion ---------- Added a method to unindent the source code of the controller ### Before ![before](https://cloud.githubusercontent.com/assets/73419/7025029/0e3b2a5a-dd41-11e4-8bba-3e846a7b1a48.png) ### After ![after](https://cloud.githubusercontent.com/assets/73419/7025030/0f442c4e-dd41-11e4-8d66-278dd2915751.png) Commits ------- 6378584 Added a method to unindent the source code of the controller
2 parents 427fe5a + 6378584 commit db75f6c

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/AppBundle/Twig/SourceCodeExtension.php

Lines 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy