Hoa central
Directory.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\File;
38 
39 use Hoa\Stream;
40 
49 class Directory extends Generic
50 {
56  const MODE_READ = 'rb';
57 
64  const MODE_CREATE = 'xb';
65 
72  const MODE_CREATE_RECURSIVE = 'xrb';
73 
74 
75 
86  public function __construct(
87  $streamName,
88  $mode = self::MODE_READ,
89  $context = null,
90  $wait = false
91  ) {
92  $this->setMode($mode);
93  parent::__construct($streamName, $context, $wait);
94 
95  return;
96  }
97 
107  protected function &_open($streamName, Stream\Context $context = null)
108  {
109  if (false === is_dir($streamName)) {
110  if ($this->getMode() == self::MODE_READ) {
111  throw new Exception\FileDoesNotExist(
112  'Directory %s does not exist.',
113  0,
114  $streamName
115  );
116  } else {
117  self::create(
118  $streamName,
119  $this->getMode(),
120  null !== $context
121  ? $context->getContext()
122  : null
123  );
124  }
125  }
126 
127  $out = null;
128 
129  return $out;
130  }
131 
137  protected function _close()
138  {
139  return true;
140  }
141 
153  public function copy($to, $force = Stream\IStream\Touchable::DO_NOT_OVERWRITE)
154  {
155  if (empty($to)) {
156  throw new Exception(
157  'The destination path (to copy) is empty.',
158  1
159  );
160  }
161 
162  $from = $this->getStreamName();
163  $fromLength = strlen($from) + 1;
164  $finder = new Finder();
165  $finder->in($from);
166 
167  self::create($to, self::MODE_CREATE_RECURSIVE);
168 
169  foreach ($finder as $file) {
170  $relative = substr($file->getPathname(), $fromLength);
171  $_to = $to . DS . $relative;
172 
173  if (true === $file->isDir()) {
174  self::create($_to, self::MODE_CREATE);
175 
176  continue;
177  }
178 
179  $file->open()->copy($_to, $force);
180  $file->close();
181  }
182 
183  return true;
184  }
185 
191  public function delete()
192  {
193  $from = $this->getStreamName();
194  $finder = new Finder();
195  $finder->in($from)
196  ->childFirst();
197 
198  foreach ($finder as $file) {
199  $file->open()->delete();
200  $file->close();
201  }
202 
203  if (null === $this->getStreamContext()) {
204  return @rmdir($from);
205  }
206 
207  return @rmdir($from, $this->getStreamContext()->getContext());
208  }
209 
221  public static function create(
222  $name,
223  $mode = self::MODE_CREATE_RECURSIVE,
224  $context = null
225  ) {
226  if (true === is_dir($name)) {
227  return true;
228  }
229 
230  if (empty($name)) {
231  return false;
232  }
233 
234  if (null !== $context) {
235  if (false === Stream\Context::contextExists($context)) {
236  throw new Exception(
237  'Context %s was not previously declared, cannot retrieve ' .
238  'this context.',
239  2,
240  $context
241  );
242  } else {
243  $context = Stream\Context::getInstance($context);
244  }
245  }
246 
247  if (null === $context) {
248  return @mkdir(
249  $name,
250  0755,
251  self::MODE_CREATE_RECURSIVE === $mode
252  );
253  }
254 
255  return @mkdir(
256  $name,
257  0755,
258  self::MODE_CREATE_RECURSIVE === $mode,
259  $context->getContext()
260  );
261  }
262 }
const MODE_CREATE_RECURSIVE
Definition: Directory.php:72
static contextExists($id)
Definition: Context.php:114
__construct($streamName, $mode=self::MODE_READ, $context=null, $wait=false)
Definition: Directory.php:86
copy($to, $force=Stream\IStream\Touchable::DO_NOT_OVERWRITE)
Definition: Directory.php:153
setMode($mode)
Definition: Generic.php:517
& _open($streamName, Stream\Context $context=null)
Definition: Directory.php:107
static create($name, $mode=self::MODE_CREATE_RECURSIVE, $context=null)
Definition: Directory.php:221
static getInstance($id)
Definition: Context.php:85