Permalink
Browse files

Refactor the view assistant

  • Loading branch information...
1 parent 08a53a3 commit 17f5fae9ef26cd4ae15b4921cf71022c9624675b @Herzult Herzult committed Nov 7, 2012
Showing with 83 additions and 18 deletions.
  1. +2 −18 Controller/AssistantController.php
  2. +35 −0 Filesystem/PathExpander.php
  3. +11 −0 Resources/config/services.xml
  4. +35 −0 View/PathDeducer.php
@@ -8,7 +8,7 @@ class AssistantController extends Controller
{
public function missingViewAction($viewName, $viewParams)
{
- $viewPath = $this->deduceViewPath($viewName);
+ $viewPath = $this->get('knp_rad.view.path_deducer')->deducePath($viewName);
$viewBody = $this->renderView(
'KnpRadBundle:Assistant:_viewBody.twig.twig',
array(
@@ -31,7 +31,7 @@ public function createViewAction(Request $request)
{
$viewName = $request->request->get('viewName');
$viewBody = $request->request->get('viewBody');
- $viewPath = $this->deduceViewPath($viewName);
+ $viewPath = $this->get('knp_rad.view.path_deducer')->deducePath($viewName);
// in case directory does not exist
$this->get('filesystem')->touch($viewPath);
@@ -40,20 +40,4 @@ public function createViewAction(Request $request)
return new Response(null, 201);
}
-
- private function deduceViewPath($viewName)
- {
- $logicalName = $this->get('templating.name_parser')->parse($viewName)->getPath();
-
- if (!preg_match('#^@([^/]+)/(.*)$#', $logicalName, $match)) {
- throw new \RuntimeException(sprintf(
- 'Unable to deduce path from logical name "%s".',
- $logicalName
- ));
- }
-
- $bundle = $this->get('kernel')->getBundle($match[1]);
-
- return sprintf('%s/%s', $bundle->getPath(), $match[2]);
- }
}
@@ -0,0 +1,35 @@
+<?php
+
+namespace Knp\RadBundle\Filesystem;
+
+use Symfony\Component\HttpKernel\KernelInterface;
+
+class PathExpander
+{
+ private $kernel;
+
+ public function __construct(KernelInterface $kernel)
+ {
+ $this->kernel = $kernel;
+ }
+
+ /**
+ * Expands the given path if needed (i.e bundle resource path)
+ *
+ * @param string $path The path to expand
+ *
+ * @return string The expanded path
+ */
+ public function expand($path)
+ {
+ if (0 !== strpos($path, '@')) {
+ return $path;
+ }
+
+ list($bundleName, $relPath) = explode('/', substr($path, 1), 2);
+
+ $bundle = $this->kernel->getBundle($bundleName);
+
+ return sprintf('%s/%s', $bundle->getPath(), $relPath);
+ }
+}
@@ -19,6 +19,8 @@
<parameter key="knp_rad.resource.resolver.resource.class">Knp\RadBundle\Resource\Resolver\ResourceResolver</parameter>
<parameter key="knp_rad.resource.resolver.argument.class">Knp\RadBundle\Resource\Resolver\ArgumentResolver</parameter>
<parameter key="knp_rad.event_listener.resource_resolver.class">Knp\RadBundle\EventListener\ResourceResolverListener</parameter>
+ <parameter key="knp_rad.filesystem.path_expander.class">Knp\RadBundle\Filesystem\PathExpander</parameter>
+ <parameter key="knp_rad.view.path_deducer.class">Knp\RadBundle\View\PathDeducer</parameter>
</parameters>
<services>
@@ -77,6 +79,15 @@
<argument type="service" id="knp_rad.resource.resolver.request" />
<tag name="kernel.event_listener" event="kernel.request" />
</service>
+
+ <service id="knp_rad.filesystem.path_expander" class="%knp_rad.filesystem.path_expander.class%">
+ <argument type="service" id="kernel" />
+ </service>
+
+ <service id="knp_rad.view.path_deducer" class="%knp_rad.view.path_deducer.class%">
+ <argument type="service" id="templating.name_parser" />
+ <argument type="service" id="knp_rad.filesystem.path_expander" />
+ </service>
</services>
</container>
View
@@ -0,0 +1,35 @@
+<?php
+
+namespace Knp\RadBundle\View;
+
+use Symfony\Component\Templating\TemplateNameParserInterface;
+use Knp\RadBundle\Filesystem\PathExpander;
+
+/**
+ * The path deducer is responsible of deducing paths from logical view names
+ */
+class PathDeducer
+{
+ private $nameParser;
+ private $pathExpander;
+
+ public function __construct(TemplateNameParserInterface $nameParser, PathExpander $pathExpander)
+ {
+ $this->nameParser = $nameParser;
+ $this->pathExpander = $pathExpander;
+ }
+
+ /**
+ * Returns the path corresponding to the specified view name
+ *
+ * @param string $logicalName A logical view name (i.e FooBundle:Bar:baz.html.twig)
+ *
+ * @return string The path of the view
+ */
+ public function deducePath($logicalName)
+ {
+ $path = $this->nameParser->parse($logicalName)->getPath();
+
+ return $this->pathExpander->expand($path);
+ }
+}

0 comments on commit 17f5fae

Please sign in to comment.