Creates whole directory structure.
Structure example: ['dir' => ['subdir' => ['file' => 'content']]].

@param array $structure

@return string Path to root directory
4331
public function createStructure($structure)
    {
        vfsStream::create($this->prepareStructure($structure), $this->getRoot());

        return $this->getRootPath();
    }
889409
public static function getStructurePartData($structure, $part)
    {
        $part_a = self::getStructurePartArray($structure, $part);
        $data   = array();

        if (empty($part_a)) {
            return $data;
        }

        // content-type
        if (is_array($part_a[0])) {
            $data['type'] = 'multipart';
        }
        else {
            $data['type']     = strtolower($part_a[0]);
            $data['encoding'] = strtolower($part_a[5]);

            // charset
            if (is_array($part_a[2])) {
               foreach ($part_a[2] as $key => $val) {
                    if (strcasecmp($val, 'charset') == 0) {
                        $data['charset'] = $part_a[2][$key+1];
                        break;
                    }
                }
            }
        }

        // size
        $data['size'] = intval($part_a[6]);

        return $data;
    }
44526
private function createDirectoryStructure() {
        foreach ($this->project->getNamespaces() as $namespace) {
            $namespaceDir = $this->dstDir . str_replace('\\', '/', $namespace->getFqsen());
            if (is_dir($namespaceDir)) {
                continue;
            }
            if (!mkdir($namespaceDir, 0755, true)) {
                throw new WriteException('Could not create directory ' . $namespaceDir);
            }
        }
    }
181405
protected function generateFilePath()
    {
        $hash = $this->getHash();
        
        $dir = '';
        
        $depth = $this->getDepth();
        for($i = 0; $i < $depth; ++$i)
        {
            $dir .= $hash[$i].'/';
        }
        
        if ('' != $this->getExtension())
        {
            $hash .= '.'.$this->getExtension();
        }
        
        $parts = array(
        	$this->getClassName(),
            $this->getFieldName(),
            rtrim($dir, '/'),
            $hash,
        );
        
        return implode('/', $parts);
    }
169189
private function getDirectoryIterator($path)
    {
        $nodes = new \DirectoryIterator($path);
        $dirs = [];

        foreach ($nodes as $dir) {
            if (!$dir->isDir()) {
                continue;
            }

            if ($dir->isDot()) {
                continue;
            }

            $dirs[hexdec($dir->getFilename())] = $dir->getPathname();
        }

        krsort($dirs);

        return new \ArrayIterator($dirs);
    }
233317
public function save( & $structure )
    {
        if ( $structure instanceof Structure )
        {
            $userId     = $structure->userId;
            $section    = $structure->section;
            $settings   = $structure->settings;
        }
        else
        {
            $data = (array) $structure;

            if ( ArrayUtils::isHashTable( $data ) )
            {
                $userId     = $data['userId'];
                $section    = $data['section'];
                $settings   = $data['settings'];
            }
            else
            {
                list( $userId,
                      $section,
                      $settings ) = $data;
            }
        }

        $rows       = 0;
        $savedKeys  = array();
        $sql        = $this->sql();

        foreach ( $settings as $key => $value )
        {
            $update = $sql->update()
                          ->set( array(
                              'value' => $value
                          ) )
                          ->where( array(
                              'userId'  => $userId,
                              'section' => $section,
                              'key'     => $key,
                          ) );

            $affected = $this->sql()
                             ->prepareStatementForSqlObject( $update )
                             ->execute()
                             ->getAffectedRows();

            if ( $affected )
            {
                $rows += $affected;
            }
            else
            {
                $insert = $sql->insert()
                              ->values( array(
                                 'userId'  => $userId,
                                 'section' => $section,
                                 'key'     => $key,
                                 'value'   => $value,
                             ) );

                $rows += $this->sql()
                              ->prepareStatementForSqlObject( $insert )
                              ->execute()
                              ->getAffectedRows();
            }

            $savedKeys[] = $key;
        }

        $deleteWhere = array(
            'userId'    => $userId,
            'section'   => $section,
        );

        if ( ! empty( $savedKeys ) )
        {
            $deleteWhere[] = new NotIn( 'key', $savedKeys );
        }

        $delete = $this->sql()
                       ->delete()
                       ->where( $deleteWhere );

        $rows += $this->sql()
                      ->prepareStatementForSqlObject( $delete )
                      ->execute()
                      ->getAffectedRows();

        return $rows;
    }
1043697
public function makeDirectory($mode = FileSystemPermission::NORMAL)
    {
        $directory = $this->factory->makeDirectory($mode);
        $this->container->add($directory);

        return $directory;
    }
189378
public function getEncoding($structure) {
        if (property_exists($structure, 'parameters')) {
            foreach ($structure->parameters as $parameter) {
                if (strtolower($parameter->attribute) == "charset") {
                    return EncodingAliases::get($parameter->value);
                }
            }
        }elseif (is_string($structure) === true){
            return mb_detect_encoding($structure);
        }

        return 'UTF-8';
    }
957332
protected function define_structure() {

        $paths = array();

        $logstore = new restore_path_element('logstore', '/logstores/logstore');
        $paths[] = $logstore;

        // Add logstore subplugin support to the 'logstore' element.
        $this->add_subplugin_structure('logstore', $logstore, 'tool', 'log');

        return array($logstore);
    }
382765
public function makeDirectory($subDir = '')
    {
        if ($this->blob === null) {
            throw new \Exception('Blob path is not set yet.');
        }

        if ($subDir) {
            $name = $this->getUniqueFileName($this->blob->path, Str::slug($subDir));
            $this->blob->path = trim($this->blob->path . '/' . $name, '/\\');
        }

        if ($this->blob->path !== '') {
            $this->storage->makeDirectory($this->blob->path);
        }

        return $this;
    }
386710
protected function getDocumentPath(array $parameters = []) {
        $dirOut = $this->getDirOutput();
        $dirOut = $dirOut.DIRECTORY_SEPARATOR.$this->getFileNameTranslate($parameters).'.'.$this->getFormat();
        return $dirOut;
    }
1006422
public function createDirectories()
    {
        if (!file_exists($this->getDomainPath())) {
            mkdir($this->getDomainPath(), 0777);
        }

        if (!file_exists($this->getInfrastructurePath())) {
            mkdir($this->getInfrastructurePath(), 0777);
        }

        if (!file_exists($this->getTestPath())) {
            mkdir($this->getTestPath(), 0777);
        }

        if (!$this->option('without-abstracts')) {
            $this->copyAbstractClasses();
        }

        $this->setDomainPath($this->getDomainPath(DIRECTORY_SEPARATOR.$this->getDirectory()));
        $this->setInfrastructurePath($this->getInfrastructurePath(DIRECTORY_SEPARATOR.$this->getDirectory()));
        $this->setTestPath($this->getTestPath(DIRECTORY_SEPARATOR.$this->getDirectory()));

        if (!file_exists($this->getDomainPath())) {
            mkdir($this->getDomainPath(), 0777);
            mkdir($this->getDomainPath('/Contracts'), 0777);
        }

        if (!file_exists($this->getInfrastructurePath())) {
            mkdir($this->getInfrastructurePath(), 0777);
            mkdir($this->getInfrastructurePath('/Contracts'), 0777);
        }

        if (!file_exists($this->getTestPath())) {
            mkdir($this->getTestPath(), 0777);
        }

        return true;
    }
964921
private function parse_structure($structure, $recursive = false)
    {
        // real content-type of message/rfc822 part
        if ($structure->mimetype == 'message/rfc822' && $structure->real_mimetype) {
            $mimetype = $structure->real_mimetype;

            // parse headers from message/rfc822 part
            if (!isset($structure->headers['subject']) && !isset($structure->headers['from'])) {
                list($headers, ) = explode("\r\n\r\n", $this->get_part_body($structure->mime_id, false, 32768));
                $structure->headers = rcube_mime::parse_headers($headers);

                if ($this->context == $structure->mime_id) {
                    $this->headers = rcube_message_header::from_array($structure->headers);
                }
            }
        }
        else {
            $mimetype = $structure->mimetype;
        }

        // show message headers
        if ($recursive && is_array($structure->headers) &&
            (isset($structure->headers['subject']) || $structure->headers['from'] || $structure->headers['to'])
        ) {
            $c = new stdClass;
            $c->type = 'headers';
            $c->headers = $structure->headers;
            $this->add_part($c);
        }

        // Allow plugins to handle message parts
        $plugin = $this->app->plugins->exec_hook('message_part_structure',
            array('object' => $this, 'structure' => $structure,
                'mimetype' => $mimetype, 'recursive' => $recursive));

        if ($plugin['abort']) {
            return;
        }

        $structure = $plugin['structure'];
        $mimetype  = $plugin['mimetype'];
        $recursive = $plugin['recursive'];

        list($message_ctype_primary, $message_ctype_secondary) = explode('/', $mimetype);

        // print body if message doesn't have multiple parts
        if ($message_ctype_primary == 'text' && !$recursive) {
            // parts with unsupported type add to attachments list
            if (!in_array($message_ctype_secondary, array('plain', 'html', 'enriched'))) {
                $this->add_part($structure, 'attachment');
                return;
            }

            $structure->type = 'content';
            $this->add_part($structure);

            // Parse simple (plain text) message body
            if ($message_ctype_secondary == 'plain') {
                foreach ((array)$this->uu_decode($structure) as $uupart) {
                    $this->mime_parts[$uupart->mime_id] = $uupart;
                    $this->add_part($uupart, 'attachment');
                }
            }
        }
        // the same for pgp signed messages
        else if ($mimetype == 'application/pgp' && !$recursive) {
            $structure->type = 'content';
            $this->add_part($structure);
        }
        // message contains (more than one!) alternative parts
        else if ($mimetype == 'multipart/alternative'
            && is_array($structure->parts) && count($structure->parts) > 1
        ) {
            // get html/plaintext parts, other add to attachments list
            foreach ($structure->parts as $p => $sub_part) {
                $sub_mimetype = $sub_part->mimetype;
                $is_multipart = preg_match('/^multipart\/(related|relative|mixed|alternative)/', $sub_mimetype);

                // skip empty text parts
                if (!$sub_part->size && !$is_multipart) {
                    continue;
                }

                // We've encountered (malformed) messages with more than
                // one text/plain or text/html part here. There's no way to choose
                // which one is better, so we'll display first of them and add
                // others as attachments (#1489358)

                // check if sub part is
                if ($is_multipart)
                    $related_part = $p;
                else if ($sub_mimetype == 'text/plain' && !$plain_part)
                    $plain_part = $p;
                else if ($sub_mimetype == 'text/html' && !$html_part) {
                    $html_part = $p;
                    $this->got_html_part = true;
                }
                else if ($sub_mimetype == 'text/enriched' && !$enriched_part)
                    $enriched_part = $p;
                else {
                    // add unsupported/unrecognized parts to attachments list
                    $this->add_part($sub_part, 'attachment');
                }
            }

            // parse related part (alternative part could be in here)
            if ($related_part !== null && !$this->parse_alternative) {
                $this->parse_alternative = true;
                $this->parse_structure($structure->parts[$related_part], true);
                $this->parse_alternative = false;

                // if plain part was found, we should unset it if html is preferred
                if ($this->opt['prefer_html'] && count($this->parts)) {
                    $plain_part = null;
                }
            }

            // choose html/plain part to print
            if ($html_part !== null && $this->opt['prefer_html']) {
                $print_part = $structure->parts[$html_part];
            }
            else if ($enriched_part !== null) {
                $print_part = $structure->parts[$enriched_part];
            }
            else if ($plain_part !== null) {
                $print_part = $structure->parts[$plain_part];
            }

            // add the right message body
            if (is_object($print_part)) {
                $print_part->type = 'content';

                // Allow plugins to handle also this part
                $plugin = $this->app->plugins->exec_hook('message_part_structure',
                    array('object' => $this, 'structure' => $print_part,
                        'mimetype' => $print_part->mimetype, 'recursive' => true));

                if (!$plugin['abort']) {
                    $this->add_part($print_part);
                }
            }
            // show plaintext warning
            else if ($html_part !== null && empty($this->parts)) {
                $c = new stdClass;
                $c->type            = 'content';
                $c->ctype_primary   = 'text';
                $c->ctype_secondary = 'plain';
                $c->mimetype        = 'text/plain';
                $c->realtype        = 'text/html';

                $this->add_part($c);
            }
        }
        // this is an ecrypted message -> create a plaintext body with the according message
        else if ($mimetype == 'multipart/encrypted') {
            $p = new stdClass;
            $p->type            = 'content';
            $p->ctype_primary   = 'text';
            $p->ctype_secondary = 'plain';
            $p->mimetype        = 'text/plain';
            $p->realtype        = 'multipart/encrypted';
            $p->mime_id         = $structure->mime_id;

            $this->add_part($p);

            // add encrypted payload part as attachment
            if (is_array($structure->parts)) {
                for ($i=0; $i < count($structure->parts); $i++) {
                    $subpart = $structure->parts[$i];
                    if ($subpart->mimetype == 'application/octet-stream' || !empty($subpart->filename)) {
                        $this->add_part($subpart, 'attachment');
                    }
                }
            }
        }
        // this is an S/MIME ecrypted message -> create a plaintext body with the according message
        else if ($mimetype == 'application/pkcs7-mime') {
            $p = new stdClass;
            $p->type            = 'content';
            $p->ctype_primary   = 'text';
            $p->ctype_secondary = 'plain';
            $p->mimetype        = 'text/plain';
            $p->realtype        = 'application/pkcs7-mime';
            $p->mime_id         = $structure->mime_id;

            $this->add_part($p);

            if (!empty($structure->filename)) {
                $this->add_part($structure, 'attachment');
            }
        }
        // message contains multiple parts
        else if (is_array($structure->parts) && !empty($structure->parts)) {
            // iterate over parts
            for ($i=0; $i < count($structure->parts); $i++) {
                $mail_part      = &$structure->parts[$i];
                $primary_type   = $mail_part->ctype_primary;
                $secondary_type = $mail_part->ctype_secondary;
                $part_mimetype  = $mail_part->mimetype;

                // multipart/alternative or message/rfc822
                if ($primary_type == 'multipart' || $part_mimetype == 'message/rfc822') {
                    // list message/rfc822 as attachment as well
                    if ($part_mimetype == 'message/rfc822') {
                        $this->add_part($mail_part, 'attachment');
                    }

                    $this->parse_structure($mail_part, true);
                }
                // part text/[plain|html] or delivery status
                else if ((($part_mimetype == 'text/plain' || $part_mimetype == 'text/html') && $mail_part->disposition != 'attachment') ||
                    in_array($part_mimetype, array('message/delivery-status', 'text/rfc822-headers', 'message/disposition-notification'))
                ) {
                    // Allow plugins to handle also this part
                    $plugin = $this->app->plugins->exec_hook('message_part_structure',
                        array('object' => $this, 'structure' => $mail_part,
                            'mimetype' => $part_mimetype, 'recursive' => true));

                    if ($plugin['abort']) {
                        continue;
                    }

                    if ($part_mimetype == 'text/html' && $mail_part->size) {
                        $this->got_html_part = true;
                    }

                    $mail_part = $plugin['structure'];
                    list($primary_type, $secondary_type) = explode('/', $plugin['mimetype']);

                    // add text part if it matches the prefs
                    if (!$this->parse_alternative ||
                        ($secondary_type == 'html' && $this->opt['prefer_html']) ||
                        ($secondary_type == 'plain' && !$this->opt['prefer_html'])
                    ) {
                        $mail_part->type = 'content';
                        $this->add_part($mail_part);
                    }

                    // list as attachment as well
                    if (!empty($mail_part->filename)) {
                        $this->add_part($mail_part, 'attachment');
                    }
                }
                // ignore "virtual" protocol parts
                else if ($primary_type == 'protocol') {
                    continue;
                }
                // part is Microsoft Outlook TNEF (winmail.dat)
                else if ($part_mimetype == 'application/ms-tnef') {
                    $tnef_parts = (array) $this->tnef_decode($mail_part);
                    foreach ($tnef_parts as $tpart) {
                        $this->mime_parts[$tpart->mime_id] = $tpart;
                        $this->add_part($tpart, 'attachment');
                    }

                    // add winmail.dat to the list if it's content is unknown
                    if (empty($tnef_parts) && !empty($mail_part->filename)) {
                        $this->mime_parts[$mail_part->mime_id] = $mail_part;
                        $this->add_part($mail_part, 'attachment');
                    }
                }
                // part is a file/attachment
                else if (preg_match('/^(inline|attach)/', $mail_part->disposition) ||
                    $mail_part->headers['content-id'] ||
                    ($mail_part->filename &&
                        (empty($mail_part->disposition) || preg_match('/^[a-z0-9!#$&.+^_-]+$/i', $mail_part->disposition)))
                ) {
                    // skip apple resource forks
                    if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile')
                        continue;

                    // part belongs to a related message and is linked
                    if (preg_match('/^multipart\/(related|relative)/', $mimetype)
                        && ($mail_part->headers['content-id'] || $mail_part->headers['content-location'])
                    ) {
                        if ($mail_part->headers['content-id'])
                            $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
                        if ($mail_part->headers['content-location'])
                            $mail_part->content_location = $mail_part->headers['content-base'] . $mail_part->headers['content-location'];

                        $this->add_part($mail_part, 'inline');
                    }
                    // regular attachment with valid content type
                    // (content-type name regexp according to RFC4288.4.2)
                    else if (preg_match('/^[a-z0-9!#$&.+^_-]+\/[a-z0-9!#$&.+^_-]+$/i', $part_mimetype)) {
                        $this->add_part($mail_part, 'attachment');
                    }
                    // attachment with invalid content type
                    // replace malformed content type with application/octet-stream (#1487767)
                    else if ($mail_part->filename) {
                        $mail_part->ctype_primary   = 'application';
                        $mail_part->ctype_secondary = 'octet-stream';
                        $mail_part->mimetype        = 'application/octet-stream';

                        $this->add_part($mail_part, 'attachment');
                    }
                }
                // calendar part not marked as attachment (#1490325)
                else if ($part_mimetype == 'text/calendar') {
                    if (!$mail_part->filename) {
                        $mail_part->filename = 'calendar.ics';
                    }

                    $this->add_part($mail_part, 'attachment');
                }
            }

            // if this was a related part try to resolve references
            if (preg_match('/^multipart\/(related|relative)/', $mimetype) && count($this->inline_parts)) {
                $a_replaces = array();
                $img_regexp = '/^image\/(gif|jpe?g|png|tiff|bmp|svg)/';

                foreach ($this->inline_parts as $inline_object) {
                    $part_url = $this->get_part_url($inline_object->mime_id, $inline_object->ctype_primary);
                    if (isset($inline_object->content_id))
                        $a_replaces['cid:'.$inline_object->content_id] = $part_url;
                    if ($inline_object->content_location) {
                        $a_replaces[$inline_object->content_location] = $part_url;
                    }

                    if (!empty($inline_object->filename)) {
                        // MS Outlook sends sometimes non-related attachments as related
                        // In this case multipart/related message has only one text part
                        // We'll add all such attachments to the attachments list
                        if (!isset($this->got_html_part)) {
                            $this->add_part($inline_object, 'attachment');
                        }
                        // MS Outlook sometimes also adds non-image attachments as related
                        // We'll add all such attachments to the attachments list
                        // Warning: some browsers support pdf in <img/>
                        else if (!preg_match($img_regexp, $inline_object->mimetype)) {
                            $this->add_part($inline_object, 'attachment');
                        }
                        // @TODO: we should fetch HTML body and find attachment's content-id
                        // to handle also image attachments without reference in the body
                        // @TODO: should we list all image attachments in text mode?
                    }
                }

                // add replace array to each content part
                // (will be applied later when part body is available)
                foreach ($this->parts as $i => $part) {
                    if ($part->type == 'content')
                        $this->parts[$i]->replaces = $a_replaces;
                }
            }
        }
        // message is a single part non-text
        else if ($structure->filename || preg_match('/^application\//i', $mimetype)) {
            $this->add_part($structure, 'attachment');
        }
    }
606118
public function getDirectory($asArray = false)
    {
        $asArray   = (bool) $asArray;
        $directory = (string) $this->_directory;
        if ($asArray) {
            $directory = explode(',', $directory);
        }

        return $directory;
    }
44913
private function fetchStructure($structure, $partNumber = null) {
        $this->client->openFolder($this->folder_path);

        if ($structure->type == IMAP::MESSAGE_TYPE_TEXT &&
            ($structure->ifdisposition == 0 ||
                ($structure->ifdisposition == 1 && !isset($structure->parts) && $partNumber == null)
            )
        ) {
            if ($structure->subtype == "PLAIN") {
                if (!$partNumber) {
                    $partNumber = 1;
                }

                $encoding = $this->getEncoding($structure);

                $content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options | IMAP::FT_UID);
                $content = $this->decodeString($content, $structure->encoding);

                // We don't need to do convertEncoding() if charset is ASCII (us-ascii):
                //     ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
                //     https://stackoverflow.com/a/11303410
                //
                // us-ascii is the same as ASCII:
                //     ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
                //     prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
                //     based on the typographical symbols predominantly in use there.
                //     https://en.wikipedia.org/wiki/ASCII
                //
                // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
                if ($encoding != 'us-ascii') {
                    $content = $this->convertEncoding($content, $encoding);
                }

                $body = new \stdClass;
                $body->type = "text";
                $body->content = $content;

                $this->bodies['text'] = $body;

                $this->fetchAttachment($structure, $partNumber);

            } elseif ($structure->subtype == "HTML") {
                if (!$partNumber) {
                    $partNumber = 1;
                }

                $encoding = $this->getEncoding($structure);

                $content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options | IMAP::FT_UID);
                $content = $this->decodeString($content, $structure->encoding);
                if ($encoding != 'us-ascii') {
                    $content = $this->convertEncoding($content, $encoding);
                }

                $body = new \stdClass;
                $body->type = "html";
                $body->content = $content;

                $this->bodies['html'] = $body;
            }
        } elseif ($structure->type == IMAP::MESSAGE_TYPE_MULTIPART) {
            foreach ($structure->parts as $index => $subStruct) {
                $prefix = "";
                if ($partNumber) {
                    $prefix = $partNumber.".";
                }
                $this->fetchStructure($subStruct, $prefix.($index + 1));
            }
        } else {
            if ($this->getFetchAttachmentOption() === true) {
                $this->fetchAttachment($structure, $partNumber);
            }
        }
    }
986876
protected function getForm( StructureInterface $structure )
    {
        /* @var $form \Zend\Form\Form */
        /* @var $type \Zend\Form\Form */
        $service = $this->getServiceLocator()
                        ->get( 'Form' );
        $form    = $service->create( 'Grid\Menu\Meta\Base' );
        $meta    = $service->get( 'Grid\Menu\Meta\Type' );
        $type    = $structure->getType();

        if ( $meta->has( $type ) )
        {
            foreach ( $meta->get( $type ) as $element )
            {
                $form->add( clone $element );
            }
        }

        $form->add( array(
            'type'  => 'Zork\Form\Element\Submit',
            'name'  => 'save',
            'attributes' => array(
                'value'  => 'menu.form.submit',
            ),
        ) );

        $form->setHydrator( $this->getModel()->getMapper() )
             ->bind( $structure );

        return $form;
    }
713841
public function createDirectory(string $path): Directory {
    $localDirectory = $this->instantiateDir($path);
    $localDirectory->create();
    return $localDirectory;
  }