Pimcore\Model\Element\Service::createFolderByPath PHP Method

createFolderByPath() public static method

public static createFolderByPath ( $path, array $options = [] ) : Folder | Folder | Folder
$path
$options array
return Pimcore\Model\Asset\Folder | Pimcore\Model\Document\Folder | Pimcore\Model\Object\Folder
    public static function createFolderByPath($path, $options = [])
    {
        $calledClass = get_called_class();
        if ($calledClass == __CLASS__) {
            throw new \Exception("This method must be called from a extended class. e.g Asset\\Service, Object\\Service, Document\\Service");
        }
        $type = str_replace('\\Service', '', $calledClass);
        $type = "\\" . ltrim($type, "\\");
        $folderType = $type . '\\Folder';
        $lastFolder = null;
        $pathsArray = [];
        $parts = explode('/', $path);
        $parts = array_filter($parts, "\\Pimcore\\Model\\Element\\Service::filterNullValues");
        $sanitizedPath = "/";
        foreach ($parts as $part) {
            $sanitizedPath = $sanitizedPath . self::getValidKey($part, $type) . "/";
        }
        if (!($foundElement = $type::getByPath($sanitizedPath))) {
            foreach ($parts as $part) {
                $pathsArray[] = $pathsArray[count($pathsArray) - 1] . '/' . self::getValidKey($part, $type);
            }
            for ($i = 0; $i < count($pathsArray); $i++) {
                $currentPath = $pathsArray[$i];
                if (!$type::getByPath($currentPath) instanceof $type) {
                    $parentFolderPath = $i == 0 ? '/' : $pathsArray[$i - 1];
                    $parentFolder = $type::getByPath($parentFolderPath);
                    $folder = new $folderType();
                    $folder->setParent($parentFolder);
                    if ($parentFolder) {
                        $folder->setParentId($parentFolder->getId());
                    } else {
                        $folder->setParentId(1);
                    }
                    $key = substr($currentPath, strrpos($currentPath, '/') + 1, strlen($currentPath));
                    if (method_exists($folder, 'setKey')) {
                        $folder->setKey($key);
                    }
                    if (method_exists($folder, 'setFilename')) {
                        $folder->setFilename($key);
                    }
                    if (method_exists($folder, 'setType')) {
                        $folder->setType('folder');
                    }
                    $folder->setPath($currentPath);
                    $folder->setUserModification(0);
                    $folder->setUserOwner(1);
                    $folder->setCreationDate(time());
                    $folder->setModificationDate(time());
                    $folder->setValues($options);
                    $folder->save();
                    $lastFolder = $folder;
                }
            }
        } else {
            return $foundElement;
        }
        return $lastFolder;
    }