Code Duplication    Length = 182-182 lines in 2 locations

php5/Objects/DirectoryTreeObject.inc 1 location

@@ 32-213 (lines=182) @@
29
use \HOWI3\libhowi\Filesystem\Commons\TraitForResponse;
30
use \HOWI3\libhowi\Filesystem\php5\TraitForSharedMethods;
31
32
class DirectoryTreeObject extends RecursiveDirectoryIterator implements DirectoryTreeInterface
33
{
34
    use TraitForResponse;
35
    use TraitForSharedMethods;
36
37
    /**
38
     *
39
     * @var array $dirkeys Reqistered basenames
40
     */
41
    private $dirkeys = [];
42
43
    /**
44
     *
45
     * {@inheritDoc}
46
     *
47
     */
48
    public function c($basename = false)
49
    {
50
        if (! empty($basename) && array_key_exists($basename, $this->dirkeys))
51
            $c = $this->dirkeys[$basename];
52
        elseif (! empty($basename) && is_dir($this->getPath() . DIRECTORY_SEPARATOR . $basename)) {
53
            $c = new DirectoryTreeObject($this->getPath() . DIRECTORY_SEPARATOR . $basename, 
54
                DirectoryTreeObject::SKIP_DOTS);
55
            $c->setFileClass('\HOWI3\libhowi\Filesystem\php5\Objects\FileObject');
56
            $c->setInfoClass('\HOWI3\libhowi\Filesystem\php5\Objects\InfoObject');
57
            $c->setLogFile($this->getLogFile());
58
            $c->setLogLevel($this->getLogLevel());
59
            $c->setUID($this->getUID());
60
            $c->setUsername($this->getUsername());
61
        } 
62
63
        else
64
            $c = false;
65
        
66
        return $this->dirkeys[$basename] = $c;
67
    }
68
69
    /**
70
     *
71
     * {@inheritDoc}
72
     *
73
     */
74
    public function hasChildren($allow_links = false)
75
    {
76
        return $this->valid();
77
    }
78
79
    /**
80
     *
81
     * {@inheritDoc}
82
     *
83
     */
84
    public function ls($sort = false)
85
    {
86
        $this->rewind();
87
        
88
        $ls = [
89
            'dir' => [],
90
            'link' => [],
91
            'file' => []
92
        ];
93
        $display = [];
94
        
95
        while ($this->valid()) {
96
            if (! $this->isDot()) {
97
                if ($this->isDir() && ! $this->isLink() && ! empty($sort))
98
                    $ls['dir'][$this->getFilename()] = $this->getType();
99
                
100
                elseif ($this->isLink() && ! empty($sort))
101
                    $ls['link'][$this->getFilename()] = $this->getType();
102
                
103
                elseif ($this->isFile() && ! empty($sort))
104
                    $ls['file'][$this->getFilename()] = $this->getType();
105
                else
106
                    $display[$this->getFilename()] = $this->getType();
107
                
108
                if ($this->isDir() || $this->isLink()) {
109
                    $this->dirkeys[$this->getFilename()] = $this->getChildren();
110
                }
111
            }
112
            
113
            $this->next();
114
        }
115
        if (! empty($sort)) {
116
            
117
            ksort($ls['dir']);
118
            ksort($ls['link']);
119
            ksort($ls['file']);
120
            
121
            $display = array_merge($ls['dir'], $ls['link'], $ls['file']);
122
        }
123
        return $display;
124
    }
125
126
    /**
127
     *
128
     * {@inheritDoc}
129
     *
130
     */
131
    public function lsInfo($sort = false, $convert = false, $timeformat = false)
132
    {
133
        $this->rewind();
134
        
135
        $ls = [
136
            'dir' => [],
137
            'link' => [],
138
            'file' => []
139
        ];
140
        $display = [];
141
        $lsinfo = [];
142
        
143
        while ($this->valid()) {
144
            
145
            if (! $this->isDot()) {
146
                $cname = $this->getFilename();
147
                $lsinfo[$cname] = new \stdClass();
148
                $lsinfo[$cname]->name = $this->getFilename();
149
                $lsinfo[$cname]->type = $this->getType();
150
                $lsinfo[$cname]->size = $this->getSize($convert);
151
                
152
                $lsinfo[$cname]->taccess = $this->getATime($timeformat);
153
                $lsinfo[$cname]->tchange = $this->getCTime($timeformat);
154
                $lsinfo[$cname]->tmodify = $this->getMTime($timeformat);
155
                
156
                if ($this->isDir() && ! $this->isLink() && ! empty($sort))
157
                    $ls['dir'][$cname] = $lsinfo[$cname]->type;
158
                
159
                elseif ($this->isLink() && ! empty($sort))
160
                    $ls['link'][$cname] = $lsinfo[$cname]->type;
161
                
162
                elseif ($this->isFile() && ! empty($sort))
163
                    $ls['file'][$cname] = $lsinfo[$cname]->type;
164
                else
165
                    $display[$cname] = $lsinfo[$cname]->type;
166
            }
167
            $this->next();
168
        }
169
        if (! empty($sort)) {
170
            ksort($ls['dir']);
171
            ksort($ls['link']);
172
            ksort($ls['file']);
173
            
174
            $display = array_merge($ls['dir'], $ls['link'], $ls['file']);
175
        }
176
        foreach ($display as $row => $type) {
177
            $display[$row] = $lsinfo[$row];
178
        }
179
        return $display;
180
    }
181
182
    /**
183
     *
184
     * {@inheritDoc}
185
     *
186
     */
187
    public function lsTree()
188
    {
189
        $DirectoryTreeObject = new RecursiveDirectoryIterator($this->getPath(), 
190
            RecursiveDirectoryIterator::SKIP_DOTS);
191
        $DirectoryTreeObject->setInfoClass('\HOWI3\libhowi\Filesystem\php5\Objects\InfoObject');
192
        
193
        $ritit = new RecursiveIteratorIterator($DirectoryTreeObject, RecursiveIteratorIterator::CHILD_FIRST);
194
        $tree = array();
195
        foreach ($ritit as $splFileInfo) {
196
            $path = $splFileInfo->isDir() ? array(
197
                $splFileInfo->getFilename() => array()
198
            ) : array(
199
                $splFileInfo->getFilename()
200
            );
201
            
202
            for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth --) {
203
                $path = array(
204
                    $ritit->getSubIterator($depth)
205
                        ->current()
206
                        ->getFilename() => $path
207
                );
208
            }
209
            $tree = array_merge_recursive($tree, $path);
210
        }
211
        return $tree;
212
    }
213
}
214

php7/Objects/DirectoryTreeObject.inc 1 location

@@ 32-213 (lines=182) @@
29
use \HOWI3\libhowi\Filesystem\Commons\TraitForResponse;
30
use \HOWI3\libhowi\Filesystem\php7\TraitForSharedMethods;
31
32
class DirectoryTreeObject extends RecursiveDirectoryIterator implements DirectoryTreeInterface
33
{
34
    use TraitForResponse;
35
    use TraitForSharedMethods;
36
37
    /**
38
     *
39
     * @var array $dirkeys Reqistered basenames
40
     */
41
    private $dirkeys = [];
42
43
    /**
44
     *
45
     * {@inheritDoc}
46
     *
47
     */
48
    public function c($basename = false)
49
    {
50
        if (! empty($basename) && array_key_exists($basename, $this->dirkeys))
51
            $c = $this->dirkeys[$basename];
52
        elseif (! empty($basename) && is_dir($this->getPath() . DIRECTORY_SEPARATOR . $basename)) {
53
            $c = new DirectoryTreeObject($this->getPath() . DIRECTORY_SEPARATOR . $basename, 
54
                DirectoryTreeObject::SKIP_DOTS);
55
            $c->setFileClass('\HOWI3\libhowi\Filesystem\php7\Objects\FileObject');
56
            $c->setInfoClass('\HOWI3\libhowi\Filesystem\php7\Objects\InfoObject');
57
            $c->setLogFile($this->getLogFile());
58
            $c->setLogLevel($this->getLogLevel());
59
            $c->setUID($this->getUID());
60
            $c->setUsername($this->getUsername());
61
        } 
62
63
        else
64
            $c = false;
65
        
66
        return $this->dirkeys[$basename] = $c;
67
    }
68
69
    /**
70
     *
71
     * {@inheritDoc}
72
     *
73
     */
74
    public function hasChildren($allow_links = false)
75
    {
76
        return $this->valid();
77
    }
78
79
    /**
80
     *
81
     * {@inheritDoc}
82
     *
83
     */
84
    public function ls($sort = false)
85
    {
86
        $this->rewind();
87
        
88
        $ls = [
89
            'dir' => [],
90
            'link' => [],
91
            'file' => []
92
        ];
93
        $display = [];
94
        
95
        while ($this->valid()) {
96
            if (! $this->isDot()) {
97
                if ($this->isDir() && ! $this->isLink() && ! empty($sort))
98
                    $ls['dir'][$this->getFilename()] = $this->getType();
99
                
100
                elseif ($this->isLink() && ! empty($sort))
101
                    $ls['link'][$this->getFilename()] = $this->getType();
102
                
103
                elseif ($this->isFile() && ! empty($sort))
104
                    $ls['file'][$this->getFilename()] = $this->getType();
105
                else
106
                    $display[$this->getFilename()] = $this->getType();
107
                
108
                if ($this->isDir() || $this->isLink()) {
109
                    $this->dirkeys[$this->getFilename()] = $this->getChildren();
110
                }
111
            }
112
            
113
            $this->next();
114
        }
115
        if (! empty($sort)) {
116
            
117
            ksort($ls['dir']);
118
            ksort($ls['link']);
119
            ksort($ls['file']);
120
            
121
            $display = array_merge($ls['dir'], $ls['link'], $ls['file']);
122
        }
123
        return $display;
124
    }
125
126
    /**
127
     *
128
     * {@inheritDoc}
129
     *
130
     */
131
    public function lsInfo($sort = false, $convert = false, $timeformat = false)
132
    {
133
        $this->rewind();
134
        
135
        $ls = [
136
            'dir' => [],
137
            'link' => [],
138
            'file' => []
139
        ];
140
        $display = [];
141
        $lsinfo = [];
142
        
143
        while ($this->valid()) {
144
            
145
            if (! $this->isDot()) {
146
                $cname = $this->getFilename();
147
                $lsinfo[$cname] = new \stdClass();
148
                $lsinfo[$cname]->name = $this->getFilename();
149
                $lsinfo[$cname]->type = $this->getType();
150
                $lsinfo[$cname]->size = $this->getSize($convert);
151
                
152
                $lsinfo[$cname]->taccess = $this->getATime($timeformat);
153
                $lsinfo[$cname]->tchange = $this->getCTime($timeformat);
154
                $lsinfo[$cname]->tmodify = $this->getMTime($timeformat);
155
                
156
                if ($this->isDir() && ! $this->isLink() && ! empty($sort))
157
                    $ls['dir'][$cname] = $lsinfo[$cname]->type;
158
                
159
                elseif ($this->isLink() && ! empty($sort))
160
                    $ls['link'][$cname] = $lsinfo[$cname]->type;
161
                
162
                elseif ($this->isFile() && ! empty($sort))
163
                    $ls['file'][$cname] = $lsinfo[$cname]->type;
164
                else
165
                    $display[$cname] = $lsinfo[$cname]->type;
166
            }
167
            $this->next();
168
        }
169
        if (! empty($sort)) {
170
            ksort($ls['dir']);
171
            ksort($ls['link']);
172
            ksort($ls['file']);
173
            
174
            $display = array_merge($ls['dir'], $ls['link'], $ls['file']);
175
        }
176
        foreach ($display as $row => $type) {
177
            $display[$row] = $lsinfo[$row];
178
        }
179
        return $display;
180
    }
181
182
    /**
183
     *
184
     * {@inheritDoc}
185
     *
186
     */
187
    public function lsTree()
188
    {
189
        $DirectoryTreeObject = new RecursiveDirectoryIterator($this->getPath(), 
190
            RecursiveDirectoryIterator::SKIP_DOTS);
191
        $DirectoryTreeObject->setInfoClass('\HOWI3\libhowi\Filesystem\php7\Objects\InfoObject');
192
        
193
        $ritit = new RecursiveIteratorIterator($DirectoryTreeObject, RecursiveIteratorIterator::CHILD_FIRST);
194
        $tree = array();
195
        foreach ($ritit as $splFileInfo) {
196
            $path = $splFileInfo->isDir() ? array(
197
                $splFileInfo->getFilename() => array()
198
            ) : array(
199
                $splFileInfo->getFilename()
200
            );
201
            
202
            for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth --) {
203
                $path = array(
204
                    $ritit->getSubIterator($depth)
205
                        ->current()
206
                        ->getFilename() => $path
207
                );
208
            }
209
            $tree = array_merge_recursive($tree, $path);
210
        }
211
        return $tree;
212
    }
213
}
214