Last active
April 12, 2018 09:43
-
-
Save RSully/701d53beab48001969be to your computer and use it in GitHub Desktop.
Wrapper around https://github.com/goetas/xsd2php)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Generator { | |
protected $namespaces = []; | |
protected $outputDirectoriesPSR4 = []; | |
protected $aliasMapTypes = []; | |
protected $inputFiles = []; | |
/* | |
* Methods to setup the data we need to generate | |
*/ | |
/** | |
* @param string $xsdNamespace | |
* @param string $phpNamespace | |
*/ | |
public function addNamespace($xsdNamespace, $phpNamespace) | |
{ | |
$this->namespaces[$xsdNamespace] = $phpNamespace; | |
} | |
/** | |
* @param string $namespace | |
* @param string $directory | |
*/ | |
public function addOutputDirectoryPSR4($namespace, $directory) | |
{ | |
$this->outputDirectoriesPSR4[$namespace] = $directory; | |
} | |
/** | |
* @param string $xsdNamespace | |
* @param string $type | |
* @param string $phpNamespace | |
*/ | |
public function addAliasMapType($xsdNamespace, $type, $phpNamespace) | |
{ | |
$this->aliasMapTypes[] = [$xsdNamespace, $type, $phpNamespace]; | |
} | |
/** | |
* @param string $xsdPath | |
*/ | |
public function addInputFile($xsdPath) | |
{ | |
$this->inputFiles[] = $xsdPath; | |
} | |
/* | |
* Worker methods | |
*/ | |
/** | |
* Setup the folder structure | |
*/ | |
protected function setup() | |
{ | |
foreach ($this->outputDirectoriesPSR4 as $dir) | |
{ | |
if (!is_dir($dir)) mkdir($dir); | |
} | |
} | |
public function generate() | |
{ | |
$this->setup(); | |
$namingStrategy = new \Goetas\Xsd\XsdToPhp\Naming\LongNamingStrategy(); | |
$phpCreator = new \Goetas\Xsd\XsdToPhp\Php\PhpConverter($namingStrategy); | |
$jmsCreator = new \Goetas\Xsd\XsdToPhp\Jms\YamlConverter($namingStrategy); | |
foreach ($this->namespaces as $xsd => $php) | |
{ | |
$phpCreator->addNamespace($xsd, $php); | |
$jmsCreator->addNamespace($xsd, $php); | |
} | |
foreach ($this->aliasMapTypes as $aliasMapType) | |
{ | |
$phpCreator->addAliasMapType($aliasMapType[0], $aliasMapType[1], $aliasMapType[2]); | |
$jmsCreator->addAliasMapType($aliasMapType[0], $aliasMapType[1], $aliasMapType[2]); | |
} | |
$schemas = []; | |
$schemaReader = new \Goetas\XML\XSDReader\SchemaReader(); | |
foreach ($this->inputFiles as $file) | |
{ | |
$schemas[] = $schemaReader->readFile($file); | |
} | |
$classesPhp = $phpCreator->convert($schemas); | |
$classesYaml = $jmsCreator->convert($schemas); | |
$phpGenerator = new \Goetas\Xsd\XsdToPhp\Php\ClassGenerator(); | |
$yamlGenerator = new \Symfony\Component\Yaml\Dumper(); | |
$pathGeneratorPhp = new \Goetas\Xsd\XsdToPhp\Php\PathGenerator\Psr4PathGenerator($this->outputDirectoriesPSR4); | |
$pathGeneratorYaml = new \Goetas\Xsd\XsdToPhp\Jms\PathGenerator\Psr4PathGenerator($this->outputDirectoriesPSR4); | |
foreach ($classesPhp as $class) | |
{ | |
/** @var string $path */ | |
$path = $pathGeneratorPhp->getPath($class); | |
$fileGen = new \Zend\Code\Generator\FileGenerator(); | |
$fileGen->setFilename($path); | |
$classGen = new \Zend\Code\Generator\ClassGenerator(); | |
if ($phpGenerator->generate($classGen, $class)) | |
{ | |
$fileGen->setClass($classGen); | |
$fileGen->write(); | |
} | |
} | |
foreach ($classesYaml as $class) | |
{ | |
/** @var string $path */ | |
$path = $pathGeneratorYaml->getPath($class); | |
$yaml = $yamlGenerator->dump($class, 10000); | |
file_put_contents($path, $yaml); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment