2amigos / yiinitializr

Library that will help boost your application installation with ease and also to run Yii applications from its bootstrap files on a much cleaner way that the framework currently proposes.
Other
44 stars 17 forks source link

Further suggestions for configureability #4

Closed mdeweerd closed 6 years ago

mdeweerd commented 11 years ago

"Moved" from Yii extension

Could you consider adding:

Url/Server dependent configuration; Debug configuration based on Url option? I currently do stuff like this in my 'index.php':

// Determine debug mode depending on site name or get parameter (secret_dbg_key in this example):

$prodtest=(preg_match('/^expressiontocomparewith?$/',getenv('HTTP_HOST'))>0);

$debugmode=!$prodtest&&(preg_match('/^localhost([:\/].*)?
$/',getenv('HTTP_HOST'))>0)||isset($_GET['secret_dbg_key']);

$isProduction = $prodtest||(preg_match('/^([^\/].*)?prodsite.com/',getenv('HTTP_HOST'))>0);
if($debugmode) {
    defined('YII_DEBUG') or define('YII_DEBUG',true);
    defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',5);
}

and similar for the configuration where I have

if(!CliCheck::isCli()) {
    // Could be: $host = $_SERVER['HTTP_HOST'];
    // I used in my setup:
    $host = getenv('SERVER_NAME');
} else {
    $host=gethostname();
}
/** Check if there is a site configuration */
$siteconf = dirname(__FILE__).DIRECTORY_SEPARATOR.$host.'_common.php';
if(file_exists($siteconf)) {
    $siteconf=require($siteconf);
} else {
    $siteconf=array();
}

which loads a configuration file based on the host name. "CliCheck::isCli()" checks if it is run from the command line or not. '$siteconf' gets merged with the default/common configuration.

tonydspaniard commented 11 years ago

We could brainstorm to figure out what is the best way to actually add this to the main configuration file... I believe is a good idea but didn't think yet on the best approach.

I would like to get rid of all those configuration additions who make the bootstrap index.php file so unreadable, we need to think to implement them within Yiinitializr structure. What are your thoughts?

mdeweerd commented 11 years ago

Ok, I have to better "imagin" Yiinitializr.

While we are at it, we can also think about:

if($debugmode) {
    if(isset($_GET['secretdebugkey'])) {
        $config['components']['log']['routes']['xweb']['allowedIPs']=array( isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:'127.0.0.1');
    }
}
mdeweerd commented 11 years ago
mdeweerd commented 11 years ago

Observations:

The file REGEX would be: ({$machine})?({$partialdomain}(@{$relpath}))?({$port})({$user})?({$servertype}_)?({$contexttype}).php

Theme specific configuration is in the theme path with same rule.

So, valid names, lowest precedence first, for machine 'server1', url 'www.github.com/2amigos', user 'mdeweerd' (I did not mention port in the example, I added it later), config.php [default file first] github.com_config.php [url before machine] server1_com@2amigos_config.php [machine before user, after Url] server1_github.com@2amigos_config.php server1_github.com github.com_mdeweerd_config.php [user after server only] server1_mdeweerd_config.php
server1_com_mdeweerd_config.php server1_github.com_mdeweerd_config.php

Implementation: get list of files in configuration directory matching regular expression using known machine properties. Then order the configuration files, then "merge" them with the required precedence.

In 'config.php', we could allow a special entry like this with the same naming rules: array( 'configs'=>array( 'github.comconfig'=>array(/* config /), 'server1_mdeweerdconfig'=>array(/ config *), ) );

Yiinitializr removes 'config' from the array to keep compatibility with 'Yii'. File 'server1_mdeweerd_config.php' settings have priority over 'server1_mdeweerd_config' array.

-> this allows flexibility in configuration.

So: globals.php XXXX_XXX_globals.php

with arrays: array( 'globals'=>array('YII_DEBUG'=>array('expression'=>'isset($_GET['specialdebugvar'])?1:0'),), );

tonydspaniard commented 11 years ago

I am for:

tonydspaniard commented 11 years ago

I disagree with it, Yiinitializr works with different configurations and settings (check http://yiinitializr.2amigos.us different templates and configurations). The example you provide is not compatible with current solution provided with Yiinitializr as Yii is another package downloaded with composer, it is part of the settings you need to configure.

tonydspaniard commented 11 years ago

If we do regex, wouldn't that increase complexity to the newbies?

mdeweerd commented 11 years ago

What I meant is that in the configuration structure, Yiinitializr itself should be configured in only one way. That is: there is only one configuration for Yiinitializr to know where to find the other configuration files.

mdeweerd commented 11 years ago

If we do regex, this is inside Yiinitializer. Newbie only has to respect the order of naming the configuration file or option. He only writes:

server1_mdeweerd_config.php

The regex is hidden.

tonydspaniard commented 11 years ago

Can we do that through one config file only? I meant through the settings of the configuration file of Yiinitializr?

Edit: I think we should do some kind of proposal (gist maybe?) Some of the ideas here I like them a lot

mdeweerd commented 11 years ago

We can allow it in one config file only too - we should be flexible. If you add 'configs' to the configuration file of Yiinitializr, this is possible.

array(
'configs'=>array(
    'default'=>array(
        'yii'=>'yiipath',
     ),
    'server1_mdeweerd'=>array(
        'yii'=>'otheryiipath',
        <restOfConfig>,
    ),
    'domain'=>array(
        'globals'=>array( 'YII_DEBUG'=>etc),
       <restOfConfig>,
   )
),
'Yiinitializr'=>....
)
mdeweerd commented 11 years ago

Let's try Gist ;-).

mdeweerd commented 11 years ago

I started a gist.

https://gist.github.com/mdeweerd/5475262

mdeweerd commented 11 years ago

Ok, I think I identified an important misunderstanding on my behalf: Yiinitiliazr sets up the project structure and does not serve project initialisation itself.

Waht I was talking about (in part based on your goal of getting rid of the configurations in 'index.php'), is the configuration of the Yii framework after is has been 'built'/'set up'. However, this is not incompatible as Yiinitializr should set up the flexible dynamic application configuration structure with the 'index.php' that supports it.

mdeweerd commented 11 years ago

Anyway, if this discussion spurred some ideas and future results that is a good thing ;-).

tonydspaniard commented 11 years ago

Thanks for your input @mdeweerd, there are all good ideas

mdeweerd commented 11 years ago

@tonydspaniard I have set up a first project using Yiinitializr, so things are getting more concrete for me.

Feedback: It's probably not all Yiinitializr's fault, but I did spent some time trying to understand why my changes to 'dev.php' were not taken into account. This was due to several issues:

This also raised another possibility of improvement: to track where a configuration was coming from, I had to hack "Initializer.php". It'ld be nice if there are some helpers for debug.

Also, now that I see the use of Yiinitializr, I agree that my earlier suggestions are good ideas for Yiinitializr ;-).

Question:

Question:

Now that I am using this and that I am getting into GIT at the same time, some pull requests will probably be coming in the future.

Borales commented 11 years ago
env/dev.php is not used - instead it seems to be copied to 'env.php'.

@mdeweerd I spent some time to figure it out to myself. env.php is generated only with the composer install command or with the composer update in case when env.lock is missing (with the absent corresponding config/env.php files).

I made a console command that removes all env.php and env.lock files in my project. And after that - I launch composer update to refresh my config files. That's not suitable for me...

To keep existing structure, I suggest to generate env.php content similar to the following:

// depends on Config::value('envlock')
return require(__DIR__.'/env/{env}.php');
// or make it dynamic
return require(__DIR__.'/env/'.Config::value('envlock').'.php');

This way - we can solve our issue with the "expired" config data.

https://github.com/tonydspaniard/yiinitializr-advanced/blob/master/common/lib/Yiinitializr/Helpers/Initializer.php#L190

What do you think, @tonydspaniard ?

tonydspaniard commented 11 years ago

@Borales what do you think if instead of doing so much changes we propose a new parameter to the command?

Borales commented 11 years ago

@tonydspaniard sure, we can do that, I was just thinking about BC. Your call

tonydspaniard commented 11 years ago

Adding a new parameter couldn't break anything IMHO... is just a new option case.

Borales commented 11 years ago

@tonydspaniard oh sorry, I misunderstood your previous comment. Sure, a new command parameter would be nice :)

Borales commented 11 years ago

@tonydspaniard did you mean a new command parameter to regenerate env.php configs?

mdeweerd commented 6 years ago

Very old ticket - no longer useful.