Skip to content

Commit 8a32eaf

Browse files
committed
Add gotcha docs about Configure::consume().
Fixes #20.
1 parent 6f8c292 commit 8a32eaf

1 file changed

Lines changed: 60 additions & 5 deletions

File tree

README.md

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66

77
A CakePHP plugin that provides a Shell to read an app's Configure vars from the command line.
88

9-
10-
* This is the Cake 3.x version of the plugin, which exists on the `master` branch and is tracked by the `3.*` semver.
11-
* For the Cake 2.x version of this plugin, please use the repo's `cake-2.x` branch. (semver `2.*`)
12-
* For the Cake 1.3 version, use the `cake-1.3` branch. (semver `1.*`) **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.
9+
* This is the Cake 3.x version of the plugin, which exists on the `master` branch and is tracked by the `~3.0` semver.
10+
* For the Cake 2.x version of this plugin, please use the repo's `cake-2.x` branch. (semver `~2.0`)
11+
* 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.
1312

1413

1514
## Requirements
1615

1716
* CakePHP 3.0.0+
18-
* PHP 5.4.19+
17+
* PHP 5.6+
1918

2019

2120
## Installation
@@ -50,6 +49,62 @@ $ ./bin/cake ConfigRead -b Key.Name
5049
KEY_NAME='foo'
5150
```
5251

52+
## Gotchas
53+
54+
### "Consumed" Configure Vars
55+
56+
CakePHP 3 by default "consumes" some of its configs so as not to confused developers. [`Configure::consume()`](http://book.cakephp.org/3.0/en/development/configuration.html#Cake\Core\Configure::consume) removes the configuration key from Configure, making it unavailable to the rest of the app. At the [time of this writing](https://github.com/cakephp/app/blob/a0f2c4/config/bootstrap.php#L136,L141), it does this for the following keys/classes:
57+
58+
* Cache/Cache
59+
* Datasources/ConnectionManager
60+
* EmailTransport/Email
61+
* Email/Email
62+
* Log/Log
63+
* Security.salt/Security::salt()
64+
65+
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).)
66+
67+
There are two possible workarounds:
68+
69+
1. Use the `ConsoleShell` instead. For example:
70+
71+
```shell
72+
$ echo 'use Cake\Datasource\ConnectionManager; foreach(ConnectionManager::config("default") as $k => $v) { echo "$k=" . escapeshellarg($v) . PHP_EOL; } exit;' | bin/cake Console -q
73+
className='Cake\Database\Connection'
74+
driver='Cake\Database\Driver\Mysql'
75+
persistent=''
76+
host='localhost'
77+
username='my_app'
78+
password='secret'
79+
database='my_app'
80+
encoding='utf8'
81+
timezone='UTC'
82+
cacheMetadata='1'
83+
quoteIdentifiers=''
84+
name='default'
85+
```
86+
87+
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.
88+
89+
2. Edit your `config/bootstrap.php` to use `Configure::read()` instead of `Configure::consume()`.
90+
91+
```diff
92+
-Cache::config(Configure::consume('Cache'));
93+
-ConnectionManager::config(Configure::consume('Datasources'));
94+
-Email::configTransport(Configure::consume('EmailTransport'));
95+
-Email::config(Configure::consume('Email'));
96+
-Log::config(Configure::consume('Log'));
97+
-Security::salt(Configure::consume('Security.salt'));
98+
+Cache::config(Configure::read('Cache'));
99+
+ConnectionManager::config(Configure::read('Datasources'));
100+
+Email::configTransport(Configure::read('EmailTransport'));
101+
+Email::config(Configure::read('Email'));
102+
+Log::config(Configure::read('Log'));
103+
+Security::salt(Configure::read('Security.salt'));
104+
```
105+
106+
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.
107+
53108

54109
## Contributing
55110

0 commit comments

Comments
 (0)