fkooman\RemoteStorage\DocumentStorage::putDocument PHP Method

putDocument() public method

Store a new document.
public putDocument ( Path $p, $documentContent )
$p Path
    public function putDocument(Path $p, $documentContent)
    {
        $folderTree = $p->getFolderTreeFromUserRoot();
        foreach ($folderTree as $pathItem) {
            $folderPath = $this->baseDir . $pathItem;
            $folderPathAsFile = substr($folderPath, 0, strlen($folderPath) - 1);
            if (file_exists($folderPathAsFile) && is_file($folderPathAsFile)) {
                throw new ConflictException('file already exists in path preventing folder creation');
            }
            if (!file_exists($folderPath)) {
                // create it
                if (false === @mkdir($this->baseDir . $pathItem, 0770)) {
                    throw new DocumentStorageException('unable to create directory');
                }
            }
        }
        $documentPath = $this->baseDir . $p->getPath();
        if (file_exists($documentPath) && is_dir($documentPath)) {
            throw new ConflictException('document path is already a folder');
        }
        if (false === @file_put_contents($documentPath, $documentContent, LOCK_EX)) {
            throw new DocumentStorageException('unable to write document');
        }
        // PHP caches files and doesn't flush on getting file size, so we
        // really have to flush the cache manually, otherwise directory listings
        // potentially give you the wrong information. This only affects the
        // unit tests, as getting a directory listing and putting a file are
        // always separate script executions
        clearstatcache(true, $documentPath);
        return $folderTree;
    }

Usage Example

 public function putDocument(Path $p, $contentType, $documentData, array $ifMatch = null, array $ifNoneMatch = null)
 {
     if (null !== $ifMatch && !in_array($this->md->getVersion($p), $ifMatch)) {
         throw new PreconditionFailedException('version mismatch');
     }
     if (null !== $ifNoneMatch && in_array('*', $ifNoneMatch) && null !== $this->md->getVersion($p)) {
         throw new PreconditionFailedException('document already exists');
     }
     $updatedEntities = $this->d->putDocument($p, $documentData);
     $this->md->updateDocument($p, $contentType);
     foreach ($updatedEntities as $u) {
         $this->md->updateFolder(new Path($u));
     }
 }