/**
* @param \Symfony\Component\HttpKernel\Bundle\BundleInterface $bundle
* @param string $document
* @param array $fields
* @param Boolean $withRepository
* @throws \RuntimeException
*/
public function generate(BundleInterface $bundle, $document, array $fields, $withRepository)
{
$config = $this->documentManager->getConfiguration();
$config->addDocumentNamespace($bundle->getName(), $bundle->getNamespace() . '\\Document');
$documentClass = $config->getDocumentNamespace($bundle->getName()) . '\\' . $document;
$documentPath = $bundle->getPath() . '/Document/' . str_replace('\\', '/', $document) . '.php';
if (file_exists($documentPath)) {
throw new \RuntimeException(sprintf('Document "%s" already exists.', $documentClass));
}
$class = new ClassMetadataInfo($documentClass);
if ($withRepository) {
$class->setCustomRepositoryClass($documentClass . 'Repository');
}
$class->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
$class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
foreach ($fields as $field) {
$class->mapField($field);
}
$documentGenerator = $this->getDocumentGenerator();
$documentCode = $documentGenerator->generateDocumentClass($class);
$this->filesystem->mkdir(dirname($documentPath));
file_put_contents($documentPath, rtrim($documentCode) . PHP_EOL, LOCK_EX);
if ($withRepository) {
$path = $bundle->getPath() . str_repeat('/..', substr_count(get_class($bundle), '\\'));
$this->getRepositoryGenerator()->writeDocumentRepositoryClass($class->customRepositoryClassName, $path);
}
}