Best VfsStream code snippet using has.getContentOfType
vfsStreamWrapper.php
Source:vfsStreamWrapper.php
...116 * @param string $path117 * @param int $type118 * @return vfsStreamContent119 */120 protected function getContentOfType($path, $type)121 {122 $content = $this->getContent($path);123 if (null !== $content && $content->getType() === $type) {124 return $content;125 }126 127 return null;128 }129 /**130 * splits path into its dirname and the basename131 *132 * @param string $path133 * @return array134 */135 protected function splitPath($path)136 {137 $lastSlashPos = strrpos($path, '/');138 if (false === $lastSlashPos) {139 return array('dirname' => '', 'basename' => $path);140 }141 142 return array('dirname' => substr($path, 0, $lastSlashPos),143 'basename' => substr($path, $lastSlashPos + 1)144 );145 }146 /**147 * helper method to resolve a path from /foo/bar/. to /foo/bar148 *149 * @param string $path150 * @return string151 */152 protected function resolvePath($path)153 {154 if (substr($path, -2) === '/.') {155 return substr($path, 0, -2);156 }157 return $path;158 }159 /**160 * open the stream161 *162 * @param string $path the path to open163 * @param string $mode mode for opening164 * @param string $options options for opening165 * @param string $opened_path full path that was actually opened166 * @return bool167 * @todo evaluate $mode and take action regarding to its value168 */169 public function stream_open($path, $mode, $options, $opened_path)170 {171 $path = vfsStream::path($path);172 $this->content = $this->getContentOfType($path, vfsStreamContent::TYPE_FILE);173 if (null !== $this->content) {174 $this->content->seek(0, SEEK_SET);175 return true;176 }177 178 $names = $this->splitPath($path);179 $dir = $this->getContentOfType($names['dirname'], vfsStreamContent::TYPE_DIR);180 // parent directory does not exist, or it does exist but then already181 // a directory with the basename exists182 if (null === $dir || $dir->hasChild($names['basename']) === true) {183 return false;184 }185 186 $this->content = vfsStream::newFile($names['basename'])->at($dir);187 return true;188 }189 /**190 * closes the stream191 */192 public function stream_close()193 {194 // nothing to do195 }196 /**197 * read the stream up to $count bytes198 *199 * @param int $count amount of bytes to read200 * @return string201 */202 public function stream_read($count)203 {204 return $this->content->read($count);205 }206 /**207 * writes data into the stream208 *209 * @param string $data210 * @return int amount of bytes written211 */212 public function stream_write($data)213 {214 return $this->content->write($data);215 }216 /**217 * checks whether stream is at end of file218 *219 * @return bool220 */221 public function stream_eof()222 {223 return $this->content->eof();224 }225 /**226 * returns the current position of the stream227 *228 * @return int229 */230 public function stream_tell()231 {232 return $this->content->getBytesRead();233 }234 /**235 * seeks to the given offset236 *237 * @param int $offset238 * @param int $whence239 * @return bool240 */241 public function stream_seek($offset, $whence)242 {243 return $this->content->seek($offset, $whence);244 }245 /**246 * flushes unstored data into storage247 *248 * @return bool249 */250 public function stream_flush()251 {252 return true;253 }254 /**255 * returns status of stream256 *257 * @return array258 */259 public function stream_stat()260 {261 $fileStat = array('dev' => 0,262 'ino' => 0,263 'mode' => $this->content->getType() | $this->content->getPermissions(),264 'nlink' => 0,265 'uid' => $this->content->getUser(),266 'gid' => $this->content->getGroup(),267 'rdev' => 0,268 'size' => $this->content->size(),269 'atime' => $this->content->filemtime(),270 'mtime' => $this->content->filemtime(),271 'ctime' => $this->content->filemtime(),272 'blksize' => -1,273 'blocks' => -1274 );275 return array_merge(array_values($fileStat), $fileStat);276 }277 /**278 * remove the data under the given path279 *280 * @param string $path281 * @return bool282 */283 public function unlink($path)284 {285 $realPath = $this->resolvePath(vfsStream::path($path));286 $content = $this->getContent($realPath);287 if (null === $content) {288 return false;289 }290 291 if (self::$root->getName() === $realPath) {292 // delete root? very brave. :)293 self::$root = null;294 clearstatcache();295 return true;296 }297 298 $names = $this->splitPath($realPath);299 $content = $this->getContent($names['dirname']);300 clearstatcache();301 return $content->removeChild($names['basename']);302 }303 /**304 * rename from one path to another305 *306 * @param string $path_from307 * @param string $path_to308 * @return bool309 * @author Benoit Aubuchon310 */311 public function rename($path_from, $path_to)312 {313 $srcRealPath = $this->resolvePath(vfsStream::path($path_from));314 $dstRealPath = vfsStream::path($path_to);315 $srcContent = $this->getContent($srcRealPath);316 if (null == $srcContent) {317 trigger_error(' No such file or directory', E_USER_WARNING);318 return false;319 }320 $dstContent = clone $srcContent;321 $dstNames = $this->splitPath($dstRealPath);322 // Renaming the filename323 $dstContent->rename($dstNames['basename']);324 // Copying to the destination325 $dstParentContent = $this->getContent($dstNames['dirname']);326 if (null == $dstParentContent) {327 trigger_error('No such file or directory', E_USER_WARNING);328 return false;329 }330 if ($dstParentContent->getType() !== vfsStreamContent::TYPE_DIR) {331 trigger_error('Target is not a directory', E_USER_WARNING);332 return false;333 }334 $dstParentContent->addChild($dstContent);335 // Removing the source336 return $this->unlink($path_from);337 }338 /**339 * creates a new directory340 *341 * @param string $path342 * @param int $mode343 * @param int $options344 * @return bool345 */346 public function mkdir($path, $mode, $options)347 {348 $mode = ((null == $mode) ? (0777) : ($mode));349 $path = vfsStream::path($path);350 if (null === self::$root) {351 self::$root = vfsStream::newDirectory($path, $mode);352 return true;353 }354 355 $maxDepth = count(explode('/', $path));356 $names = $this->splitPath($path);357 $newDirs = $names['basename'];358 $dir = null;359 $i = 0;360 while ($dir === null && $i < $maxDepth) {361 $dir = $this->getContent($names['dirname']);362 $names = $this->splitPath($names['dirname']);363 $newDirs = $names['basename'] . '/' . $newDirs;364 $i++;365 }366 367 if (null === $dir || $dir->getType() !== vfsStreamContent::TYPE_DIR) {368 return false;369 }370 371 $newDirs = str_replace($dir->getName() . '/', '', $newDirs);372 $recursive = ((STREAM_MKDIR_RECURSIVE & $options) !== 0) ? (true) : (false);373 if (strpos($newDirs, '/') !== false && false === $recursive) {374 return false;375 }376 vfsStream::newDirectory($newDirs, $mode)->at($dir);377 return true;378 }379 /**380 * removes a directory381 *382 * @param string $path383 * @param int $options384 * @return bool385 * @todo consider $options with STREAM_MKDIR_RECURSIVE386 */387 public function rmdir($path, $options)388 {389 $path = $this->resolvePath(vfsStream::path($path));390 $child = $this->getContentOfType($path, vfsStreamContent::TYPE_DIR);391 if (null === $child) {392 return false;393 }394 395 // can only remove empty directories396 if (count($child->getChildren()) > 0) {397 return false;398 }399 400 if (self::$root->getName() === $path) {401 // delete root? very brave. :)402 self::$root = null;403 clearstatcache();404 return true;405 }406 407 $names = $this->splitPath($path);408 $dir = $this->getContentOfType($names['dirname'], vfsStreamContent::TYPE_DIR);409 clearstatcache();410 return $dir->removeChild($child->getName());411 }412 /**413 * opens a directory414 *415 * @param string $path416 * @param int $options417 * @return bool418 */419 public function dir_opendir($path, $options)420 {421 $path = $this->resolvePath(vfsStream::path($path));422 $this->dir = $this->getContentOfType($path, vfsStreamContent::TYPE_DIR);423 if (null === $this->dir) {424 return false;425 }426 427 $this->dirIterator = $this->dir->getIterator();428 return true;429 }430 /**431 * reads directory contents432 *433 * @return string434 */435 public function dir_readdir()436 {...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with getContentOfType on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!