Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameters injections to ViewRenderer + minor refactoring #122

Merged
merged 26 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
47c0cb7
Add parameters injections in ViewRenderer.
vjik Aug 8, 2020
0956c0d
Minor improve findLayoutFile method in ViewRenderer.
vjik Aug 8, 2020
6fb446d
Refactoring method getName in ViewRenderer.
vjik Aug 8, 2020
1002f1f
Add ViewRenderer params.
vjik Aug 9, 2020
780a4b3
Remove use ContainerInterface in ViewRenderer.
vjik Aug 9, 2020
02021a2
Remove withConfig() method from InjectionInterface.
vjik Aug 9, 2020
62a8e87
Add methods addContentInjections, withContentInjections, addLayoutInj…
vjik Aug 9, 2020
88f93d9
Add methods getMetaTags() and getLinkTags to InjectionInterface.
vjik Aug 9, 2020
c1259d2
Merge contentInjections and layoutInjections.
vjik Aug 16, 2020
a3aa9f3
Add ApplicationInjection.
vjik Aug 16, 2020
85578b6
Add example of meta and link tags to ApplicationInjection.
vjik Aug 16, 2020
c58402f
Split private method inject() to contentInject() and layoutInject().
vjik Aug 16, 2020
d872bd0
Split InjectionInterface to separately interface for each type.
vjik Aug 16, 2020
5e54115
Minor refactoring.
vjik Aug 16, 2020
61ab00d
Refactoring ViewRenderer.
vjik Aug 16, 2020
ec03ffb
Code style fix.
vjik Aug 16, 2020
5007096
Move config injections from params.php to web.php.
vjik Aug 16, 2020
7811c73
Rename addInjections to withAddedInjections.
vjik Aug 16, 2020
e447fb7
Replace isset to array_key_exists in getName().
vjik Aug 18, 2020
d564fca
Add phpdoc to interfaces.
vjik Aug 18, 2020
e85a1dc
Minor fix in phpdoc.
vjik Aug 18, 2020
7608fbc
Make static method getName() is dynamic.
vjik Aug 19, 2020
0884161
Add withCsrf().
vjik Aug 19, 2020
ad098b9
Merge remote-tracking branch 'remotes/upstream/master' into view-rend…
vjik Sep 10, 2020
e289acb
Use session from yiisoft/session.
vjik Sep 14, 2020
be5f6b7
Use yiisoft/yii-view.
vjik Sep 14, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"php": "^7.4|^8.0",
"cebe/markdown": "^1.2@dev",
"cycle/proxy-factory": "^1.2",
"yiisoft/composer-config-plugin": "^1.0@dev",
"nyholm/psr7": "^1.0",
"psr/log": "^1.1",
"yiisoft/access": "^1.0",
Expand All @@ -30,9 +29,10 @@
"yiisoft/auth": "^1.0",
"yiisoft/cache": "^3.0@dev",
"yiisoft/cache-file": "^3.0@dev",
"yiisoft/composer-config-plugin": "^1.0@dev",
"yiisoft/data": "^3.0@dev",
"yiisoft/data-response": "^3.0@dev",
"yiisoft/di": "^3.0@dev",
"yiisoft/yii-event": "^3.0@dev",
"yiisoft/factory": "^3.0@dev",
"yiisoft/html": "^3.0@dev",
"yiisoft/injector": "^1.0",
Expand All @@ -45,18 +45,19 @@
"yiisoft/router": "^3.0@dev",
"yiisoft/router-fastroute": "^3.0@dev",
"yiisoft/security": "^3.0@dev",
"yiisoft/serializer": "^3.0@dev",
"yiisoft/strings": "^1.0",
"yiisoft/var-dumper": "^3.0@dev",
"yiisoft/view": "^3.0@dev",
"yiisoft/widget": "^3.0@dev",
"yiisoft/yii-bootstrap4": "^3.0@dev",
"yiisoft/yii-console": "^3.0@dev",
"yiisoft/yii-cycle": "^3.0@dev",
"yiisoft/yii-web": "^3.0@dev",
"yiisoft/yii-debug": "^3.0@dev",
"yiisoft/yii-debug-api": "^3.0@dev",
"yiisoft/serializer": "^3.0@dev",
"yiisoft/data-response": "^3.0@dev",
"yiisoft/yii-bootstrap4": "^3.0@dev"
"yiisoft/yii-event": "^3.0@dev",
"yiisoft/yii-view": "^3.0@dev",
"yiisoft/yii-web": "^3.0@dev"
},
"require-dev": {
"roave/security-advisories": "dev-master",
Expand Down
20 changes: 14 additions & 6 deletions config/params.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use App\ApplicationViewInjection;
use App\Command;
use Cycle\Schema\Generator;
use Yiisoft\Factory\Definitions\Reference;

return [
'yiisoft/yii-debug' => [
Expand Down Expand Up @@ -40,17 +42,17 @@

'yiisoft/yii-cycle' => [
'dbal' => [
'default' => 'default',
'aliases' => [],
'databases' => [
'default' => 'default',
'aliases' => [],
'databases' => [
'default' => ['connection' => 'sqlite']
],
'connections' => [
'sqlite' => [
'driver' => \Spiral\Database\Driver\SQLite\SQLiteDriver::class,
'driver' => \Spiral\Database\Driver\SQLite\SQLiteDriver::class,
'connection' => 'sqlite:@runtime/database.db',
'username' => '',
'password' => '',
'username' => '',
'password' => '',
],
],
// 'query-logger' => \Yiisoft\Yii\Cycle\Logger\StdoutQueryLogger::class,
Expand Down Expand Up @@ -80,4 +82,10 @@
'@src/Blog/Entity',
],
],

'yiisoft/yii-view' => [
'injections' => [
Reference::to(ApplicationViewInjection::class),
],
],
];
13 changes: 4 additions & 9 deletions config/web.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

declare(strict_types=1);

use App\Blog\Comment\CommentRepository;
use App\Blog\Comment\CommentService;
use App\Contact\ContactMailer;
use App\Factory\MiddlewareDispatcherFactory;
use App\ViewRenderer;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\RequestFactoryInterface;
Expand All @@ -19,9 +20,9 @@
use Yiisoft\DataResponse\DataResponseFactoryInterface;
use Yiisoft\DataResponse\DataResponseFormatterInterface;
use Yiisoft\DataResponse\Formatter\HtmlDataResponseFormatter;
use Yiisoft\Session\Session;
use Yiisoft\Session\SessionInterface;
use Yiisoft\Yii\Web\MiddlewareDispatcher;
use Yiisoft\Yii\Web\Session\Session;
use Yiisoft\Yii\Web\Session\SessionInterface;
use App\Repository\UserRepository;

/**
Expand Down Expand Up @@ -57,10 +58,4 @@
$container->get(CommentRepository::class)
);
},
ViewRenderer::class => [
'__construct()' => [
'viewBasePath' => '@views',
'layout' => '@views/layout/main',
],
],
];
60 changes: 60 additions & 0 deletions src/ApplicationViewInjection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace App;

use Yiisoft\Router\UrlMatcherInterface;
use Yiisoft\Yii\View\LayoutParametersInjectionInterface;
use Yiisoft\Yii\View\LinkTagsInjectionInterface;
use Yiisoft\Yii\View\MetaTagsInjectionInterface;
use Yiisoft\Yii\Web\User\User;

class ApplicationViewInjection implements
LayoutParametersInjectionInterface,
MetaTagsInjectionInterface,
LinkTagsInjectionInterface
{

private User $user;
private UrlMatcherInterface $urlMatcher;

public function __construct(
User $user,
UrlMatcherInterface $urlMatcher
) {
$this->user = $user;
$this->urlMatcher = $urlMatcher;
}

public function getLayoutParameters(): array
{
return [
'user' => $this->user->getIdentity(),
'currentUrl' => (string)$this->urlMatcher->getLastMatchedRequest()->getUri(),
'brandLabel' => 'Yii Demo',
];
}

public function getMetaTags(): array
{
return [
[
'__key' => 'generator',
'name' => 'generator',
'value' => 'Yii',
],
];
}

public function getLinkTags(): array
{
return [
[
'__key' => 'favicon',
'name' => 'icon',
'value' => '/favicon.ico',
],
];
}
}
4 changes: 3 additions & 1 deletion src/Blog/Archive/ArchiveController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Blog\Archive;

use App\Blog\Tag\TagRepository;
use App\ViewRenderer;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Yii\View\ViewRenderer;

final class ArchiveController
{
Expand Down
2 changes: 1 addition & 1 deletion src/Blog/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use App\Blog\Archive\ArchiveRepository;
use App\Blog\Post\PostRepository;
use App\Blog\Tag\TagRepository;
use App\ViewRenderer;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Yii\View\ViewRenderer;
use Yiisoft\Yii\Web\User\User;

final class BlogController
Expand Down
2 changes: 1 addition & 1 deletion src/Blog/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace App\Blog;

use App\Blog\Comment\CommentService;
use App\ViewRenderer;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Yii\View\ViewRenderer;

final class CommentController
{
Expand Down
4 changes: 3 additions & 1 deletion src/Blog/Post/PostController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Blog\Post;

use App\Blog\Entity\Post;
use App\Blog\Entity\Tag;
use App\Entity\User;
use App\ViewRenderer;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Transaction;
use Psr\Http\Message\ResponseFactoryInterface;
Expand All @@ -15,6 +16,7 @@
use Yiisoft\Access\AccessCheckerInterface;
use Yiisoft\Http\Method;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Yii\View\ViewRenderer;
use Yiisoft\Yii\Web\User\User as UserComponent;

final class PostController
Expand Down
4 changes: 3 additions & 1 deletion src/Blog/Tag/TagController.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Blog\Tag;

use App\Blog\Post\PostRepository;
use App\ViewRenderer;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Yii\View\ViewRenderer;

final class TagController
{
Expand Down
4 changes: 3 additions & 1 deletion src/Contact/ContactController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Contact;

use App\ViewRenderer;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Yiisoft\Http\Method;
use Yiisoft\Yii\View\ViewRenderer;

class ContactController
{
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/AuthController.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use App\ViewRenderer;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Yiisoft\Auth\IdentityRepositoryInterface;
use Yiisoft\Http\Method;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Yii\View\ViewRenderer;
use Yiisoft\Yii\Web\User\User;

class AuthController
Expand Down Expand Up @@ -75,10 +77,9 @@ public function login(
}
}

return $this->viewRenderer->render(
return $this->viewRenderer->withCsrf()->render(
'login',
[
'csrf' => $request->getAttribute('csrf_token'),
'body' => $body,
'error' => $error,
]
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/SignupController.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Entity\User;
use App\ViewRenderer;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Transaction;
use Psr\Http\Message\RequestInterface;
Expand All @@ -13,6 +14,7 @@
use Yiisoft\Auth\IdentityRepositoryInterface;
use Yiisoft\Http\Method;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Yii\View\ViewRenderer;

final class SignupController
{
Expand Down Expand Up @@ -66,12 +68,11 @@ public function signup(
}
}

return $this->viewRenderer->render(
return $this->viewRenderer->withCsrf()->render(
'signup',
[
'body' => $body,
'error' => $error,
'csrf' => $request->getAttribute('csrf_token'),
]
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/SiteController.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use Psr\Http\Message\ResponseInterface;
use App\ViewRenderer;
use Yiisoft\Yii\View\ViewRenderer;

class SiteController
{
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/UserController.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Repository\UserRepository;
use App\ViewRenderer;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Yiisoft\Data\Paginator\OffsetPaginator;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Yii\View\ViewRenderer;

class UserController
{
Expand Down
6 changes: 3 additions & 3 deletions src/Factory/MiddlewareDispatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
namespace App\Factory;

use Psr\Container\ContainerInterface;
use Yiisoft\Csrf\CsrfMiddleware;
use Yiisoft\Router\Middleware\Router;
use Yiisoft\Session\SessionMiddleware;
use Yiisoft\Yii\Web\ErrorHandler\ErrorCatcher;
use Yiisoft\Yii\Web\Middleware\Csrf;
use Yiisoft\Yii\Web\Middleware\SubFolder;
use Yiisoft\Yii\Web\MiddlewareDispatcher;
use Yiisoft\Yii\Web\Session\SessionMiddleware;

final class MiddlewareDispatcherFactory
{
public function __invoke(ContainerInterface $container)
{
$csrf = $container->get(Csrf::class);
$csrf = $container->get(CsrfMiddleware::class);
$session = $container->get(SessionMiddleware::class);
$router = $container->get(Router::class);
$errorCatcher = $container->get(ErrorCatcher::class);
Expand Down
Loading