Skip to content

Commit 4f43ac9

Browse files
committed
Merge pull request #24 from loadsys/f/serializing-output
F/serializing output
2 parents ea130f8 + 29946b3 commit 4f43ac9

3 files changed

Lines changed: 600 additions & 115 deletions

File tree

README.md

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A CakePHP plugin that provides a Shell to read an app's Configure vars from the
1010

1111
* This is the Cake 3.x version of the plugin, which exists on the `master` branch and is tracked by the `~3.0` semver.
1212
* For the Cake 2.x version of this plugin, please use the repo's `cake-2.x` branch. (semver `~2.0`)
13-
* For the Cake 1.3 version, use the `cake-1.3` branch. (semver `~1.0`) **Note:** we don't expect to actively maintain the 1.3 version. It's here because the project started life as a 1.3 Shell.
13+
* For the Cake 1.3 version, use the `cake-1.3` branch. (semver `~1.0`) **Note:** we don't expect to actively maintain or improve the 1.3 version. It's here because the project started life as a 1.3 Shell.
1414

1515

1616
## Requirements
@@ -28,30 +28,78 @@ $ composer require loadsys/cakephp-config-read:~3.0
2828

2929
## Usage
3030

31+
Imagine the following defined in `config/app.php`:
32+
33+
```php
34+
return [
35+
'Key' => [
36+
'Name' => 'foo',
37+
],
38+
'Second' => [
39+
'Key' => [
40+
'First' => 'bar',
41+
'Second' => 'baz',
42+
'Third' => 42,
43+
],
44+
],
45+
];
46+
```
47+
48+
49+
To use this plugin, call it from the command line:
50+
3151
```shell
3252
$ cd path/to/app/
33-
$ ./bin/cake ConfigRead Key.Name
53+
$ ./bin/cake ConfigRead.ConfigRead Key.Name
3454
'foo'
3555
```
3656

3757
### Format as a bash variable definition
3858

3959
```shell
40-
$ ./bin/cake ConfigRead Key.Name Second.Key
60+
$ ./bin/cake ConfigRead.ConfigRead Key.Name Second.Key
4161
KEY_NAME='foo'
4262
SECOND_KEY_FIRST='bar'
4363
SECOND_KEY_SECOND='baz'
4464
SECOND_KEY_THIRD='42'
4565
```
4666

47-
Note that this format is automatically used whenever more than one key is returned. For example, if you request a key that contains an array, all values in the array will be returned sequentially. Alternatively, if you pass multiple keys on the command line, they will be returned. The format can also be forced using the `-b` or `--bash` command line switch:
67+
Note that this format is automatically used whenever more than one key is returned (unless the `--serialize` switch has been used). For example, if you request a key that contains an array, all values in the array will be returned sequentially. Alternatively, if you pass multiple keys on the command line, they will all be returned. The format can also be forced using the `-b` or `--bash` command line switch:
4868

4969
```shell
50-
$ ./bin/cake ConfigRead -b Key.Name
70+
$ ./bin/cake ConfigRead.ConfigRead -b Key.Name
5171
KEY_NAME='foo'
5272
```
5373

54-
## Gotchas
74+
### Serializing output
75+
76+
It is possible serialize the output from ConfigReadShell so that it can be consumed by other PHP scripts more easily by using the `-s` or `--serialize` command line switch.
77+
78+
Requesting multiple keys on the command line will produce an array of those keys. Requesting a single scalar value will produce only that scalar value.
79+
80+
This switch always overrides both the `--bash` switch and the Shell's automatic bash formatting.
81+
82+
```shell
83+
$ ./bin/cake ConfigRead.ConfigRead -s Key.Name Second.Key
84+
a:2:{s:8:"Key.Name";s:3:"foo";s:10:"Second.Key";a:3:{s:5:"First";s:3:"bar";s:6:"Second";s:3:"baz";s:5:"Third";i:42;}}
85+
# Check the result by piping into PHP and unserializing the result.
86+
$ ./bin/cake ConfigRead.ConfigRead -s Key.Name Second.Key | php -r 'print_r(unserialize(file_get_contents("php://stdin")));'
87+
Array
88+
(
89+
[Key.Name] => foo
90+
91+
[Second.Key] => Array
92+
(
93+
[First] => bar
94+
[Second] => baz
95+
[Third] => 42
96+
)
97+
98+
)
99+
```
100+
101+
102+
## Potential Gotchas
55103

56104
### "Consumed" Configure Vars
57105

@@ -64,48 +112,9 @@ CakePHP 3 by default "consumes" some of its configs so as not to confused develo
64112
* Log/Log
65113
* Security.salt/Security::salt()
66114

67-
The effect is that you can not use the ConfigReadShell to obtain Configure values for any of these keys since they no longer exist in Configure's store. (This is particularly troublesome if you are using [Environment-Aware Configs](https://github.com/beporter/CakePHP-EnvAwareness/tree/master/slides).)
68-
69-
There are two possible workarounds:
70-
71-
1. Use the `ConsoleShell` instead. For example:
72-
73-
```shell
74-
$ echo 'use Cake\Datasource\ConnectionManager; foreach(ConnectionManager::config("default") as $k => $v) { echo "$k=" . escapeshellarg($v) . PHP_EOL; } exit;' | bin/cake Console -q
75-
className='Cake\Database\Connection'
76-
driver='Cake\Database\Driver\Mysql'
77-
persistent=''
78-
host='localhost'
79-
username='my_app'
80-
password='secret'
81-
database='my_app'
82-
encoding='utf8'
83-
timezone='UTC'
84-
cacheMetadata='1'
85-
quoteIdentifiers=''
86-
name='default'
87-
```
88-
89-
This command is wrapped up in our [loadsys/cakephp-shell-scripts](https://github.com/loadsys/CakePHP-Shell-Scripts) repo as the [`db-credentials`](https://github.com/loadsys/CakePHP-Shell-Scripts/blob/76a24/db-credentials) script.
90-
91-
2. Edit your `config/bootstrap.php` to use `Configure::read()` instead of `Configure::consume()`.
92-
93-
```diff
94-
-Cache::config(Configure::consume('Cache'));
95-
-ConnectionManager::config(Configure::consume('Datasources'));
96-
-Email::configTransport(Configure::consume('EmailTransport'));
97-
-Email::config(Configure::consume('Email'));
98-
-Log::config(Configure::consume('Log'));
99-
-Security::salt(Configure::consume('Security.salt'));
100-
+Cache::config(Configure::read('Cache'));
101-
+ConnectionManager::config(Configure::read('Datasources'));
102-
+Email::configTransport(Configure::read('EmailTransport'));
103-
+Email::config(Configure::read('Email'));
104-
+Log::config(Configure::read('Log'));
105-
+Security::salt(Configure::read('Security.salt'));
106-
```
107-
108-
This will leave the Configure vars in place and allow commands like `bin/cake ConfigRead Datasources.default` to work as expected, but be warned that the values in Configure might not reflect the values actually being used by the various Cake modules.
115+
The ConfigReadShell devotes about half of its codebase dealing with this for you, allowing you to continue to fetch values using the Configure path (`Datasources.default.host` -> `localhost`) while in the background querying `ConnectionManager::config('default')['host']`. (This is particularly helpful if you are using [Environment-Aware Configs](https://github.com/beporter/CakePHP-EnvAwareness/tree/master/slides).)
116+
117+
The "gotcha" here is that ConfigReadShell has to maintain a static list of Configure keys that are consumed, and how to access them in their new container. **If your app consumes a non-standard Configure key during bootstrapping, you will not be able to obtain it from the ConfigReadShell.**
109118

110119

111120
## Contributing

0 commit comments

Comments
 (0)