cancel
Showing results for 
Search instead for 
Did you mean: 

Override Magento\Framework\Setup\FilePermissions

Override Magento\Framework\Setup\FilePermissions

Hello,

how can I override the class \Magento\Framework\Setup\FilePermissions?
Ive tried overriding setup/config/application.config.php and setup/config/di.config.php but it is not working.

 

di.config.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

return [
    'di' => [
        'allowed_controllers' => [
            /* ... */
        ],
        'instance' => [
            'preference' => [
                \Zend\EventManager\EventManagerInterface::class => 'EventManager',
                \Zend\ServiceManager\ServiceLocatorInterface::class => \Zend\ServiceManager\ServiceManager::class,
                \Magento\Framework\DB\LoggerInterface::class => \Magento\Framework\DB\Logger\Quiet::class,
                \Magento\Framework\Locale\ConfigInterface::class => \Magento\Framework\Locale\Config::class,
                \Magento\Framework\Filesystem\DriverInterface::class =>
                    \Magento\Framework\Filesystem\Driver\File::class,
                \Magento\Framework\Component\ComponentRegistrarInterface::class =>
                    \Magento\Framework\Component\ComponentRegistrar::class,
                '\Magento\Framework\Setup\FilePermissions' => 'Vendor\Performance\Setup\FilePermissions'
            ],
        ],
    ],
];

 

application.config.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

use Magento\Setup\Mvc\Bootstrap\InitParamListener;
use Zend\Mvc\Service\DiAbstractServiceFactoryFactory;
use Zend\ServiceManager\Di\DiAbstractServiceFactory;

return [
    'modules' => [
        'Magento\Setup',
    ],
    'module_listener_options' => [
        'module_paths' => [
            __DIR__ . '/../src',
        ],
        'config_glob_paths' => [
            __DIR__ . '/autoload/{,*.}{global,local}.php',
        ],
    ],
    'listeners' => [
        InitParamListener::class
    ],
    'service_manager' => [
        'factories' => [
            DiAbstractServiceFactory::class => DiAbstractServiceFactoryFactory::class,
            InitParamListener::BOOTSTRAP_PARAM => InitParamListener::class,
        ],
        'aliases' => [
            'magentoframeworksetupfilepermissions' => 'Vendor\Performance\Setup\FilePermissions'
        ]
    ],
    // list of Magento specific required services, like default abstract factory
    'required_services' => [
        DiAbstractServiceFactory::class
    ]
];

 

3 REPLIES 3

Re: Override Magento\Framework\Setup\FilePermissions

Hello @Crtl,

 

Please check below example, How I override it.

New Company\Performance\Setup\FilePermissions class extends existing Magento\Framework\Setup\FilePermissions class and overrides getInstallationWritableDirectories() method. In this method we are going to check whether path of the directory which should be writable is a symlink or real. In case it is a symlink, we suppose it is an EFS storage and we simply exclude the directory from further recursive is_writable() check.

 

Assuming that’s only Media directory is stored under EFS we are going to skip only 1 out of 4 directories. The static files however, can also be placed under EFS, but I won’t recommend doing this.

 

Here is our Company\Performance\Setup\FilePermissions class.

 

namespace Pronko\Performance\Setup;
 
use Magento\Framework\Setup\FilePermissions as MagentoFilePermissions;
 
class FilePermissions extends MagentoFilePermissions
{
    /**
     * Retrieve list of required writable directories for installation
     * Exclude symlinks from isWritable recursive checks
     *
     * @return array
     */
    public function getInstallationWritableDirectories()
    {
        parent::getInstallationWritableDirectories();
        foreach ($this->installationWritableDirectories as $code => $path) {
            if (is_link($path)) {
                unset($this->installationWritableDirectories[$code]);
            }
        }
        return $this->installationCurrentWritableDirectories;
    }
}

Next step is to add a preference for our the Magento\Framework\Setup\FilePermissions in di configuration. Since setup code is a different application with Symphony configuration, there is no way we can simply use our own di.xml configuration file to add preference as we always do. Having said, we have to modify <magento_root>/setup/config/di.config.php file and add our new preference:

return [
    'di' => [
        'instance' => [
            'preference' => [
                'Magento\Framework\Setup\FilePermissions' => 'Company\Performance\Setup\FilePermissions'
            ],
        ],
    ],
];


*There are other references listed in the original file, which I excluded here in the example.

Also, we have to modify additional configuration file which stores aliases for Setup Application. Remember, we are talking about Symphony application – Setup. Here are changes to the <magento_root>/setup/config/application.config.php file.

use Magento\Setup\Mvc\Bootstrap\InitParamListener;
 
return [
    'modules' => [
        'Magento\Setup',
    ],
    'module_listener_options' => [
        'module_paths' => [
            __DIR__ . '/../src',
        ],
        'config_glob_paths' => [
            __DIR__ . '/autoload/{,*.}{global,local}.php',
        ],
    ],
    'listeners' => [
        'Magento\Setup\Mvc\Bootstrap\InitParamListener'
    ],
    'service_manager' => [
        'factories' => [
            InitParamListener::BOOTSTRAP_PARAM => 'Magento\Setup\Mvc\Bootstrap\InitParamListener'
        ],
        'aliases' => [
            'magentoframeworksetupfilepermissions' => 'Company\Performance\Setup\FilePermissions',
        ]
    ],
];


Next time you will run bin/magento setup:upgrade command your new version of the code as well as database will be updated almost instantly. Magento won’t be checking permissions of the pub/media directory which is in reality a symlink to a location stored in Elastic File System storage.

 

--
If my answer is useful, please Accept as Solution & give Kudos

Re: Override Magento\Framework\Setup\FilePermissions

Hi!

This works on Magento 2.3?

I follow the steps and it doesn't work.

The method "getInstallationWritableDirectories" is never called when doing an upgrade.

Re: Override Magento\Framework\Setup\FilePermissions

I have used your approach to rewrite 

Magento\Setup\Console\Command\DeployStaticContentCommand

class and it works!!! Huge thanks to you!!!