Joli\Jane\Jane::generate PHP Method

generate() public method

Generate code.
public generate ( $schemaFilePath, $name, $namespace, $directory ) : array
$schemaFilePath
$name
$namespace
$directory
return array
    public function generate($schemaFilePath, $name, $namespace, $directory)
    {
        $context = $this->createContext($schemaFilePath, $name, $namespace, $directory);
        if (!file_exists($directory . DIRECTORY_SEPARATOR . 'Model')) {
            mkdir($directory . DIRECTORY_SEPARATOR . 'Model', 0755, true);
        }
        if (!file_exists($directory . DIRECTORY_SEPARATOR . 'Normalizer')) {
            mkdir($directory . DIRECTORY_SEPARATOR . 'Normalizer', 0755, true);
        }
        $prettyPrinter = new Standard();
        $modelFiles = $this->modelGenerator->generate($context->getRootReference(), $name, $context);
        $normalizerFiles = $this->normalizerGenerator->generate($context->getRootReference(), $name, $context);
        $generated = [];
        foreach ($modelFiles as $file) {
            $generated[] = $file->getFilename();
            file_put_contents($file->getFilename(), $prettyPrinter->prettyPrintFile([$file->getNode()]));
        }
        foreach ($normalizerFiles as $file) {
            $generated[] = $file->getFilename();
            file_put_contents($file->getFilename(), $prettyPrinter->prettyPrintFile([$file->getNode()]));
        }
        $this->fix($directory);
        return $generated;
    }

Usage Example

Example #1
0
 /**
  * Unique test with ~70% coverage, library generated from json schema must be the same as the library used
  */
 public function testLibrary()
 {
     $this->jane->generate(__DIR__ . '/data/json-schema.json', 'JsonSchema', 'Joli\\Jane', __DIR__ . "/generated");
     $this->assertTrue(file_exists(__DIR__ . "/generated/Model/JsonSchema.php"));
     $this->assertTrue(file_exists(__DIR__ . "/generated/Normalizer/JsonSchemaNormalizer.php"));
     $this->assertTrue(file_exists(__DIR__ . "/generated/Normalizer/NormalizerFactory.php"));
     $this->assertEquals(file_get_contents(__DIR__ . "/../src/Model/JsonSchema.php"), file_get_contents(__DIR__ . "/generated/Model/JsonSchema.php"));
     $this->assertEquals(file_get_contents(__DIR__ . "/../src/Normalizer/JsonSchemaNormalizer.php"), file_get_contents(__DIR__ . "/generated/Normalizer/JsonSchemaNormalizer.php"));
     $this->assertEquals(file_get_contents(__DIR__ . "/../src/Normalizer/NormalizerFactory.php"), file_get_contents(__DIR__ . "/generated/Normalizer/NormalizerFactory.php"));
 }