Separate configuration paths for Cli_Project and Web_Project (same application)

103 views
Skip to first unread message

Igal Alkon

unread,
Nov 19, 2014, 7:15:38 AM11/19/14
to aur...@googlegroups.com

Separate configuration paths for Cli_Project and Web_Project (same application)

A week ago I had an issue when I wanted to load the configurations for my Cli_Project from a different configuration path than config/
I wanted the application to work this way, because my CLI commands do not require a request object for example, or ACL, and probably lost of other components in the future as I develop it.

Looking inside composer.json you will find found this line:
"Aura\\Web_Project\\_Config\\": "config/"

So the trivial thing to do was adding another configuration similar to this:
"Aura\\Cli_Project\\_Config\\": "config/cli/"

This did not work well for me, and Cli_Project was still loading Common from config/
Than Harikt send me couple of links on IRC, leading me making another change in composer.json, finally it was looking like this:

"autoload": {
    "psr-4": {
        "": "src/",
        "Aura\\Framework_Project\\_Config\\": "config/",
        "Aura\\Cli_Project\\_Config\\": "config/cli/",
        "Aura\\Web_Project\\_Config\\": "config/web/",
    }
},
"extra": {
    "aura": {
        "type": "project",
        "config": {
            "common": [
                "Aura\\Framework_Project\\_Config\\Common",
                "Aura\\Cli_Project\\_Config\\Common",
                "Aura\\Web_Project\\_Config\\Common"
            ],
            "dev": [
                "Aura\\Framework_Project\\_Config\\Dev",
                "Aura\\Cli_Project\\_Config\\Dev",
                "Aura\\Web_Project\\_Config\\Dev"
            ],
            "test": [
                "Aura\\Framework_Project\\_Config\\Test",
                "Aura\\Cli_Project\\_Config\\Test",
                "Aura\\Web_Project\\_Config\\Test"
            ],
            "prod": [
                "Aura\\Framework_Project\\_Config\\Prod",
                "Aura\\Cli_Project\\_Config\\Prod",
                "Aura\\Web_Project\\_Config\\Prod"
            ]
        }
    }
}

Notice the "config" property, all it's child properties (common,dev,test,prod) can receive an array instead of a string.
Allowing to supply multiple configuration paths per type.
In this manner, framework configuration loads first, then other configurations.

I still have one issue with this method, had to add this lines to _env.php, web/Common.php, cli/Common.php:

_env.php

$_ENV['AURA_SAPI_NAME'] = php_sapi_name();

web/Common.php

public function define(Container $di) {
    if($_ENV['AURA_SAPI_NAME'] === 'cli') {
        return;
    }
    ...
}

public function modify(Container $di) {
    if($_ENV['AURA_SAPI_NAME'] === 'cli') {
        return;
    }
    ...
}

cli/Common.php

public function define(Container $di) {
    if($_ENV['AURA_SAPI_NAME'] !== 'cli') {
        return;
    }
    ...
}

public function modify(Container $di)
{
    if($_ENV['AURA_SAPI_NAME'] !== 'cli') {
        return;
    }
    ...
}

Did this because I couldn't make so the web/* configurations would not load at all, when in CLI mode.
If anyone have solution post!

Hari K T

unread,
Nov 19, 2014, 7:31:45 AM11/19/14
to aur...@googlegroups.com
Yes I agree with you the configuration is what may be needed.

I hope you have namespace'd the Web_Project and Cli_Project for configuration and not using Aura.Web_Project and Aura.Cli_Project and only using Aura.Framework_Project .

--
You received this message because you are subscribed to the Google Groups "The Aura Project for PHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to auraphp+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Igal Alkon

unread,
Nov 19, 2014, 8:44:22 AM11/19/14
to aur...@googlegroups.com
On Wednesday, November 19, 2014 2:31:45 PM UTC+2, Hari K T wrote:
I hope you have namespace'd the Web_Project and Cli_Project for configuration and not using Aura.Web_Project and Aura.Cli_Project and only using Aura.Framework_Project.

I'm not sure what you mean, in both cases, the framework configuration loads (config/Common.php).
I do not extend Web_Project, Cli_Project into my own namespace.
care to explain?

I think the "hack" in modify() and define() is not looking so good, I'm trying to think of another nicer solution.
What is missing for me some kind of method to tell the framework load both cli/Common.php and web/Common.php.
But if cli/Common.php was loaded first (because first the Cli_Project is needed first when I run console.php) then  web/Common.php would not be loaded.

Hari K T

unread,
Nov 19, 2014, 9:46:11 AM11/19/14
to aur...@googlegroups.com
Hi,

I hope you have namespace'd the Web_Project and Cli_Project for configuration and not using Aura.Web_Project and Aura.Cli_Project and only using Aura.Framework_Project.

I'm not sure what you mean, in both cases, the framework configuration loads (config/Common.php).
I do not extend Web_Project, Cli_Project into my own namespace.
care to explain?

If you are using a Aura.Framework project, both cli and web will be having a config/Common.php with namespace Aura\Framework_Project\_Config\Common .

So when I noticed your namespace as Aura\Web_Project and Aura\Cli_Project I was just wondering whether you have only changed the namespace or using some other ways.

I think the "hack" in modify() and define() is not looking so good, I'm trying to think of another nicer solution.

As of your configuration I am not sure what it is lagging, for the web projects can load the web and cli can load the cli. 
 
What is missing for me some kind of method to tell the framework load both cli/Common.php and web/Common.php.

If you need both cli and web to get loaded I suggest adding it on the config/Common.php so that will be 

namespace Aura\Framework_Project

class Common implements Container ?
{
   define and modify for the common case.
}
 
But if cli/Common.php was loaded first (because first the Cli_Project is needed first when I run console.php) then  web/Common.php would not be loaded.

In that case you need to change the ordering of the array in json, so it can load better I guess.

So something like 

Aura\Framework_Project\_Config\Common
Aura\Web_Project\_Config\Common
Aura\Cli_Project\_Config\Common

Does that helps ?

Igal Alkon

unread,
Nov 19, 2014, 11:01:28 AM11/19/14
to aur...@googlegroups.com


On Wednesday, November 19, 2014 4:46:11 PM UTC+2, Hari K T wrote:

I hope you have namespace'd the Web_Project and Cli_Project for configuration and not using Aura.Web_Project and Aura.Cli_Project and only using Aura.Framework_Project.

I'm not sure what you mean, in both cases, the framework configuration loads (config/Common.php).
I do not extend Web_Project, Cli_Project into my own namespace.
care to explain?

If you are using a Aura.Framework project, both cli and web will be having a config/Common.php with namespace Aura\Framework_Project\_Config\Common .
So when I noticed your namespace as Aura\Web_Project and Aura\Cli_Project I was just wondering whether you have only changed the namespace or using some other ways.

Yes, I did split the CLI and Web configurations, what you will find combined in the framework project.
 

I think the "hack" in modify() and define() is not looking so good, I'm trying to think of another nicer solution.

As of your configuration I am not sure what it is lagging, for the web projects can load the web and cli can load the cli. 

This what I want to happen, but this is not what actually happens.
Both config/cli/Common.php and config/web/Common.php are automatically loaded once they are defined in "config": { ... } (composer.json)
 
 
What is missing for me some kind of method to tell the framework load both cli/Common.php and web/Common.php.

If you need both cli and web to get loaded I suggest adding it on the config/Common.php so that will be 

namespace Aura\Framework_Project

class Common implements Container ?
{
   define and modify for the common case.
}
 
But if cli/Common.php was loaded first (because first the Cli_Project is needed first when I run console.php) then  web/Common.php would not be loaded.

In that case you need to change the ordering of the array in json, so it can load better I guess.

So something like 

Aura\Framework_Project\_Config\Common
Aura\Web_Project\_Config\Common
Aura\Cli_Project\_Config\Common

Does that helps ?

Unfortunately I tried this also.
In your example Aura\Web_Project\_Config\Common would indeed load before Cli configuration, but they both will load and cause the same problem.
What I want to happen is that, Aura\Framework_Project\_Config\Common will load first, it defines couple of general stuff for the whole project,
then Aura\Web_Project\_Config\Common will load if I'm coming from a web request, and Aura\Cli_Project\_Config\Common will load if I'm executing cli/console.php

I also tried something like:

"common": [
    "Aura\\Framework_Project\\_Config\\Common",
    "Aura\\Web_Project\\_Config\\Common"
]

"cli_common": [
    "Aura\\Framework_Project\\_Config\\Common",
    "Aura\\Cli_Project\\_Config\\Common"
]

...

"dev": [
    "Aura\\Framework_Project\\_Config\\Dev",
    "Aura\\Web_Project\\_Config\\Dev"
]

"cli_dev": [
    "Aura\\Framework_Project\\_Config\\Dev",
    "Aura\\Cli_Project\\_Config\\Dev"
]

when in _env.php set the right configuration:
(php_sapi_name() === 'cli') ? 'cli_dev' : 'dev'

This did not work either. It seems that even if the "cli_dev" selected, something is still causing "common" to load, it might be because how the configuration inherits. 


Hari K T

unread,
Nov 19, 2014, 11:15:17 AM11/19/14
to aur...@googlegroups.com
Could you give me a gist . So I can try and get back to you ?
Reply all
Reply to author
Forward
Message has been deleted
0 new messages