mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Created EntityManagerFactory
This commit is contained in:
parent
db9051dcde
commit
03298fc448
5
bin/cli
5
bin/cli
@ -1,11 +1,14 @@
|
|||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
use Zend\Diactoros\ServerRequestFactory;
|
use Zend\Diactoros\ServerRequestFactory;
|
||||||
use Zend\Diactoros\Uri;
|
use Zend\Diactoros\Uri;
|
||||||
use Zend\Expressive\Application;
|
use Zend\Expressive\Application;
|
||||||
|
|
||||||
|
/** @var ContainerInterface $container */
|
||||||
|
$container = include __DIR__ . '/../config/container.php';
|
||||||
/** @var Application $app */
|
/** @var Application $app */
|
||||||
$app = include __DIR__ . '/../config/app.php';
|
$app = $container->get(Application::class);
|
||||||
|
|
||||||
$command = count($_SERVER['argv']) > 1 ? $_SERVER['argv'][1] : '';
|
$command = count($_SERVER['argv']) > 1 ? $_SERVER['argv'][1] : '';
|
||||||
$request = ServerRequestFactory::fromGlobals()
|
$request = ServerRequestFactory::fromGlobals()
|
||||||
|
11
cli-config.php
Normal file
11
cli-config.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
use Doctrine\ORM\EntityManager;
|
||||||
|
use Doctrine\ORM\Tools\Console\ConsoleRunner;
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
|
|
||||||
|
/** @var ContainerInterface $container */
|
||||||
|
$container = include __DIR__ . '/config/container.php';
|
||||||
|
/** @var EntityManager $em */
|
||||||
|
$em = $container->get(EntityManager::class);
|
||||||
|
|
||||||
|
return ConsoleRunner::createHelperSet($em);
|
@ -19,7 +19,8 @@
|
|||||||
"zendframework/zend-servicemanager": "^3.0",
|
"zendframework/zend-servicemanager": "^3.0",
|
||||||
"zendframework/zend-expressive-twigrenderer": "^1.0",
|
"zendframework/zend-expressive-twigrenderer": "^1.0",
|
||||||
"doctrine/orm": "^2.5",
|
"doctrine/orm": "^2.5",
|
||||||
"guzzlehttp/guzzle": "^6.2"
|
"guzzlehttp/guzzle": "^6.2",
|
||||||
|
"acelaya/zsm-annotated-services": "^0.2.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^4.8",
|
"phpunit/phpunit": "^4.8",
|
||||||
|
15
config/autoload/database.local.php.dist
Normal file
15
config/autoload/database.local.php.dist
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
return [
|
||||||
|
|
||||||
|
'database' => [
|
||||||
|
'driver' => 'pdo_mysql',
|
||||||
|
'user' => '',
|
||||||
|
'password' => '',
|
||||||
|
'dbname' => '',
|
||||||
|
'charset' => 'utf-8',
|
||||||
|
'driverOptions' => [
|
||||||
|
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
|
||||||
|
],
|
||||||
|
]
|
||||||
|
|
||||||
|
];
|
@ -1,16 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
|
use Acelaya\UrlShortener\Factory\EntityManagerFactory;
|
||||||
|
use Acelaya\UrlShortener\Service\UrlShortener;
|
||||||
|
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
|
||||||
|
use Doctrine\ORM\EntityManager;
|
||||||
use Zend\Expressive\Application;
|
use Zend\Expressive\Application;
|
||||||
use Zend\Expressive\Container;
|
use Zend\Expressive\Container;
|
||||||
use Zend\Expressive\Helper;
|
use Zend\Expressive\Helper;
|
||||||
use Zend\Expressive\Router;
|
use Zend\Expressive\Router;
|
||||||
use Zend\Expressive\Template;
|
use Zend\Expressive\Template;
|
||||||
|
use Zend\Expressive\Twig;
|
||||||
|
use Zend\ServiceManager\Factory\InvokableFactory;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
'services' => [
|
'services' => [
|
||||||
'invokables' => [
|
'invokables' => [
|
||||||
Helper\ServerUrlHelper::class => Helper\ServerUrlHelper::class,
|
Helper\ServerUrlHelper::class,
|
||||||
Router\RouterInterface::class => Router\AuraRouter::class,
|
Router\AuraRouter::class,
|
||||||
],
|
],
|
||||||
'factories' => [
|
'factories' => [
|
||||||
Application::class => Container\ApplicationFactory::class,
|
Application::class => Container\ApplicationFactory::class,
|
||||||
@ -19,11 +25,22 @@ return [
|
|||||||
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
|
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
|
||||||
Helper\ServerUrlMiddleware::class => Helper\ServerUrlMiddlewareFactory::class,
|
Helper\ServerUrlMiddleware::class => Helper\ServerUrlMiddlewareFactory::class,
|
||||||
Helper\UrlHelperMiddleware::class => Helper\UrlHelperMiddlewareFactory::class,
|
Helper\UrlHelperMiddleware::class => Helper\UrlHelperMiddlewareFactory::class,
|
||||||
|
Helper\ServerUrlHelper::class => InvokableFactory::class,
|
||||||
|
Router\RouterInterface::class => InvokableFactory::class,
|
||||||
|
|
||||||
// View
|
// View
|
||||||
'Zend\Expressive\FinalHandler' => Container\TemplatedErrorHandlerFactory::class,
|
'Zend\Expressive\FinalHandler' => Container\TemplatedErrorHandlerFactory::class,
|
||||||
Template\TemplateRendererInterface::class => Zend\Expressive\Twig\TwigRendererFactory::class,
|
Template\TemplateRendererInterface::class => Twig\TwigRendererFactory::class,
|
||||||
|
|
||||||
|
// Services
|
||||||
|
EntityManager::class => EntityManagerFactory::class,
|
||||||
|
GuzzleHttp\Client::class => InvokableFactory::class,
|
||||||
|
UrlShortener::class => AnnotatedFactory::class,
|
||||||
],
|
],
|
||||||
|
'aliases' => [
|
||||||
|
'em' => EntityManager::class,
|
||||||
|
'httpClient' => GuzzleHttp\Client::class,
|
||||||
|
]
|
||||||
],
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
@ -4,11 +4,10 @@ use Zend\ServiceManager\ServiceManager;
|
|||||||
|
|
||||||
chdir(dirname(__DIR__));
|
chdir(dirname(__DIR__));
|
||||||
|
|
||||||
require __DIR__ . '/../vendor/autoload.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
// Build container
|
// Build container
|
||||||
$config = require __DIR__ . '/config.php';
|
$config = require __DIR__ . '/config.php';
|
||||||
$container = new ServiceManager($config['services']);
|
$container = new ServiceManager($config['services']);
|
||||||
$container->setService('config', $config);
|
$container->setService('config', $config);
|
||||||
|
return $container;
|
||||||
return $container->get(Application::class);
|
|
0
data/.gitignore → data/cache/.gitignore
vendored
0
data/.gitignore → data/cache/.gitignore
vendored
2
data/proxies/.gitignore
vendored
Normal file
2
data/proxies/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*
|
||||||
|
!.gitignore
|
@ -1,6 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
use Zend\Expressive\Application;
|
use Zend\Expressive\Application;
|
||||||
|
|
||||||
|
/** @var ContainerInterface $container */
|
||||||
|
$container = include __DIR__ . '/../config/container.php';
|
||||||
/** @var Application $app */
|
/** @var Application $app */
|
||||||
$app = include __DIR__ . '/../config/app.php';
|
$app = $container->get(Application::class);
|
||||||
$app->run();
|
$app->run();
|
||||||
|
43
src/Factory/EntityManagerFactory.php
Normal file
43
src/Factory/EntityManagerFactory.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
namespace Acelaya\UrlShortener\Factory;
|
||||||
|
|
||||||
|
use Doctrine\Common\Cache\ArrayCache;
|
||||||
|
use Doctrine\Common\Cache\Cache;
|
||||||
|
use Doctrine\ORM\EntityManager;
|
||||||
|
use Doctrine\ORM\Tools\Setup;
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
|
use Interop\Container\Exception\ContainerException;
|
||||||
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
||||||
|
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
||||||
|
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||||
|
|
||||||
|
class EntityManagerFactory implements FactoryInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create an object
|
||||||
|
*
|
||||||
|
* @param ContainerInterface $container
|
||||||
|
* @param string $requestedName
|
||||||
|
* @param null|array $options
|
||||||
|
* @return object
|
||||||
|
* @throws ServiceNotFoundException if unable to resolve the service.
|
||||||
|
* @throws ServiceNotCreatedException if an exception is raised when
|
||||||
|
* creating a service.
|
||||||
|
* @throws ContainerException if any other error occurs
|
||||||
|
*/
|
||||||
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
|
||||||
|
{
|
||||||
|
$globalConfig = $container->get('config');
|
||||||
|
$isDevMode = isset($globalConfig['debug']) ? ((bool) $globalConfig['debug']) : false;
|
||||||
|
$cache = $container->has(Cache::class) ? $container->get(Cache::class) : new ArrayCache();
|
||||||
|
$dbConfig = isset($globalConfig['database']) ? $globalConfig['database'] : [];
|
||||||
|
|
||||||
|
return EntityManager::create($dbConfig, Setup::createAnnotationMetadataConfiguration(
|
||||||
|
['src/Entity'],
|
||||||
|
$isDevMode,
|
||||||
|
'data/proxies',
|
||||||
|
$cache,
|
||||||
|
false
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ use Acelaya\UrlShortener\Entity\ShortUrl;
|
|||||||
use Acelaya\UrlShortener\Exception\InvalidShortCodeException;
|
use Acelaya\UrlShortener\Exception\InvalidShortCodeException;
|
||||||
use Acelaya\UrlShortener\Exception\InvalidUrlException;
|
use Acelaya\UrlShortener\Exception\InvalidUrlException;
|
||||||
use Acelaya\UrlShortener\Exception\RuntimeException;
|
use Acelaya\UrlShortener\Exception\RuntimeException;
|
||||||
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\ORM\ORMException;
|
use Doctrine\ORM\ORMException;
|
||||||
use GuzzleHttp\ClientInterface;
|
use GuzzleHttp\ClientInterface;
|
||||||
@ -28,6 +29,14 @@ class UrlShortener implements UrlShortenerInterface
|
|||||||
*/
|
*/
|
||||||
private $chars;
|
private $chars;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UrlShortener constructor.
|
||||||
|
* @param ClientInterface $httpClient
|
||||||
|
* @param EntityManagerInterface $em
|
||||||
|
* @param string $chars
|
||||||
|
*
|
||||||
|
* @Inject({"httpClient", "em"})
|
||||||
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ClientInterface $httpClient,
|
ClientInterface $httpClient,
|
||||||
EntityManagerInterface $em,
|
EntityManagerInterface $em,
|
||||||
|
38
tests/Factory/EntityManagerFactoryTest.php
Normal file
38
tests/Factory/EntityManagerFactoryTest.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
namespace AcelayaTest\UrlShortener\Factory;
|
||||||
|
|
||||||
|
use Acelaya\UrlShortener\Factory\EntityManagerFactory;
|
||||||
|
use Doctrine\ORM\EntityManager;
|
||||||
|
use PHPUnit_Framework_TestCase as TestCase;
|
||||||
|
use Zend\ServiceManager\ServiceManager;
|
||||||
|
|
||||||
|
class EntityManagerFactoryTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var EntityManagerFactory
|
||||||
|
*/
|
||||||
|
protected $factory;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->factory = new EntityManagerFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function serviceIsCreated()
|
||||||
|
{
|
||||||
|
$sm = new ServiceManager(['services' => [
|
||||||
|
'config' => [
|
||||||
|
'debug' => true,
|
||||||
|
'database' => [
|
||||||
|
'driver' => 'pdo_sqlite',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]]);
|
||||||
|
|
||||||
|
$em = $this->factory->__invoke($sm, EntityManager::class);
|
||||||
|
$this->assertInstanceOf(EntityManager::class, $em);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user