Completed
Push — master ( 541968...b54375 )
by Sergi Tur
02:51
created

Filesystem::createParentFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace Acacha\AdminLTETemplateLaravel\Filesystem;
4
5
/**
6
 * Class Filesystem.
7
 *
8
 * @package Acacha\Llum\Filesystem
9
 */
10
class Filesystem
11
{
12
13
    /**
14
     * Root directory
15
     *
16
     * @var string
17
     */
18
    protected $root;
19
20
    /**
21
     * @param null|string $root
22
     */
23
    public function __construct($root = '/')
24
    {
25
        $this->root = $root;
26
    }
27
28
    /**
29
     *
30
     * Overwrite file with provided content.
31
     *
32
     * @param $file
33
     * @param $content
34
     */
35
    public function overwrite($file, $content)
36
    {
37
        $this->put($this->getPath($file), $content);
38
    }
39
40
    /**
41
     * Create file with provided content if not exists.
42
     *
43
     * @param $file
44
     * @param $content
45
     * @param bool $recursive
46
     * @throws FileAlreadyExists
47
     */
48
    public function make($file, $content, $recursive = false)
49
    {
50
        if ($this->exists($this->getPath($file))) {
51
            throw new FileAlreadyExists;
52
        }
53
        $this->put($file, $content,0,$recursive);
54
    }
55
56
    /**
57
     * Get file contents.
58
     * @param $file
59
     * @return string
60
     * @throws FileDoesNotExists
61
     */
62
    public function get($file)
63
    {
64
        $path = $this->getPath($file);
65
66
        if (! file_exists($path)) {
67
            throw new FileDoesNotExists;
68
        }
69
70
        return file_get_contents($path);
71
    }
72
73
    /**
74
     * Put content in file.
75
     *
76
     * @param $file
77
     * @param $content
78
     * @param $flag
79
     * @param bool $recursive
80
     * @return int
81
     */
82
    public function put($file, $content, $flag = null, $recursive = false)
83
    {
84
        if ($recursive) $this->createParentFolder($file);
85
        return file_put_contents($this->getPath($file), $content, $flag);
86
    }
87
88
    /**
89
     * Create parent folder recursively (if not exists).
90
     *
91
     * @param $file
92
     * @return bool
93
     */
94
    public function createParentFolder($file) {
95
        if ( ! file_exists($folder = dirname($file))) return mkdir(dirname($file), 0775, true);
96
    }
97
98
    /**
99
     * Does the given file exist?
100
     *
101
     * @param $file
102
     * @return bool
103
     */
104
    public function exists($file)
105
    {
106
        return file_exists($this->getPath($file));
107
    }
108
109
    /**
110
     * Build the path to the file.
111
     *
112
     * @param $file
113
     * @return string
114
     */
115
    protected function getPath($file)
116
    {
117
        return $this->root . '/' . $file;
118
    }
119
120
    /**
121
     * Append to a file
122
     *
123
     * @param $file
124
     * @param $body
125
     * @return int
126
     */
127
    public function append($file, $body)
128
    {
129
        return $this->put($file, $body, FILE_APPEND);
130
    }
131
    /**
132
     * Delete a file
133
     *
134
     * @param $file
135
     */
136
    public function delete($file)
137
    {
138
        unlink($this->getPath($file));
139
    }
140
}
141