codysnider /
urss
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace RssApp; |
||
| 6 | |||
| 7 | use Doctrine\Common\Annotations\AnnotationException; |
||
| 8 | use Doctrine\Common\Annotations\AnnotationReader; |
||
| 9 | use Doctrine\Common\Annotations\AnnotationRegistry; |
||
| 10 | use Doctrine\Common\Cache\ArrayCache; |
||
| 11 | use Doctrine\Common\Cache\RedisCache; |
||
| 12 | use Doctrine\Common\Proxy\AbstractProxyFactory; |
||
| 13 | use Doctrine\DBAL\DBALException; |
||
| 14 | use Doctrine\DBAL\DriverManager; |
||
| 15 | use Doctrine\ORM\Cache\DefaultCacheFactory; |
||
| 16 | use Doctrine\ORM\Cache\RegionsConfiguration; |
||
| 17 | use Doctrine\ORM\EntityManager; |
||
| 18 | use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
||
| 19 | use Doctrine\ORM\Mapping\UnderscoreNamingStrategy; |
||
| 20 | use Doctrine\ORM\ORMException; |
||
| 21 | use Doctrine\ORM\Tools\Setup; |
||
| 22 | use Exception; |
||
| 23 | use JMS\Serializer\SerializerBuilder; |
||
| 24 | use Redis; |
||
| 25 | use RssApp\Components\Registry; |
||
| 26 | use RssApp\Components\Twig\Filters; |
||
| 27 | use RssApp\Components\Twig\Functions; |
||
| 28 | use RssApp\Model\Extension\BacktickQuoteStrategy; |
||
| 29 | use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader; |
||
| 30 | use Symfony\Component\Config\FileLocator; |
||
| 31 | use Symfony\Component\HttpFoundation\Request; |
||
| 32 | use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader; |
||
| 33 | use Symfony\Component\Routing\RequestContext; |
||
| 34 | use Symfony\Component\Routing\Router; |
||
| 35 | use Twig\Environment; |
||
| 36 | use Twig\Loader\FilesystemLoader; |
||
| 37 | use Zend\Diactoros\ServerRequestFactory; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @internal Intended to be a final abstract (or static) class (https://wiki.php.net/rfc/abstract_final_class) |
||
| 41 | */ |
||
| 42 | abstract class Bootstrap |
||
| 43 | { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Runs the class methods in order of declaration |
||
| 47 | * |
||
| 48 | * @param array $omitMethods |
||
| 49 | * |
||
| 50 | * @throws Exception |
||
| 51 | */ |
||
| 52 | public static function initialize($omitMethods = []): void |
||
| 53 | { |
||
| 54 | $methods = get_class_methods(self::class); |
||
| 55 | $key = array_search('initialize', $methods); |
||
| 56 | if ($key !== false) { |
||
| 57 | unset($methods[$key]); |
||
| 58 | } |
||
| 59 | foreach ($omitMethods as $omitMethod) { |
||
| 60 | $omitKey = array_search($omitMethod, $methods); |
||
| 61 | if ($omitKey !== false) { |
||
| 62 | unset($methods[$key]); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | foreach ($methods as $method) { |
||
| 66 | if (self::{$method}() === false) { |
||
| 67 | throw new Exception($method.' failed to initialize'); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | protected static function redis(): bool |
||
| 73 | { |
||
| 74 | if (!empty(getenv('CACHE_HOST'))) { |
||
| 75 | $redis = new Redis(); |
||
| 76 | $redis->pconnect(getenv('CACHE_HOST'), (int) getenv('CACHE_PORT')); |
||
| 77 | $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); |
||
| 78 | $redis->select(1); |
||
| 79 | Registry::set('redis', $redis); |
||
| 80 | } else { |
||
| 81 | Registry::set('redis', false); |
||
| 82 | } |
||
| 83 | return true; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @throws AnnotationException |
||
| 88 | * @throws ORMException |
||
| 89 | * @throws \Doctrine\DBAL\DBALException |
||
| 90 | */ |
||
| 91 | protected static function orm(): bool |
||
| 92 | { |
||
| 93 | $config = Setup::createAnnotationMetadataConfiguration([BASEPATH.DS.'src'], true); |
||
| 94 | |||
| 95 | $config->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS); |
||
| 96 | $config->setProxyDir(BASEPATH.DS.'tmp'.DS.'proxies'); |
||
| 97 | $config->addEntityNamespace('RssApp', 'RssApp\Model'); |
||
| 98 | $config->setQuoteStrategy(new BacktickQuoteStrategy()); |
||
| 99 | $config->setNamingStrategy(new UnderscoreNamingStrategy()); |
||
| 100 | |||
| 101 | if (Registry::get('redis')) { |
||
| 102 | $ormCache = new RedisCache(); |
||
| 103 | $ormCache->setRedis(Registry::get('redis')); |
||
| 104 | } else { |
||
| 105 | $ormCache = new ArrayCache(); |
||
| 106 | } |
||
| 107 | $config->setSecondLevelCacheEnabled(); |
||
| 108 | $config->getSecondLevelCacheConfiguration() |
||
| 109 | ->setCacheFactory(new DefaultCacheFactory(new RegionsConfiguration(), $ormCache)); |
||
| 110 | $config->setMetadataCacheImpl($ormCache); |
||
| 111 | $config->setQueryCacheImpl($ormCache); |
||
| 112 | $config->setResultCacheImpl($ormCache); |
||
| 113 | $reader = new AnnotationReader(); |
||
| 114 | $driver = new AnnotationDriver($reader, [BASEPATH.DS.'src']); |
||
| 115 | $config->setMetadataDriverImpl($driver); |
||
| 116 | |||
| 117 | $slaves = []; |
||
| 118 | if (getenv('DB_SLAVES') !== false) { |
||
| 119 | $slaveHosts = explode(',', getenv('DB_SLAVES')); |
||
| 120 | foreach ($slaveHosts as $host) { |
||
| 121 | $slaves[] = [ |
||
| 122 | 'user' => getenv('DB_USER'), |
||
| 123 | 'password' => getenv('DB_PASS'), |
||
| 124 | 'host' => $host, |
||
| 125 | 'dbname' => getenv('DB_NAME'), |
||
| 126 | ]; |
||
| 127 | } |
||
| 128 | } else { |
||
| 129 | $slaves[] = [ |
||
| 130 | 'user' => getenv('DB_USER'), |
||
| 131 | 'password' => getenv('DB_PASS'), |
||
| 132 | 'host' => getenv('DB_HOST'), |
||
| 133 | 'dbname' => getenv('DB_NAME'), |
||
| 134 | ]; |
||
| 135 | } |
||
| 136 | $connection = DriverManager::getConnection([ |
||
| 137 | 'wrapperClass' => 'Doctrine\DBAL\Connections\MasterSlaveConnection', |
||
| 138 | 'driver' => 'pdo_mysql', |
||
| 139 | 'master' => [ |
||
| 140 | 'user' => getenv('DB_USER'), |
||
| 141 | 'password' => getenv('DB_PASS'), |
||
| 142 | 'host' => getenv('DB_HOST'), |
||
| 143 | 'dbname' => getenv('DB_NAME'), |
||
| 144 | ], |
||
| 145 | 'slaves' => $slaves, |
||
| 146 | ]); |
||
| 147 | $entityManager = EntityManager::create($connection, $config); |
||
| 148 | Registry::set('em', $entityManager); |
||
| 149 | |||
| 150 | try { |
||
| 151 | $dbPlatform = $entityManager->getConnection()->getDatabasePlatform(); |
||
| 152 | $dbPlatform->registerDoctrineTypeMapping('enum', 'string'); |
||
| 153 | $dbPlatform->registerDoctrineTypeMapping('bit', 'boolean'); |
||
| 154 | } catch (DBALException $e) { |
||
| 155 | echo $e->getMessage(); |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | AnnotationRegistry::registerAutoloadNamespace('JMS\Serializer\Annotation', BASEPATH.DS.'external/jms/serializer/src'); |
||
|
0 ignored issues
–
show
|
|||
| 159 | Registry::set('serializer', SerializerBuilder::create()->addMetadataDir(BASEPATH.DS.'src')->build()); |
||
| 160 | return true; |
||
| 161 | } |
||
| 162 | |||
| 163 | protected static function twig(): bool |
||
| 164 | { |
||
| 165 | $cache = false; |
||
| 166 | if (APPLICATION_ENV !== 'dev') { |
||
| 167 | $cache = BASEPATH.DS.'tmp'.DS.'view-cache'; |
||
| 168 | } |
||
| 169 | $loader = new FilesystemLoader(BASEPATH.DS.'views'); |
||
| 170 | $twig = new Environment($loader, ['cache' => $cache]); |
||
| 171 | |||
| 172 | $twig->addGlobal('basePath', BASEPATH); |
||
| 173 | $twig->addGlobal('applicationEnv', APPLICATION_ENV); |
||
| 174 | |||
| 175 | foreach (Filters::all() as $filter) { |
||
| 176 | $twig->addFilter($filter); |
||
| 177 | } |
||
| 178 | foreach (Functions::all() as $function) { |
||
| 179 | $twig->addFunction($function); |
||
| 180 | } |
||
| 181 | |||
| 182 | Registry::set('twig', $twig); |
||
| 183 | return true; |
||
| 184 | } |
||
| 185 | } |
||
| 186 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.