Doctrine\ODM\MongoDB\Tools\DocumentGenerator::writeDocumentClass PHP Method

writeDocumentClass() public method

Generated and write document class to disk for the given ClassMetadataInfo instance
public writeDocumentClass ( ClassMetadataInfo $metadata, string $outputDirectory ) : void
$metadata Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo
$outputDirectory string
return void
    public function writeDocumentClass(ClassMetadataInfo $metadata, $outputDirectory)
    {
        $path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->extension;
        $dir = dirname($path);
        if (!is_dir($dir)) {
            mkdir($dir, 0775, true);
        }
        $this->isNew = !file_exists($path) || file_exists($path) && $this->regenerateDocumentIfExists;
        if (!$this->isNew) {
            $this->parseTokensInDocumentFile($path);
        }
        if ($this->backupExisting && file_exists($path)) {
            $backupPath = dirname($path) . DIRECTORY_SEPARATOR . basename($path) . '~';
            if (!copy($path, $backupPath)) {
                throw new \RuntimeException('Attempt to backup overwritten document file but copy operation failed.');
            }
        }
        // If document doesn't exist or we're re-generating the documents entirely
        if ($this->isNew) {
            file_put_contents($path, $this->generateDocumentClass($metadata));
            // If document exists and we're allowed to update the document class
        } elseif (!$this->isNew && $this->updateDocumentIfExists) {
            file_put_contents($path, $this->generateUpdatedDocumentClass($metadata, $path));
        }
        chmod($path, 0664);
    }