Added dotenv component to define env vars in local file

This commit is contained in:
Alejandro Celaya 2016-04-30 17:21:35 +02:00
parent 03298fc448
commit a60a6ccc4d
8 changed files with 69 additions and 22 deletions

7
.env.dist Normal file
View File

@ -0,0 +1,7 @@
# Application
APP_ENV=
# Database
DB_USER=
DB_PASSWORD=
DB_NAME=

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
build build
composer.lock composer.lock
vendor/ vendor/
.env

View File

@ -27,7 +27,8 @@
"squizlabs/php_codesniffer": "^2.3", "squizlabs/php_codesniffer": "^2.3",
"roave/security-advisories": "dev-master", "roave/security-advisories": "dev-master",
"filp/whoops": "^2.0", "filp/whoops": "^2.0",
"symfony/var-dumper": "^3.0" "symfony/var-dumper": "^3.0",
"vlucas/phpdotenv": "^2.2"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@ -0,0 +1,15 @@
<?php
return [
'database' => [
'driver' => 'pdo_mysql',
'user' => getenv('DB_USER'),
'password' => getenv('DB_PASSWORD'),
'dbname' => getenv('DB_NAME') ?: 'acelaya_url_shortener',
'charset' => 'utf8',
'driverOptions' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
],
],
];

View File

@ -1,15 +0,0 @@
<?php
return [
'database' => [
'driver' => 'pdo_mysql',
'user' => '',
'password' => '',
'dbname' => '',
'charset' => 'utf-8',
'driverOptions' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
],
]
];

View File

@ -1,7 +1,9 @@
<?php <?php
use Acelaya\UrlShortener\Factory\CacheFactory;
use Acelaya\UrlShortener\Factory\EntityManagerFactory; use Acelaya\UrlShortener\Factory\EntityManagerFactory;
use Acelaya\UrlShortener\Service\UrlShortener; use Acelaya\UrlShortener\Service\UrlShortener;
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory; use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
use Doctrine\Common\Cache\Cache;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Zend\Expressive\Application; use Zend\Expressive\Application;
use Zend\Expressive\Container; use Zend\Expressive\Container;
@ -14,19 +16,16 @@ use Zend\ServiceManager\Factory\InvokableFactory;
return [ return [
'services' => [ 'services' => [
'invokables' => [
Helper\ServerUrlHelper::class,
Router\AuraRouter::class,
],
'factories' => [ 'factories' => [
Application::class => Container\ApplicationFactory::class, Application::class => Container\ApplicationFactory::class,
// Url helpers // Routes
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, Helper\ServerUrlHelper::class => InvokableFactory::class,
Router\RouterInterface::class => InvokableFactory::class, Router\RouterInterface::class => InvokableFactory::class,
Router\AuraRouter::class => InvokableFactory::class,
// View // View
'Zend\Expressive\FinalHandler' => Container\TemplatedErrorHandlerFactory::class, 'Zend\Expressive\FinalHandler' => Container\TemplatedErrorHandlerFactory::class,
@ -36,6 +35,7 @@ return [
EntityManager::class => EntityManagerFactory::class, EntityManager::class => EntityManagerFactory::class,
GuzzleHttp\Client::class => InvokableFactory::class, GuzzleHttp\Client::class => InvokableFactory::class,
UrlShortener::class => AnnotatedFactory::class, UrlShortener::class => AnnotatedFactory::class,
Cache::class => CacheFactory::class,
], ],
'aliases' => [ 'aliases' => [
'em' => EntityManager::class, 'em' => EntityManager::class,

View File

@ -1,11 +1,19 @@
<?php <?php
use Zend\Expressive\Application; use Dotenv\Dotenv;
use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\ServiceManager;
chdir(dirname(__DIR__)); chdir(dirname(__DIR__));
require 'vendor/autoload.php'; require 'vendor/autoload.php';
// If the Dotenv class exists, load env vars and enable errors
if (class_exists(Dotenv::class)) {
error_reporting(E_ALL);
ini_set('display_errors', 1);
$dotenv = new Dotenv(__DIR__ . '/..');
$dotenv->load();
}
// Build container // Build container
$config = require __DIR__ . '/config.php'; $config = require __DIR__ . '/config.php';
$container = new ServiceManager($config['services']); $container = new ServiceManager($config['services']);

View File

@ -0,0 +1,30 @@
<?php
namespace Acelaya\UrlShortener\Factory;
use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\ArrayCache;
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 CacheFactory 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)
{
return getenv('APP_ENV') === 'pro' ? new ApcuCache() : new ArrayCache();
}
}