1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
<?php
namespace OneOfZero\Json\Helpers;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\IndexedReader;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Annotations\SimpleAnnotationReader;
use Interop\Container\ContainerInterface;
use RuntimeException;
class Environment
{
private static $readerImplementations = [
Reader::class,
AnnotationReader::class,
CachedReader::class,
SimpleAnnotationReader::class,
IndexedReader::class
];
private static $annotationReader;
public static function getVendorPath()
{
$options = [
__DIR__ . '/../../../../../vendor',
__DIR__ . '/../../../../vendor',
__DIR__ . '/../../../vendor',
__DIR__ . '/../../vendor',
$_SERVER['DOCUMENT_ROOT'] . '/vendor',
$_SERVER['DOCUMENT_ROOT'] . '/../vendor',
];
foreach ($options as $option)
{
if (file_exists($option))
{
return $option;
}
}
throw new RuntimeException('Could not determine vendor directory');
}
public static function getAutoloadFile()
{
$autoloader = self::getVendorPath() . '/autoload.php';
if (!file_exists($autoloader))
{
throw new RuntimeException('Could not locate autoload.php');
}
return $autoloader;
}
public static function getAnnotationReader(ContainerInterface $container = null)
{
if ($container !== null)
{
foreach (self::$readerImplementations as $readerClass)
{
if ($container->has($readerClass))
{
return $container->get($readerClass);
}
}
}
if (!self::$annotationReader)
{
AnnotationRegistry::registerLoader([ require(self::getAutoloadFile()), 'loadClass' ]);
self::$annotationReader = new AnnotationReader();
}
return self::$annotationReader;
}
}