@@ -25,10 +25,14 @@ class SourceCodeExtension extends \Twig_Extension
25
25
protected $ loader ;
26
26
protected $ controller ;
27
27
protected $ template ;
28
+ protected $ kernelRootDir ;
29
+ protected $ fileLinkFormat ;
28
30
29
- public function __construct (\Twig_LoaderInterface $ loader )
31
+ public function __construct (\Twig_LoaderInterface $ loader, $ kernelRootDir , $ fileLinkFormat )
30
32
{
33
+ $ this ->kernelRootDir = $ kernelRootDir ;
31
34
$ this ->loader = $ loader ;
35
+ $ this ->fileLinkFormat = $ fileLinkFormat ?: ini_get ('xdebug.file_link_format ' ) ?: get_cfg_var ('xdebug.file_link_format ' );
32
36
}
33
37
34
38
public function setController ($ controller )
@@ -48,18 +52,16 @@ public function showSourceCode(\Twig_Environment $twig, $template)
48
52
$ this ->template = $ template ;
49
53
50
54
return $ twig ->render ('default/_source_code.html.twig ' , array (
51
- 'controller_source_code ' => $ this ->getControllerCode (),
52
- 'controller_file_path ' => $ this ->getControllerRelativePath (),
53
- 'template_source_code ' => $ this ->getTemplateCode (),
54
- 'template_file_path ' => $ this ->getTemplateRelativePath (),
55
+ 'controller ' => $ this ->getController (),
56
+ 'template ' => $ this ->getTemplate (),
55
57
));
56
58
}
57
59
58
- private function getControllerCode ()
60
+ private function getController ()
59
61
{
60
62
// this happens for example for exceptions (404 errors, etc.)
61
63
if (null === $ this ->controller ) {
62
- return ' Not available ' ;
64
+ return ;
63
65
}
64
66
65
67
$ className = get_class ($ this ->controller [0 ]);
@@ -70,42 +72,22 @@ private function getControllerCode()
70
72
$ methodCode = array_slice ($ classCode , $ method ->getStartline () - 1 , $ method ->getEndLine () - $ method ->getStartline () + 1 );
71
73
$ controllerCode = ' ' .$ method ->getDocComment ()."\n" .implode ('' , $ methodCode );
72
74
73
- return $ this ->unindentCode ($ controllerCode );
74
- }
75
-
76
- private function getControllerRelativePath ()
77
- {
78
- // this happens for example for exceptions (404 errors, etc.)
79
- if (null === $ this ->controller ) {
80
- return '' ;
81
- }
82
-
83
- $ className = get_class ($ this ->controller [0 ]);
84
- $ class = new \ReflectionClass ($ className );
85
-
86
- $ absolutePath = $ class ->getFilename ();
87
- $ pathParts = explode (DIRECTORY_SEPARATOR .'src ' .DIRECTORY_SEPARATOR , $ absolutePath );
88
- $ relativePath = 'src ' .DIRECTORY_SEPARATOR .$ pathParts [1 ];
89
-
90
- return $ relativePath ;
75
+ return array (
76
+ 'file_path ' => $ class ->getFilename (),
77
+ 'starting_line ' => $ method ->getStartline (),
78
+ 'source_code ' => $ this ->unindentCode ($ controllerCode )
79
+ );
91
80
}
92
81
93
- private function getTemplateCode ()
82
+ private function getTemplate ()
94
83
{
95
- return $ this ->loader ->getSource ($ this ->template ->getTemplateName ());
96
- }
84
+ $ templateName = $ this ->template ->getTemplateName ();
97
85
98
- /**
99
- * The logic implemented in this method is solely developed for the Symfony
100
- * Demo application and cannot be used as a general purpose solution.
101
- * Specifically, this logic won't work for templates that use a namespaced path
102
- * (e.g. @WebProfiler/Collector/time.html.twig) or any loader different from
103
- * Twig_Loader_Filesystem (e.g. TwigBundle:Exception:exception.txt.twig notation
104
- * or an anonymous template created by the {% embed %} tag).
105
- */
106
- private function getTemplateRelativePath ()
107
- {
108
- return 'app/Resources/views/ ' .$ this ->template ->getTemplateName ();
86
+ return array (
87
+ 'file_path ' => $ this ->kernelRootDir .'/Resources/views/ ' .$ templateName ,
88
+ 'starting_line ' => 1 ,
89
+ 'source_code ' => $ this ->loader ->getSource ($ templateName ),
90
+ );
109
91
}
110
92
111
93
/**
0 commit comments