Exemplo n.º 1
0
 /**
  * @param SchemaDescriptor $schema
  * @param string           $filename
  * @param string           $directory
  * @param bool             $isLatest
  *
  * @return string
  */
 protected function getSchemaTarget(SchemaDescriptor $schema, $filename, $directory = null, $isLatest = false)
 {
     $filename = str_replace(['{vendor}', '{package}', '{category}', '{version}', '{major}'], [$schema->getId()->getVendor(), $schema->getId()->getPackage(), $schema->getId()->getCategory(), $schema->getId()->getVersion()->toString(), $schema->getId()->getVersion()->getMajor()], $filename);
     if ($directory === null) {
         $directory = sprintf('%s/%s/%s', StringUtils::toCamelFromSlug($schema->getId()->getVendor()), StringUtils::toCamelFromSlug($schema->getId()->getPackage()), StringUtils::toCamelFromSlug($schema->getId()->getCategory()));
     }
     if ($directory) {
         $directory .= '/';
     }
     return sprintf('%s/%s%s%s', $this->compileOptions->getOutput(), $directory, $filename, $this->extension);
 }
Exemplo n.º 2
0
 /**
  * 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);
         }
     }
 }