FileService::mkDir()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: onysko
5
 * Date: 19.11.2014
6
 * Time: 19:04
7
 */
8
namespace samsonphp\fs;
9
10
use samson\core\CompressableService;
11
use samsonframework\core\ConfigurableInterface;
12
use samsonphp\event\Event;
13
14
/**
15
 * File system module controller
16
 * @package samsonphp\fs
17
 */
18
class FileService extends CompressableService implements IFileSystem, ConfigurableInterface
0 ignored issues
show
Deprecated Code introduced by
The class samson\core\CompressableService has been deprecated with message: Just implement samsonframework\core\CompressInterface

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19
{
20
    /** @var string Module identifier */
21
    protected $id = 'fs';
22
23
    /** @var string Configurable file service class name */
24
    public $fileServiceClassName = 'samsonphp\fs\LocalFileService';
25
26
    /** @var array Collection of configuration parameters */
27
    public $configuration = array();
28
29
    /** @var \samsonphp\fs\AbstractFileService Pointer to file system adapter */
30
    protected $fileService;
31
32
    /**
33
     * Initialize module
34
     * @param array $params Collection of module parameters
35
     * @return bool True if module successfully initialized
36
     */
37
    public function init(array $params = array())
38
    {
39
        if (!$this->loadExternalService($this->fileServiceClassName)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
40
41
            // Signal error
42
            Event::fire(
43
                'error',
44
                array(
45
                    $this,
46
                    'Cannot initialize file system adapter[' . $this->fileServiceClassName . ']'
47
                )
48
            );
49
        }
50
51
        // Call parent initialization
52
        return parent::init($params);
53
    }
54
55
    /**
56
     * Load external file service instance
57
     * @param string $serviceClassName File service class name for loading
58
     * @return bool True if external service instance has been created
59
     */
60
    public function loadExternalService($serviceClassName)
61
    {
62
        // If defined file service is supported
63
        if (class_exists($serviceClassName)) {
64
            /** @var \samsonphp\fs\AbstractFileService Create file service instance */
65
            $this->fileService = new $serviceClassName();
66
67
            // Set nested file service instance parameters
68
            foreach ($this->configuration as $key => $value) {
69
                $this->fileService->$key = $value;
70
            }
71
72
            // Initialize file service
73
            $this->fileService->initialize();
74
75
            return true;
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * @param mixed $entityConfiguration current instance for configuration
83
     * @return boolean False if something went wrong otherwise true
84
     */
85
    public function configure($entityConfiguration)
86
    {
87
        // Convert object to array
88
        $this->configuration = (array)$entityConfiguration;
89
        // Set fileServiceClassName parameter from config
90
        if (!empty($this->configuration['fileServiceClassName']{0})) {
91
	        $this->fileServiceClassName = $this->configuration['fileServiceClassName'];
92
        }
93
    }
94
95
    /**
96
     * Write data to a specific relative location
97
     *
98
     * @param mixed $data Data to be written
99
     * @param string $filename File name
100
     * @param string $uploadDir Relative file path
101
     * @return string|boolean Relative path to created file, false if there were errors
102
     */
103
    public function write($data, $filename = '', $uploadDir = '')
104
    {
105
        return $this->fileService->write($data, $filename, $uploadDir);
106
    }
107
108
    /**
109
     * Check existing current file in current file system
110
     * @param $filename string Filename
111
     * @return boolean File exists or not
112
     */
113
    public function exists($filename)
114
    {
115
        return $this->fileService->exists($filename);
116
    }
117
118
    /**
119
     * Read the file from current file system
120
     * @param $filePath string Full path to file
121
     * @param $filename string File name
122
     * @return string File data
123
     */
124
    public function read($filePath, $filename = null)
125
    {
126
        return $this->fileService->read($filePath, $filename);
127
    }
128
129
    /**
130
     * Delete file from current file system
131
     * @param $filename string File for deleting
132
     * @return mixed
133
     */
134
    public function delete($filename)
135
    {
136
        return $this->fileService->delete($filename);
137
    }
138
139
    /**
140
     * Get file extension in current file system
141
     * @param $filePath string Path
142
     * @return string|bool false if extension not found, otherwise file extension
143
     */
144
    public function extension($filePath)
145
    {
146
        return $this->fileService->extension($filePath);
147
    }
148
149
    /**
150
     * Define if $filePath is directory
151
     * @param string $filePath Path
152
     * @return boolean Is $path a directory or not
153
     */
154
    public function isDir($filePath)
155
    {
156
        return $this->fileService->isDir($filePath);
157
    }
158
159
    /**
160
     * Get recursive $path listing collection
161
     * @param string $path Path for listing contents
162
     * @param array $restrict Collection of restricted paths
163
     * @param array $result   Collection of restricted paths
164
     * @return array $path recursive directory listing
165
     */
166
    public function dir($path, $restrict = array(), & $result = array())
167
    {
168
        return $this->fileService->dir($path, $restrict, $result);
169
    }
170
171
    /**
172
     * Get file mime type in current file system
173
     * @param $filePath string Path to file
174
     * @return false|integer|string false if mime not found, otherwise file mime type
175
     */
176
    public function mime($filePath)
177
    {
178
        return $this->fileService->mime($filePath);
179
    }
180
181
    /**
182
     * Get relative path from $path
183
     * @param string $fullPath Full file path
184
     * @param string $fileName File name
185
     * @param string $basePath Base path, must end WITHOUT '/', if not passed
186
     *                          $fullPath one level top directory is used.
187
     * @return string Relative path to file
188
     */
189
    public function relativePath($fullPath, $fileName, $basePath = null)
190
    {
191
        return $this->fileService->relativePath($fullPath, $fileName, $basePath);
192
    }
193
194
    /**
195
     * Copy file/folder to selected location.
196
     * Copy can be performed from file($filePath) to file($newPath),
197
     * also copy can be performed from folder($filePath) to folder($newPath),
198
     * currently copying from file($filePath) to folder($newPath) is not supported.
199
     *
200
     * @param string $filePath      Source path or file path
201
     * @param string $newPath       New path or file path
202
     * @return boolean False if failed otherwise true if file/folder has been copied
203
     */
204
    public function copyPath($filePath, $newPath)
205
    {
206
        return $this->fileService->copyPath($filePath, $newPath);
207
    }
208
209
    /**
210
     * Create catalog in selected location
211
     * @param string    $path   Path for new catalog
212
     * @return boolean  Result of catalog creating
213
     */
214
    public function mkDir($path)
215
    {
216
        return $this->fileService->mkDir($path);
217
    }
218
219
    /** Обработчик сериализации объекта */
220
    public function __sleep()
221
    {
222
        // Remove all unnecessary fields from serialization
223
        return array_diff(array_keys(get_object_vars($this)), array('fileService'));
224
    }
225
}
226