yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.91k forks source link

AssetManager: Creation of basepath directory #7285

Closed dynasource closed 7 years ago

dynasource commented 9 years ago

The current implementation of the AssetManager returns an error when the basepath of the configured Assetcomponent is not yet created.

https://github.com/yiisoft/yii2/blob/master/framework/web/AssetManager.php#L175

To me this was already a rigid exception in the past, but since I am working with a temporary disk in memory, it confronts me with a bigger problem then just being an extra step.

Is there a security or control reason behind it? If so, I would advocate an extra setting to define if the basePath should be created automaticall. Otherwise I would advocate to generate the basepath as well as default.

thanks!

dynasource commented 9 years ago

by the way, Yii2 is not so rigid about the runtime folder which does get created automatically.

qiangxue commented 9 years ago

The Web server process normally doesn't have permission to create assets and runtime directories. So it's not a good idea to automatically create it because it will still fail.

dynasource commented 9 years ago

a few things: 1) you are looking at it from a production environment, we also have our development environment in which I use a ram disk 2) if it is not a good idea, why does it work for runtime? 3) isnt it up to the user to define if it should be automatically created? These are temporary folders. It is quite understandable that these are rendered on temporary disks?

qiangxue commented 9 years ago

These folders are supposed to be created already if you are using the app templates. Even if you are working in a team environment from scratch, it shouldn't involve much effort to figure out that you should have these folders ready beforehand. It's a waste of execution time to check the existence of these folders, especially they are on the critical path and involve file operations.

dynasource commented 9 years ago

It's a waste of execution time to check the existence of these folders

it is exactly what is happening already https://github.com/yiisoft/yii2/blob/master/framework/web/AssetManager.php#L175:

 public function init()
    {
        parent::init();
        $this->basePath = Yii::getAlias($this->basePath);
        if (!is_dir($this->basePath)) {
            throw new InvalidConfigException("The directory does not exist: {$this->basePath}");
        } elseif (!is_writable($this->basePath)) {
            throw new InvalidConfigException("The directory is not writable by the Web process: {$this->basePath}");
        } else {
            $this->basePath = realpath($this->basePath);
        }
        $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
    }

it shouldn't involve much effort to figure out that you should have these folders ready beforehand.

there are scenarios in which these folders are not there at forehand, like in my case with a continious reset on a ramdisk

klimov-paul commented 9 years ago

why does it work for runtime?

Yii does not create 'runtime' directory explicitly, however yii\log\FileTarget will do so while writing the log file.

Actually these checks inside AssetManager do not make much sense, because 'asset' directory will also be created implicitly while publishing assets. However, if such creation fails due to lack of permissions, nothing will tell you that. It will like your assets are not published by unknown reason.

dynasource commented 7 years ago

Ive created a RamDisk component which solved this issue for me.

SilverFire commented 7 years ago

Can you leave a link for other developers, if this extension is open-sourced?

dynasource commented 7 years ago

its not open source (yet). Its for a rare usecase and primarily created for serving files during local parallel testing for maximum speed. People can of course vote to have this released in open source. Then I will make time to do that.

strtob commented 2 years ago

Food for thoughts: Could be useful to add an additional check in init() if the folder "app/web/assets" exists or could be created. In my case I've cloned my project and the "app/web/assets" foldern wasn't created, yii2 told me: "The directory does not exist: " (without named a directory). It have taken time to find out, that the assets folder is missing.