symfony /src/Symfony/Bundle/FrameworkBundle/Util/Filesystem.php

Language PHP Lines 226
MD5 Hash 84337933bef9d361118bab57facc59a2
Repository https://github.com/come/symfony.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php

namespace Symfony\Bundle\FrameworkBundle\Util;

/*
 * This file is part of the Symfony framework.
 *
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

/**
 * Provides basic utility to manipulate the file system.
 *
 * @package    Symfony
 * @subpackage Bundle_FrameworkBundle
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 */
class Filesystem
{
    /**
     * Copies a file.
     *
     * This method only copies the file if the origin file is newer than the target file.
     *
     * By default, if the target already exists, it is not overridden.
     *
     * To override existing files, pass the "override" option.
     *
     * @param string $originFile  The original filename
     * @param string $targetFile  The target filename
     * @param array  $options     An array of options
     */
    public function copy($originFile, $targetFile, $options = array())
    {
        if (!array_key_exists('override', $options)) {
            $options['override'] = false;
        }

        // we create target_dir if needed
        if (!is_dir(dirname($targetFile))) {
            $this->mkdirs(dirname($targetFile));
        }

        $mostRecent = false;
        if (file_exists($targetFile)) {
            $statTarget = stat($targetFile);
            $stat_origin = stat($originFile);
            $mostRecent = ($stat_origin['mtime'] > $statTarget['mtime']) ? true : false;
        }

        if ($options['override'] || !file_exists($targetFile) || $mostRecent) {
            copy($originFile, $targetFile);
        }
    }

    /**
     * Creates a directory recursively.
     *
     * @param  string $path  The directory path
     * @param  int    $mode  The directory mode
     *
     * @return bool true if the directory has been created, false otherwise
     */
    public function mkdirs($path, $mode = 0777)
    {
        if (is_dir($path)) {
            return true;
        }

        return @mkdir($path, $mode, true);
    }

    /**
     * Creates empty files.
     *
     * @param mixed $files  The filename, or an array of filenames
     */
    public function touch($files)
    {
        if (!is_array($files)) {
            $files = array($files);
        }

        foreach ($files as $file) {
            touch($file);
        }
    }

    /**
     * Removes files or directories.
     *
     * @param mixed $files  A filename or an array of files to remove
     */
    public function remove($files)
    {
        if (!is_array($files)) {
            $files = array($files);
        }

        $files = array_reverse($files);
        foreach ($files as $file) {
            if (!file_exists($file)) {
                continue;
            }

            if (is_dir($file) && !is_link($file)) {
                $fp = opendir($file);
                while (false !== $item = readdir($fp)) {
                    if (!in_array($item, array('.', '..'))) {
                        $this->remove($file.'/'.$item);
                    }
                }
                closedir($fp);

                rmdir($file);
            } else {
                unlink($file);
            }
        }
    }

    /**
     * Change mode for an array of files or directories.
     *
     * @param array   $files  An array of files or directories
     * @param integer $mode   The new mode
     * @param integer $umask  The mode mask (octal)
     */
    public function chmod($files, $mode, $umask = 0000)
    {
        $currentUmask = umask();
        umask($umask);

        if (!is_array($files)) {
            $files = array($files);
        }

        foreach ($files as $file) {
            chmod($file, $mode);
        }

        umask($currentUmask);
    }

    /**
     * Renames a file.
     *
     * @param string $origin  The origin filename
     * @param string $target  The new filename
     *
     * @throws \RuntimeException When target file already exists
     */
    public function rename($origin, $target)
    {
        // we check that target does not exist
        if (is_readable($target)) {
            throw new \RuntimeException(sprintf('Cannot rename because the target "%" already exist.', $target));
        }

        rename($origin, $target);
    }

    /**
     * Creates a symbolic link or copy a directory.
     *
     * @param string $originDir      The origin directory path
     * @param string $targetDir      The symbolic link name
     * @param bool   $copyOnWindows  Whether to copy files if on windows
     */
    public function symlink($originDir, $targetDir, $copyOnWindows = false)
    {
        if (!function_exists('symlink') && $copyOnWindows) {
            $this->mirror($originDir, $targetDir);

            return;
        }

        $ok = false;
        if (is_link($targetDir)) {
            if (readlink($targetDir) != $originDir) {
                unlink($targetDir);
            } else {
                $ok = true;
            }
        }

        if (!$ok) {
            symlink($originDir, $targetDir);
        }
    }

    /**
     * Mirrors a directory to another.
     *
     * @param string $originDir      The origin directory
     * @param string $targetDir      The target directory
     * @param \Traversable $iterator A Traversable instance
     * @param array  $options        An array of options (see copy())
     *
     * @throws \RuntimeException When file type is unknown
     */
    public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array())
    {
        if (null === $iterator) {
            $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
        }

        foreach ($iterator as $file) {
            $target = $targetDir.DIRECTORY_SEPARATOR.str_replace(realpath($originDir), '', $file->getRealPath());

            if (is_dir($file)) {
                $this->mkdirs($target);
            } else if (is_file($file)) {
                $this->copy($file, $target, $options);
            } else if (is_link($file)) {
                $this->symlink($file, $target);
            } else {
                throw new \RuntimeException(sprintf('Unable to guess "%s" file type.', $file));
            }
        }
    }
}
Back to Top