Code

< 40 %
40-60 %
> 60 %
1
<?php
2
/**
3
 * Module.php
4
 * @author Revin Roman http://phptime.ru
5
 */
6
7
namespace rmrevin\yii\module\File;
8
9
use yii\base\InvalidConfigException;
10
use yii\helpers\FileHelper;
11
12
/**
13
 * Class Module
14
 * @package rmrevin\yii\module\File
15
 */
16
class Module extends \yii\base\Module
17
{
18
19
    /** @var string */
20
    public $upload_alias = '@app/web/upload';
21
22
    /** @var string */
23
    public $upload_path = null;
24
25
    /** @var string */
26
    public $upload_web_alias = '/upload';
27
28
    /** @var string */
29
    public $upload_web_path = null;
30
31
    /** @var string */
32
    public $storage_alias = '@app/web/storage';
33
34
    /** @var string */
35
    public $storage_path = null;
36
37
    /** @var string */
38
    public $storage_web_alias = '/storage';
39
40
    /** @var string */
41
    public $storage_web_path = null;
42
43
    /** @var int */
44
    public $max_upload_file_size = 10; // megabytes
45
46
    /** @var string */
47
    public $no_image_alias = '@vendor/rmrevin/yii2-file/assets/no-image.png';
48
49
    /**
50
     * @inheritdoc
51
     */
52 15
    public function init()
53
    {
54 15
        parent::init();
55
56 15
        $this->checkIniSizeParam('upload_max_filesize');
57
58 15
        $this->checkIniSizeParam('post_max_size');
59
60 15
        $this->reinitAliases();
61
62 15
        if (empty($this->upload_path)) {
63 1
            throw new InvalidConfigException(\Yii::t('service-file', 'Unable to determine the path to the upload directory (FileService::$upload_path).'));
64
        }
65
66 15
        if (empty($this->storage_path)) {
67 1
            throw new InvalidConfigException(\Yii::t('service-file', 'Unable to determine the path to the storage directory (FileService::$storage_path).'));
68
        }
69
70 15
        $this->createDirectory($this->upload_path);
71
72 15
        $this->createDirectory($this->storage_path);
73
74 15
        if (!is_readable($this->upload_path)) {
75 1
            throw new \RuntimeException(\Yii::t('service-file', 'Upload directory not available for reading (FileService::$upload_path).'));
76
        }
77
78 15
        if (!is_writable($this->upload_path)) {
79 1
            throw new \RuntimeException(\Yii::t('service-file', 'Upload directory not available for writing (FileService::$upload_path).'));
80
        }
81
82 15
        if (!is_readable($this->storage_path)) {
83 1
            throw new \RuntimeException(\Yii::t('service-file', 'Storage directory not available for reading (FileService::$storage_path).'));
84
        }
85
86 15
        if (!is_writable($this->storage_path)) {
87 1
            throw new \RuntimeException(\Yii::t('service-file', 'Storage directory not available for writing (FileService::$storage_path).'));
88
        }
89 15
    }
90
91 15
    public function reinitAliases()
92
    {
93 15
        $this->storage_path = \Yii::getAlias($this->storage_alias);
94 15
        $this->storage_web_path = \Yii::getAlias($this->storage_web_alias);
95 15
        $this->upload_web_path = \Yii::getAlias($this->upload_web_alias);
96 15
        $this->upload_path = \Yii::getAlias($this->upload_alias);
97 15
    }
98
99
    /**
100
     * @return static
101
     */
102 12
    public static function module()
103
    {
104 12
        return \Yii::$app->getModule(static::MODULE);
105
    }
106
107
    /**
108
     * @param string $path
109
     */
110 15
    private function createDirectory($path)
111
    {
112 15
        if (!file_exists($path) || !is_dir($path)) {
113 15
            FileHelper::createDirectory($path);
114 15
        }
115 15
    }
116
117
    /**
118
     * @param string $param
119
     */
120 15
    private function checkIniSizeParam($param)
121
    {
122 15
        if ((int)ini_get($param) < $this->max_upload_file_size) {
123 15
            \Yii::warning(sprintf('The parameter `%s` in php.ini must be equal to`%s`', $param,
124 15
                $this->max_upload_file_size . 'M'), __METHOD__);
125 15
        }
126 15
    }
127
128
    const MODULE = 'file';
129
}