3

Short version: How to use custom db and module config files instead of env.php and config.php ? or How to completely overwrite \Magento\Framework\Config\File\ConfigFilePool ?

Long version:
I'm developing a multi client system similar to https://github.com/elisei/Magento-Multi_Client. I want to develop it in such a way that with one codebase, multiple databases can be installed. So multiple projects can be developed.

On code level following folders/files will be different for each project.

var/*
pub/*
app/etc/env.php 
app/etc/config.php 

Instead of above I'll be using following folders/files:

var/client1/*
var/client2/*
pub/client1/*
pub/client2/*
app/etc/cliet1_env.php 
app/etc/cliet2_env.php 
app/etc/client1_config.php 
app/etc/client2_config.php 

Based on the url used diferent database will be used (which will be done through setting host flags for each url). If url is client1.com , db used will use cliet1_env.php.

DB config file (env.php) which is used by default is mentioned in Magento\Framework\Config\File\ConfigFilePool, so I've overwritten this file in a custom module using preference. For clients1 here is the program.

di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\Config\File\ConfigFilePool" type="Aceturtle\MultiClient\Config\File\ConfigFilePool"/>
</config>

and overwritten file

<?php

namespace Aceturtle\MultiClient\Config\File;

class ConfigFilePool extends \Magento\Framework\Config\File\ConfigFilePool {
   const APP_CONFIG = 'app_config';
    const APP_ENV = 'app_env';

    /**
     * Default files for configuration
     *
     * @var array
     */
    private $applicationConfigFiles = [
        self::APP_CONFIG => 'client1_config.php',
        self::APP_ENV => 'client1_env.php',
    ];
    public function getPaths()
    {
        return $this->applicationConfigFiles;
    }
}

Call to \Magento\Framework\Config\File\ConfigFilePool::getPaths() is showing client1_env.php and client1_config.php but still, If I print \Magento\Framework\Config\File\ConfigFilePool object it prints client1_env.php, client1_config.php, and env.php, config.php and if I run any command (module enable), it updates config.php, not client1_config.php. That means \Magento\Framework\Config\File\ConfigFilePool is not being completely overwritten.

How I can configure it such that my custom config files are used instead of default ones?

  • It's private variables how can u override? – Ankit Shah May 12 '17 at 9:09
  • Why you are doing such a way. If you want to use separate DB for each website, why not create different magento setup for all. Hope you can understand. – Sunil Patel May 13 '17 at 7:36
  • I want to have a basic db config ready and just connect to it through above method. I do not want to create a new codebase instance again. Just modify module/theme according to my need. – amitshree May 13 '17 at 14:51
3
+50

Another option would be to use a Magento 2 plugin, for example, in a etc/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Config\File\ConfigFilePool">
        <plugin name="extendGetterPlugin" type="Aceturtle\MultiClient\Plugin\ExtendGetterPlugin" />
    </type>
</config>

Then under Plugin/ExtendGetterPlugin.php:

<?php

namespace Aceturtle\MultiClient\Plugin;

class ExtendGetterPlugin
{
    const APP_CONFIG = 'app_config';
    const APP_ENV = 'app_env';

    /**
     * Default files for configuration
     *
     * @var array
     */
    private $applicationConfigFiles = [
        self::APP_CONFIG => 'client1_config.php',
        self::APP_ENV => 'client1_env.php',
    ];

    public function afterGetPaths(\Magento\Framework\Config\File\ConfigFilePool $subject, $result)
    {
        return $this->applicationConfigFiles;
    }
}

This way, you will override the result of the getPaths method via plugin.

  • @Raphal, it's throwing [LogicException] Circular dependency: Magento\Framework\App\Cache\State depends on Magento\Framework\App\Cache\Type\AccessProxy and vice versa. – amitshree May 18 '17 at 10:19
  • @amitshree interesting do you have the full trace ? – Raphael at Digital Pianism May 18 '17 at 10:22
  • kobra.io/#/e/-KkQ6r7dNCTAlPVYe2hE – amitshree May 18 '17 at 10:35
  • @amitshree interesting. It could be because the plugin system relies on the config pool files. – Raphael at Digital Pianism May 18 '17 at 10:58
1

U cannot override private variables.

When we run

$ php bin/magento setup:upgrade
<pre>Array
(
    [dist] => Array
        (
            [app_config] => config.dist.php
            [app_env] => env.dist.php
        )

    [local] => Array
        (
            [app_config] => config.local.php
            [app_env] => env.local.php
        )

)

So u have to apply changes in

 public function getInitialFilePools()
    {
        echo "1<pre>";
        print_r($this->initialConfigFiles);
        return $this->initialConfigFiles;
    }

public function getPath($fileKey)
{
    echo 2; exit;
    return client1_env.php; // Give dynamic logic over here
}

U have to overwrite value in Array in getInitialFilePools function

  • Tried this. It's not working. – amitshree May 12 '17 at 14:36
  • In public function getPath($fileKey) { return 'config_1.php;' } & it works fine – Ankit Shah May 15 '17 at 2:05
  • here is the code I tried. kobra.io/#/e/-Kk9q2_JpDRR4pevG8Ui . Are you modifying core to test? – amitshree May 15 '17 at 6:45
  • r u still facing issue? @amitshree – Ankit Shah May 18 '17 at 1:43
  • Yes. Kindly check my code shared above. – amitshree May 18 '17 at 3:03
1

We also ran into a similar issue. We wanted to rewrite some files that used in setup upgrade to optimize the deploy process. After some investigation, we found that these CLI commands are implemented independently of Magento using Symfony. It does not follow usual Magento execution cycle.

As a solution, we have created a module/script using the same method they have used in setup command namespace. The purpose of this module is to patch core files. The module can be used as composer hook or Magento CLI command.

You can find the module here. https://github.com/bgkavinga/magento2-patches

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.