コード例 #1
0
ファイル: CompilerTest.php プロジェクト: gdbots/pbj
 public function testConstruct()
 {
     $compiler = new Compiler();
     $schemaIds = ['pbj:acme:blog:entity:article:1-0-0', 'pbj:acme:blog:entity:article:1-0-1', 'pbj:acme:blog:entity:comment:1-0-0', 'pbj:acme:blog:mixin:has-comments:1-0-0', 'pbj:acme:core:mixin:article:1-0-0'];
     $found = preg_grep('/pbj:acme:*/', array_keys(SchemaStore::getSchemas()));
     $this->assertCount(5, $found);
     $this->assertEquals($schemaIds, $found);
 }
コード例 #2
0
ファイル: SchemaValidator.php プロジェクト: gdbots/pbj
 /**
  * Validates a single schema against previous version.
  *
  * @param SchemaDescriptor $schema
  *
  * @throws \RuntimeException
  */
 public function validate(SchemaDescriptor $schema)
 {
     if ($prevSchema = SchemaStore::getPreviousSchema($schema->getId())) {
         if (!$prevSchema instanceof SchemaDescriptor) {
             throw new \RuntimeException(sprintf('Un-parsed schema "%s".', $prevSchema['id']));
         }
         /** @var \Gdbots\Pbjc\Validator\Constraint $constraint */
         foreach ($this->constraints as $constraint) {
             $constraint->validate($prevSchema, $schema);
         }
     }
     $constraint = new Constraint\SchemaDependencyVersion();
     $constraint->validate($schema, $schema);
     $constraint = new Constraint\SchemaInheritanceFields();
     $constraint->validate($schema, $schema);
     $constraint = new Constraint\FieldValidEnumValue();
     $constraint->validate($schema, $schema);
 }
コード例 #3
0
ファイル: pbj-schema-stores.php プロジェクト: wb-eme/schemas
<?php

/**
 * DO NOT EDIT THIS FILE as it will be overwritten by Composer as part of
 * the installation/update process.
 *
 * Registers all directories from all required packages which are of
 * of the type "pbj-schema-store".
 *
 * This file has been auto-generated by the Pbj Compiler.
 */
$vendorDir = realpath(__DIR__ . '/vendor');
\Gdbots\Pbjc\SchemaStore::addDirs([$vendorDir . '/gdbots/schemas/schemas/', __DIR__ . '/schemas/']);
コード例 #4
0
ファイル: PhpGenerator.php プロジェクト: gdbots/pbj
 /**
  * {@inheritdoc}
  */
 public function generateManifest(array $schemas)
 {
     $messages = [];
     if (!($filename = $this->compileOptions->getManifest())) {
         return;
     }
     // extract previous schemas
     if (file_exists($filename)) {
         $content = file_get_contents($filename);
         if (preg_match_all('/\'([a-z0-9-]+:[a-z0-9\\.-]+:[a-z0-9-]+?:[a-z0-9-]+(:v[0-9]+)?)\' => \'(.*)\'/', $content, $matches) !== false) {
             foreach ($matches[1] as $key => $value) {
                 $messages[$value] = $matches[3][$key];
             }
         }
     }
     // merge with selected schemas (only non-mixin schema's)
     /** @var SchemaDescriptor $schema */
     foreach ($schemas as $schema) {
         if ($schema->isMixinSchema()) {
             continue;
         }
         if (!array_key_exists($schema->getId()->getCurie(), $messages)) {
             $messages[$schema->getId()->getCurie()] = sprintf('%s\\%sV%d', $schema->getLanguage('php')->get('namespace'), StringUtils::toCamelFromSlug($schema->getId()->getMessage()), $schema->getId()->getVersion()->getMajor());
         }
         if (SchemaStore::hasOtherSchemaMajorRev($schema->getId())) {
             /** @var SchemaDescriptor $s */
             foreach (SchemaStore::getOtherSchemaMajorRev($schema->getId()) as $s) {
                 if (!array_key_exists($s->getId()->getCurieWithMajorRev(), $messages)) {
                     $messages[$s->getId()->getCurieWithMajorRev()] = sprintf('%s\\%sV%d', $s->getLanguage('php')->get('namespace'), StringUtils::toCamelFromSlug($s->getId()->getMessage()), $s->getId()->getVersion()->getMajor());
                 }
             }
         }
     }
     // delete invalid schemas
     foreach ($messages as $key => $value) {
         if (!SchemaStore::getSchemaById($key, true)) {
             unset($messages[$key]);
         }
     }
     $response = new GeneratorResponse();
     $response->addFile($this->renderFile('manifest.twig', $filename, ['messages' => $messages]));
     return $response;
 }
コード例 #5
0
ファイル: SchemaStoreTest.php プロジェクト: gdbots/pbj
 public function testAddEnum()
 {
     $enum = new EnumDescriptor('vendor:package:number', 'int', [1, 2, 3, 4]);
     SchemaStore::addEnum($enum->getId(), $enum);
     $this->assertEquals(SchemaStore::getEnumById('vendor:package:number'), $enum);
 }
コード例 #6
0
ファイル: bootstrap.php プロジェクト: gdbots/pbj
<?php

error_reporting(-1);
date_default_timezone_set('UTC');
// Ensure that composer has installed all dependencies
if (!file_exists(dirname(__DIR__) . '/composer.lock')) {
    die("Dependencies must be installed using composer:\n\nphp composer.phar install\n\n" . "See http://getcomposer.org for help with installing composer\n");
}
// Include the composer autoloader
$loader = (require dirname(__DIR__) . '/vendor/autoload.php');
use Gdbots\Pbjc\SchemaStore;
SchemaStore::addDir(__DIR__ . '/Fixtures/schemas');
コード例 #7
0
ファイル: SchemaParser.php プロジェクト: gdbots/pbj
 /**
  * @param SchemaId $schemaId
  * @param string   $curieWithMajorRev
  *
  * @return SchemaDescriptor|null
  *
  * @throws \InvalidArgumentException
  * @throws MissingSchema
  */
 private function getMixin(SchemaId $schemaId, $curieWithMajorRev)
 {
     if ($curieWithMajorRev == $schemaId->getCurieWithMajorRev()) {
         throw new \InvalidArgumentException(sprintf('Cannot add yourself "%s" as to mixins.', $schemaId->toString()));
     }
     if (!($schema = SchemaStore::getSchemaById($curieWithMajorRev, true))) {
         throw new MissingSchema($curieWithMajorRev);
     }
     return $schema;
 }
コード例 #8
0
ファイル: pbj-schema-stores.php プロジェクト: gdbots/schemas
<?php

/**
 * DO NOT EDIT THIS FILE as it will be overwritten by Composer as part of
 * the installation/update process.
 *
 * Registers all directories from all required packages which are of
 * of the type "pbj-schema-store".
 *
 * This file has been auto-generated by the Pbj Compiler.
 */
$vendorDir = realpath(__DIR__ . '/vendor');
\Gdbots\Pbjc\SchemaStore::addDirs([__DIR__ . '/schemas/']);
コード例 #9
0
ファイル: SchemaExtension.php プロジェクト: gdbots/pbj
 /**
  * @param SchemaDescriptor $schema
  *
  * @return array
  */
 public function getAllVersions(SchemaDescriptor $schema)
 {
     return SchemaStore::getAllSchemaVersions($schema->getId());
 }
コード例 #10
0
ファイル: Compiler.php プロジェクト: gdbots/pbj
 /**
  * Generates and writes files for each schema.
  *
  * @param string         $language
  * @param CompileOptions $options
  *
  * @throws \InvalidArgumentException
  */
 public function run($language, CompileOptions $options)
 {
     $namespaces = $options->getNamespaces();
     if (!$namespaces || count($namespaces) === 0) {
         throw new \InvalidArgumentException('Missing "namespaces" options.');
     }
     if (!is_array($namespaces)) {
         $namespaces = [$namespaces];
     }
     foreach ($namespaces as $namespace) {
         if (!preg_match('/^([a-z0-9-]+):([a-z0-9\\.-]+)$/', $namespace)) {
             throw new \InvalidArgumentException(sprintf('The namespace "%s" must follow "vendor:package" format.', $namespace));
         }
     }
     if (!$options->getOutput()) {
         throw new \InvalidArgumentException('Missing "output" directory options.');
     }
     $class = sprintf('\\Gdbots\\Pbjc\\Generator\\%sGenerator', StringUtils::toCamelFromSlug($language));
     /** @var \Gdbots\Pbjc\Generator\Generator $generator */
     $generator = new $class($options);
     $outputFiles = [];
     /** @var EnumDescriptor $enum */
     foreach (SchemaStore::getEnums() as $enum) {
         if (!$options->getIncludeAll() && !in_array($enum->getId()->getNamespace(), $namespaces)) {
             continue;
         }
         /** @var $response \Gdbots\Pbjc\Generator\GeneratorResponse */
         if ($response = $generator->generateEnum($enum)) {
             $outputFiles = array_merge($outputFiles, $response->getFiles());
         }
     }
     /** @var SchemaDescriptor $schema */
     foreach (SchemaStore::getSchemas() as $schema) {
         if (!$options->getIncludeAll() && !in_array($schema->getId()->getNamespace(), $namespaces)) {
             continue;
         }
         /** @var $response \Gdbots\Pbjc\Generator\GeneratorResponse */
         if ($response = $generator->generateSchema($schema)) {
             $outputFiles = array_merge($outputFiles, $response->getFiles());
         }
     }
     if ($options->getManifest()) {
         /** @var $response \Gdbots\Pbjc\Generator\GeneratorResponse */
         if ($response = $generator->generateManifest(SchemaStore::getSchemas())) {
             $outputFiles = array_merge($outputFiles, $response->getFiles());
         }
     }
     if ($callback = $options->getCallback()) {
         foreach ($outputFiles as $outputFile) {
             call_user_func($callback, $outputFile);
         }
     }
 }