Skip to content

Commit

Permalink
Revert "Added creation of a cloned WebView for rendering except ViewR…
Browse files Browse the repository at this point in the history
…enderer::extractControllerName() (#3)"

This partially reverts commit a6f4d82.
  • Loading branch information
samdark committed Oct 19, 2020
1 parent ece3baa commit 49a0e52
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/ViewRenderer.php
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Yii\View;

use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Yiisoft\Aliases\Aliases;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\DataResponse\DataResponseFactoryInterface;
Expand All @@ -15,6 +14,7 @@

final class ViewRenderer implements ViewContextInterface
{

private DataResponseFactoryInterface $responseFactory;
private Aliases $aliases;
private WebView $view;
Expand Down Expand Up @@ -64,8 +64,7 @@ public function render(string $view, array $parameters = []): ResponseInterface

public function renderPartial(string $view, array $parameters = []): ResponseInterface
{
$newView = clone $this->view;
$content = $newView->render($view, $parameters, $this);
$content = $this->view->render($view, $parameters, $this);

return $this->responseFactory->createResponse($content);
}
Expand Down Expand Up @@ -117,7 +116,7 @@ public function withLayout(string $layout): self
public function withAddedInjections(array $injections): self
{
$new = clone $this;
$new->injections = \array_merge($this->injections, $injections);
$new->injections = array_merge($this->injections, $injections);
return $new;
}

Expand Down Expand Up @@ -148,13 +147,11 @@ public function withCsrf(): self

private function renderProxy(string $view, array $parameters = []): string
{
$newView = clone $this->view;

$this->injectMetaTags($newView);
$this->injectLinkTags($newView);
$this->injectMetaTags();
$this->injectLinkTags();

$parameters = $this->getContentParameters($parameters);
$content = $newView->render($view, $parameters, $this);
$content = $this->view->render($view, $parameters, $this);

$layout = $this->findLayoutFile($this->layout);
if ($layout === null) {
Expand All @@ -163,28 +160,32 @@ private function renderProxy(string $view, array $parameters = []): string

$layoutParameters = $this->getLayoutParameters(['content' => $content]);

return $newView->renderFile($layout, $layoutParameters, $this);
return $this->view->renderFile(
$layout,
$layoutParameters,
$this,
);
}

private function injectMetaTags(WebView $view): void
private function injectMetaTags(): void
{
foreach ($this->injections as $injection) {
if ($injection instanceof MetaTagsInjectionInterface) {
foreach ($injection->getMetaTags() as $options) {
$key = ArrayHelper::remove($options, '__key');
$view->registerMetaTag($options, $key);
$this->view->registerMetaTag($options, $key);
}
}
}
}

private function injectLinkTags(WebView $view): void
private function injectLinkTags(): void
{
foreach ($this->injections as $injection) {
if ($injection instanceof LinkTagsInjectionInterface) {
foreach ($injection->getLinkTags() as $options) {
$key = ArrayHelper::remove($options, '__key');
$view->registerLinkTag($options, $key);
$this->view->registerLinkTag($options, $key);
}
}
}
Expand All @@ -194,7 +195,7 @@ private function getContentParameters(array $parameters): array
{
foreach ($this->injections as $injection) {
if ($injection instanceof ContentParametersInjectionInterface) {
$parameters = \array_merge($parameters, $injection->getContentParameters());
$parameters = array_merge($parameters, $injection->getContentParameters());
}
}
return $parameters;
Expand All @@ -204,7 +205,7 @@ private function getLayoutParameters(array $parameters): array
{
foreach ($this->injections as $injection) {
if ($injection instanceof LayoutParametersInjectionInterface) {
$parameters = \array_merge($parameters, $injection->getLayoutParameters());
$parameters = array_merge($parameters, $injection->getLayoutParameters());
}
}
return $parameters;
Expand All @@ -218,7 +219,7 @@ private function findLayoutFile(?string $file): ?string

$file = $this->aliases->get($file);

if (\pathinfo($file, PATHINFO_EXTENSION) !== '') {
if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
return $file;
}

Expand All @@ -243,18 +244,18 @@ private function extractControllerName(object $controller): string
{
static $cache = [];

$class = \get_class($controller);
if (\array_key_exists($class, $cache)) {
$class = get_class($controller);
if (array_key_exists($class, $cache)) {
return $cache[$class];
}

$regexp = '/((?<=controller\\\|s\\\)(?:[\w\\\]+)|(?:[a-z]+))controller/iuU';
if (!\preg_match($regexp, $class, $m) || empty($m[1])) {
throw new RuntimeException('Cannot detect controller name.');
if (!preg_match($regexp, $class, $m) || empty($m[1])) {
throw new \RuntimeException('Cannot detect controller name');
}

$inflector = new Inflector();
$name = \str_replace('\\', '/', $m[1]);
$name = str_replace('\\', '/', $m[1]);
return $cache[$class] = $inflector->pascalCaseToId($name);
}
}

0 comments on commit 49a0e52

Please sign in to comment.