|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Mtf\Util\Filesystem; |
| 7 | + |
| 8 | +/** |
| 9 | + * Filesystem helper. |
| 10 | + */ |
| 11 | +class FileHelper |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Normalizes a file/directory path. |
| 15 | + * |
| 16 | + * @param string $path |
| 17 | + * @param string $ds |
| 18 | + * @return string |
| 19 | + */ |
| 20 | + public function normalizePath($path, $ds = DIRECTORY_SEPARATOR) |
| 21 | + { |
| 22 | + $path = rtrim(strtr($path, '/\\', $ds . $ds), $ds); |
| 23 | + if (strpos($ds . $path, "{$ds}.") === false && strpos($path, "{$ds}{$ds}") === false) { |
| 24 | + return $path; |
| 25 | + } |
| 26 | + |
| 27 | + return $this->realpath($ds, $path); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns canonicalized pathname. |
| 32 | + * |
| 33 | + * @param string $ds |
| 34 | + * @param string $path |
| 35 | + * @return string |
| 36 | + */ |
| 37 | + private function realpath($ds, $path) |
| 38 | + { |
| 39 | + $parts = []; |
| 40 | + foreach (explode($ds, $path) as $part) { |
| 41 | + if ($part === '..' && !empty($parts) && end($parts) !== '..') { |
| 42 | + array_pop($parts); |
| 43 | + } elseif ($part === '.' || $part === '' && !empty($parts)) { |
| 44 | + continue; |
| 45 | + } else { |
| 46 | + $parts[] = $part; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + $path = implode($ds, $parts); |
| 51 | + |
| 52 | + return $path === '' ? '.' : $path; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a new directory. |
| 57 | + * |
| 58 | + * @param string $path |
| 59 | + * @param int $mode |
| 60 | + * @param bool $recursive |
| 61 | + * @return bool |
| 62 | + * @throws \Exception |
| 63 | + */ |
| 64 | + public function createDirectory($path, $mode = 0775, $recursive = true) |
| 65 | + { |
| 66 | + if (is_dir($path)) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + $parentDir = dirname($path); |
| 70 | + |
| 71 | + if ($recursive && !is_dir($parentDir) && $parentDir !== $path) { |
| 72 | + $this->createDirectory($parentDir, $mode, true); |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + if (!mkdir($path, $mode)) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + } catch (\Exception $e) { |
| 80 | + if (!is_dir($path)) { |
| 81 | + throw new \Exception("Failed to create directory \"$path\""); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + return chmod($path, $mode); |
| 87 | + } catch (\Exception $e) { |
| 88 | + throw new \Exception("Failed to change permissions for directory \"$path\""); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Create a new file with content. |
| 94 | + * |
| 95 | + * @param string $filename |
| 96 | + * @param string $content |
| 97 | + * @return bool |
| 98 | + */ |
| 99 | + public function createFile($filename, $content) |
| 100 | + { |
| 101 | + return file_put_contents($filename, $content) !== false; |
| 102 | + } |
| 103 | +} |
0 commit comments