Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFR: Get one element of the array with config->get() #12221

Closed
xerron opened this issue Sep 14, 2016 · 23 comments
Closed

NFR: Get one element of the array with config->get() #12221

xerron opened this issue Sep 14, 2016 · 23 comments
Assignees
Milestone

Comments

@xerron
Copy link

xerron commented Sep 14, 2016

The good from the get function is that you can set a default value.

now:

$adapter = $this->config->database->adapter ? $this->config->database->adapter : 'Mysql' ;

This is better:

$adapter = $this->config->get('database.adapter', 'Mysql');
@mzf
Copy link

mzf commented Sep 15, 2016

Dot notation is very good idea

@sergeyklay
Copy link
Contributor

I think will be better:

$adapter = $this->config->path('application.database.adapter', 'Mysql');
$adapter = $this->config->path('application/database/adapter', 'Mysql', '/');

@mzf
Copy link

mzf commented Sep 15, 2016

Ye. Fine option

@xerron
Copy link
Author

xerron commented Sep 16, 2016

it's a little confusing use 'path', for me is better 'get'

@Jurigag
Copy link
Contributor

Jurigag commented Sep 16, 2016

But what if for some weird reason someone already have named section with dot ?

@xerron
Copy link
Author

xerron commented Sep 16, 2016

@Jurigag you mean this?

<?php
return new \Phalcon\Config([
    'database' => [
        'adapter' => 'Postgresql',
        'host' => 'localhost',
        'username' => 'xerron',
        'password' => 'root',
        'dbname' => 'db_uwu'
    ],
   // ...code ...
    ' section.with.dot' =>  'mmm...'
]

good question. 👍

@xerron
Copy link
Author

xerron commented Sep 16, 2016

@sergeyklay 👍

@michanismus
Copy link
Contributor

I need this so bad! 100x thumbs up

@sergeyklay sergeyklay added this to the 3.1.0 milestone Sep 18, 2016
@sergeyklay sergeyklay self-assigned this Sep 18, 2016
@sergeyklay
Copy link
Contributor

Will be implemented in 3.1.0

@Jurigag
Copy link
Contributor

Jurigag commented Sep 18, 2016

So implement it in other places too, as far as i know people wanted this in forms too and somewhere else.

@xerron
Copy link
Author

xerron commented Sep 21, 2016

In PHP7 default values is most readable:

$adapter =  $this->config->get('database.adapter') ?? 'Mysql';

@sergeyklay
Copy link
Contributor

but it's longer and we support not only PHP7

@Jurigag
Copy link
Contributor

Jurigag commented Sep 21, 2016

Just add option to just call $this->config->path('application.database.adapter') without second argument, and you can use default value like php7 with null coalescing operator.

@xerron
Copy link
Author

xerron commented Sep 22, 2016

Well, this is good:

$adapter = $this->config->path('application/database/adapter', '/');

@daison12006013
Copy link

This is cool

@ricksanchez
Copy link

@sergeyklay Actually what @xerron needs, is only:
$adapter = $this->config->database->adapter ?: 'Mysql' ;
Not PHP7 ?? required for this, just the classic Elvis operator ?:

@sergeyklay
Copy link
Contributor

@udarkness Related to #11157

@Yurist-85
Copy link

After read all above I think, the best way is something like this:

$config = new Config([ ... ]);
Use '.' [dot] notation by default but allowed to reset it if needed.
$config->setPathSeparator('/');

Use 3rd param to reset separator per request:
$val = $config->get('some.other.path.to_the.val', null, '.');

Return null if not exists (it's current behaviour I guess):
$val = $config->get('some/path/to/nonexistantval'); // null

Three ways to get default value:

$val = $config->get('some/path/to/nonexistantval', 'default_value');
$val = $config->get('some/path/to/nonexistantval') ?: 'default_value'; // 5.6
$val = $config->get('some/path/to/nonexistantval') ?? 'default_value'; // 7

And I'm absolutely sure that ->get() is much more clear and obvious than ->path() .

@fenixphp
Copy link

get($key, $default = null)

get('Dashboard/Main.plugins', [])

Up to the first dot, the path to the file. Dashboard/Main
Dashboard/Main.json or Dashboard/Main.php depending on the adapter.

After first dot using as array dot notation.

Something like this?

@Jurigag
Copy link
Contributor

Jurigag commented Feb 28, 2017

What? But config adapter itself accept file path, so no, it can't be like this.

@sergeyklay sergeyklay modified the milestones: 3.1.0, 3.2.0 Mar 2, 2017
@fenixphp
Copy link

fenixphp commented Mar 2, 2017

А если он будет принимать путь к папке с конфигами, и получится ленивая загрузка конфигураций только там где это нужно и со значениями по умолчанию?


And if he will take the path to the configuration file, and you get lazy loading configurations only where it is needed and with default values?


Sorry translation may be inaccurate.

@sergeyklay
Copy link
Contributor

Implemented in the 3.2.x branch

@sergeyklay
Copy link
Contributor

@fenixphp This should be a separated NFR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants