diff --git a/Controller/StaticController.php b/Controller/StaticController.php index 173738a..38a9f9e 100755 --- a/Controller/StaticController.php +++ b/Controller/StaticController.php @@ -14,6 +14,8 @@ class StaticController extends Controller { // The excution flow can go e.g.: findAction -> renderTwig -> showTwigError, // and we'd like to keep some information around. + /** @var bool The kernel.debug parameter */ + protected $debug; /** @var string Base URL for a request, e.g. '/static/bundleref/' */ protected $baseUrl; /** @var null|string Valid bundle name, e.g. 'SomeCoolBundle' */ @@ -44,7 +46,8 @@ public function rootAction() { * @throws \Exception */ public function findAction($slug, $path) { - $cleanPath = Utils::getCleanPath($path); + $this->debug = $this->getParameter('kernel.debug'); + $cleanPath = Utils::getCleanPath($path); $pathExt = pathinfo($cleanPath, PATHINFO_EXTENSION); $showSource = $pathExt === 'twig'; @@ -55,25 +58,30 @@ public function findAction($slug, $path) { ]); // Redirect if we can clean up the URL - if ($cleanPath !== $path) { + if ($cleanPath !== $path) { return $this->redirect($this->baseUrl . $cleanPath); - } + } + + // Redirect if URL asks for a Twig file but we’re not in debug mode + if (!$this->debug && substr($cleanPath, -5) === '.twig') { + return $this->redirect($this->baseUrl . substr($cleanPath, 0, -5)); + } - // Figure out bundle name + // Figure out bundle name $bundleConfig = $this->getParameter('twig_express.bundles'); if (!array_key_exists($slug, $bundleConfig)) { - $rootUrl = $this->generateUrl('gradientz_twig_express_root'); - return $this->redirect($rootUrl . '?was=' . $slug); - } + $rootUrl = $this->generateUrl('gradientz_twig_express_root'); + return $this->redirect($rootUrl . '?was=' . $slug); + } $this->bundleName = $bundleConfig[$slug]['name']; - // Check that it's a valid bundle + // Check that it's a valid bundle $allBundles = array_keys($this->get('kernel')->getBundles()); - if (!in_array($this->bundleName, $allBundles)) { - throw new \Exception('Unknown bundle "'.$this->bundleName.'". Make sur this bundle is installed and your "twig_express.bundles" config is correct.'); + if (!in_array($this->bundleName, $allBundles)) { + throw new \Exception('Unknown bundle "'.$this->bundleName.'". Make sur this bundle is installed and your "twig_express.bundles" config is correct.'); } - // Where is our "document root"? + // Where is our "document root"? $docRoot = $bundleConfig[$slug]['root']; $this->bundlePath = rtrim($this->container->get('kernel')->locateResource('@'.$this->bundleName), '/'); $this->docRootName = '@'.$this->bundleName . '/' . $docRoot; @@ -110,13 +118,13 @@ public function findAction($slug, $path) { return $this->render404($cleanPath); } - /** - * Show a directory listing page - * @param string $dirPath Directory system path + /** + * Show a directory listing page + * @param string $dirPath Directory system path * @param string $urlFragment URL without base or bundle key - * @return Response - */ - private function renderDir($dirPath, $urlFragment) { + * @return Response + */ + private function renderDir($dirPath, $urlFragment) { // Prepare breadcrumbs $breadcrumbs = Utils::makeBreadcrumbs( $this->baseUrl, @@ -124,94 +132,99 @@ private function renderDir($dirPath, $urlFragment) { $urlFragment ); - // Prepare content list - $finder = new Finder(); - $iterator = $finder->depth(0)->in($dirPath)->sortByName(); - $dirList = []; - $fileList = []; - foreach ($iterator->directories() as $dir) { + // Prepare content list + $finder = new Finder(); + $iterator = $finder->depth(0)->in($dirPath)->sortByName(); + $dirList = []; + $fileList = []; + foreach ($iterator->directories() as $dir) { $dirList[] = $dir->getFilename(); - } - foreach ($iterator->files()->name('*.twig') as $file) { + } + foreach ($iterator->files()->name('*.twig') as $file) { $fileList[] = str_replace('.twig', '', $file->getFilename()); - } + } - return $this->render('@GradientzTwigExpress/dir.html.twig', [ - 'crumbs' => $breadcrumbs, - 'dirList' => $dirList, - 'fileList' => $fileList - ]); - } + return $this->render('@GradientzTwigExpress/dir.html.twig', [ + 'crumbs' => $breadcrumbs, + 'dirList' => $dirList, + 'fileList' => $fileList + ]); + } - /** - * Show a File Not Found page + /** + * Show a File Not Found page * @param string $urlFragment URL without base or bundle key - * @return Response - */ - private function render404($urlFragment) { - // Prepare breadcrumbs + * @return Response + */ + private function render404($urlFragment) { + // Prepare breadcrumbs $breadcrumbs = Utils::makeBreadcrumbs( $this->baseUrl, $this->bundleName, $urlFragment ); - // Prepare message + // Prepare message $root = $this->docRootName; if (substr($urlFragment, -5) === '.twig' || substr($urlFragment, -1) === '/') { $miss = $urlFragment; } else { $miss = $urlFragment . '.twig'; } - $message = '
Could not find : '.$miss.'
';
- $message .= "\nIn : $root
Could not find : '.$miss.'
';
+ $message .= "\nIn : $root
$fileName
"
- ];
+ 'message' => "$message$fileName
"
+ ];
// Get a few lines of code from the buggy template
- if (file_exists($filePath)) {
+ if (file_exists($filePath)) {
$code = file_get_contents($filePath);
$data['code'] = Utils::formatCodeBlock($code, true, $line, 5);
$data['codeLang'] = Utils::getHighlightLanguage($fileRef);
- }
+ }
return $this->render('@GradientzTwigExpress/page.html.twig', $data);
}
- /**
- * Show a Twig file with syntax highlighting
- * @param string $systemPath Full path to file
+ /**
+ * Show a Twig file with syntax highlighting
+ * @param string $systemPath Full path to file
* @param string $urlFragment URL without base or bundle key
* @return Response
- */
- private function showSource($systemPath, $urlFragment) {
+ */
+ private function showSource($systemPath, $urlFragment) {
// Prepare breadcrumbs
$breadcrumbs = Utils::makeBreadcrumbs(
$this->baseUrl,
@@ -273,13 +286,13 @@ private function showSource($systemPath, $urlFragment) {
$urlFragment
);
$code = file_get_contents($systemPath);
- $data = [
- 'metaTitle' => 'Source: ' . basename($systemPath),
- 'crumbs' => $breadcrumbs,
- 'code' => Utils::formatCodeBlock($code, true),
+ $data = [
+ 'metaTitle' => 'Source: ' . basename($systemPath),
+ 'crumbs' => $breadcrumbs,
+ 'code' => Utils::formatCodeBlock($code, true),
'codeLang' => Utils::getHighlightLanguage($systemPath)
- ];
+ ];
return $this->render('@GradientzTwigExpress/page.html.twig', $data);
- }
+ }
}
diff --git a/Resources/public/demo/styles.css b/Resources/public/demo/styles.css
index 30f83ef..2eac971 100644
--- a/Resources/public/demo/styles.css
+++ b/Resources/public/demo/styles.css
@@ -1,18 +1,18 @@
body::after,
.test {
- display: block;
- margin: 1em 0;
- padding: 2em 1em;
- text-align: center;
- color: white;
- background: black;
+ display: block;
+ margin: 1em 0;
+ padding: 2em 1em;
+ text-align: center;
+ color: white;
+ background: black;
}
body::after {
- content: "Stylesheet 1/1 is loaded";
- background: blue;
+ content: "Stylesheet 1/1 is loaded";
+ background: blue;
}
.test {
- background: green;
+ background: green;
}