/**
  * При выполнении каждого юнит-теста происходят следующие действия:
  *
  *  1. Стартует транзакция
  *  2. Применяются фикстуры
  *  3. Инжектится SchemaService в SchemaParams
  *
  * @throws \DI\NotFoundException
  */
 protected function setUp()
 {
     $transactionService = $this->container()->get(TransactionService::class);
     /** @var TransactionService $transactionService */
     $transactionService->beginTransaction();
     $app = $this->app();
     $em = $this->container()->get(EntityManager::class);
     array_map(function (Fixture $fixture) use($app, $em) {
         $this->upFixture($fixture);
     }, $this->getFixtures());
     SchemaParams::injectSchemaService($app->getContainer()->get(SchemaService::class));
 }
 public function __invoke(AppBuilder $appBuilder)
 {
     SchemaParams::injectSchemaService($appBuilder->getContainer()->get(SchemaService::class));
 }
Esempio n. 3
0
class SchemaService
{
    /** @var SchemaCache */
    private $cache;
    /** @var BundleService */
    private $bundleService;
    /**
     * ZEA2\Platform\Bundles\REST\Service\SchemaService constructor.
     * @param BundleService $bundleService
     */
    public function __construct(BundleService $bundleService)
    {
        $this->bundleService = $bundleService;
        $this->cache = new class implements SchemaCache
        {
            /** @var JSONSchema[] */
            private $schemas = [];
            public function generateCacheToken(string $bundle, string $path)
            {
                return sprintf('bundle_%s_path_%s', $bundle, $path);
            }
            public function putSchemaToCache(string $token, JSONSchema $schema)
            {
                $this->schemas[$token] = $schema;
            }
            public function hasCachedSchema(string $token) : bool
            {
                return isset($this->schemas[$token]);
            }
            public function getCachedSchema(string $token) : JSONSchema
            {
                if (!$this->hasCachedSchema($token)) {
                    throw new CachedSchemaNotFound(sprintf('No schema available with token `%s`', $token));
                }
                return $this->schemas[$token];
            }
            public function reset()
            {
                $this->schemas = [];
            }
        };
        SchemaParams::injectSchemaService($this);
    }
    public function getSchema($bundleName, $path)
    {
        $cache = $this->cache;
        $token = $cache->generateCacheToken($bundleName, $path);
        if (!$cache->hasCachedSchema($token)) {
            $cache->putSchemaToCache($token, $this->fetchSchema($bundleName, $path));
        }
        return $cache->getCachedSchema($token);
    }
    private function fetchSchema($bundleName, $path) : JSONSchema
    {
        $bundle = $this->bundleService->getBundleByName($bundleName);
        if (!$bundle->hasAPIDocsDir()) {
            throw new NoAPIDocsAvailableException(sprintf('No API docs available for bundle `%s`', $bundle->getName()));
        }
        $apiFile = "{$bundle->getAPIDocsDir()}/{$path}";
        if (!file_exists($apiFile)) {
            throw new SchemaFileNotExists(sprintf('Schema file `%s` not found', $apiFile));
        }
        $yaml = Yaml::parse(file_get_contents($apiFile));
        $yaml = array_pop($yaml);
        $yaml = json_decode(json_encode($yaml));
        return new JSONSchema($yaml);
    }
}