-
-
Save vasildakov-zz/9d3d7967a7d0f908bf9c935f54a4a445 to your computer and use it in GitHub Desktop.
Zend Expressive Doctrine Factory and Redis Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Configuration for doctrine cli. | |
* | |
* This file is auto detected and used by doctrine cli. | |
*/ | |
use Doctrine\ORM\Tools\Console\ConsoleRunner; | |
use Doctrine\ORM\EntityManager; | |
require 'vendor/autoload.php'; | |
/** @var \Interop\Container\ContainerInterface $container */ | |
$container = require 'config/container.php'; | |
/** @var \Doctrine\ORM\EntityManager $em */ | |
$em = $container->get(EntityManager::class); | |
return ConsoleRunner::createHelperSet($em); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Repository\RepositoryFactory; | |
return [ | |
'dependencies' => [ | |
'invokables' => [ | |
// ... | |
], | |
'factories' => [ | |
// ... | |
Doctrine\Common\Cache\Cache::class => App\Container\DoctrineRedisCacheFactory::class, | |
Doctrine\ORM\EntityManager::class => App\Container\DoctrineFactory::class, | |
], | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'doctrine' => [ | |
'orm' => [ | |
'auto_generate_proxy_classes' => false, | |
'proxy_dir' => 'data/cache/EntityProxy', | |
'proxy_namespace' => 'EntityProxy', | |
'underscore_naming_strategy' => true, | |
], | |
'connection' => [ | |
// default connection name | |
'orm_default' => [ | |
'driver' => 'pdo_mysql', | |
'host' => '127.0.0.1', | |
'port' => '3306', | |
'dbname' => 'stacker', | |
'user' => 'stacker', | |
'password' => 'stacker', | |
'charset' => 'UTF8', | |
], | |
], | |
'cache' => [ | |
'redis' => [ | |
'host' => '127.0.0.1', | |
'port' => '6379', | |
], | |
], | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Action; | |
use App\Domain\Video\Video; | |
use App\Repository\VideoRepository; | |
use Doctrine\ORM\EntityManager; | |
use Interop\Container\ContainerInterface; | |
use Psr\Http\Message\ResponseInterface; | |
use Psr\Http\Message\ServerRequestInterface; | |
use Zend\Diactoros\Response\HtmlResponse; | |
use Zend\Diactoros\Response\JsonResponse; | |
use Zend\Expressive\Template\TemplateRendererInterface; | |
class IndexAction | |
{ | |
private $container; | |
private $template; | |
public function __construct(ContainerInterface $container, TemplateRendererInterface $template) | |
{ | |
$this->container = $container; | |
$this->template = $template; | |
} | |
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) | |
{ | |
/** @var \Doctrine\ORM\EntityManager $em */ | |
$em = $this->container->get(EntityManager::class); | |
$userRepository = $em->getRepository('App\Domain\User\User'); | |
$users = $userRepository->findAll(); | |
return new HtmlResponse($this->template->render('app::index')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Container; | |
use Doctrine\Common\Annotations\AnnotationReader; | |
use Doctrine\Common\Annotations\AnnotationRegistry; | |
use Doctrine\Common\Cache\Cache; | |
use Doctrine\ORM\Configuration; | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; | |
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy; | |
use Interop\Container\ContainerInterface; | |
class DoctrineFactory | |
{ | |
public function __invoke(ContainerInterface $container) | |
{ | |
$config = $container->has('config') ? $container->get('config') : []; | |
$proxyDir = (isset($config['doctrine']['orm']['proxy_dir'])) ? | |
$config['doctrine']['orm']['proxy_dir'] : 'data/cache/EntityProxy'; | |
$proxyNamespace = (isset($config['doctrine']['orm']['proxy_namespace'])) ? | |
$config['doctrine']['orm']['proxy_namespace'] : 'EntityProxy'; | |
$autoGenerateProxyClasses = (isset($config['doctrine']['orm']['auto_generate_proxy_classes'])) ? | |
$config['doctrine']['orm']['auto_generate_proxy_classes'] : false; | |
$underscoreNamingStrategy = (isset($config['doctrine']['orm']['underscore_naming_strategy'])) ? | |
$config['doctrine']['orm']['underscore_naming_strategy'] : false; | |
// Doctrine ORM | |
$doctrine = new Configuration(); | |
$doctrine->setProxyDir($proxyDir); | |
$doctrine->setProxyNamespace($proxyNamespace); | |
$doctrine->setAutoGenerateProxyClasses($autoGenerateProxyClasses); | |
if ($underscoreNamingStrategy) { | |
$doctrine->setNamingStrategy(new UnderscoreNamingStrategy()); | |
} | |
// ORM mapping by Annotation | |
AnnotationRegistry::registerFile('vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); | |
$driver = new AnnotationDriver( | |
new AnnotationReader(), | |
['src/Domain'] | |
); | |
$doctrine->setMetadataDriverImpl($driver); | |
// Cache | |
$cache = $container->get(Cache::class); | |
$doctrine->setQueryCacheImpl($cache); | |
$doctrine->setResultCacheImpl($cache); | |
$doctrine->setMetadataCacheImpl($cache); | |
// EntityManager | |
return EntityManager::create($config['doctrine']['connection']['orm_default'], $doctrine); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Container; | |
use Doctrine\Common\Cache\RedisCache; | |
use Interop\Container\ContainerInterface; | |
class DoctrineRedisCacheFactory | |
{ | |
public function __invoke(ContainerInterface $container) | |
{ | |
$config = $container->has('config') ? $container->get('config') : []; | |
$redis = new \Redis(); | |
$redis->connect( | |
$config['doctrine']['cache']['redis']['host'], | |
$config['doctrine']['cache']['redis']['port'] | |
); | |
$cache = new RedisCache(); | |
$cache->setRedis($redis); | |
return $cache; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment