Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 8f8568e

Browse files
committed
Merging develop to master in preparation for 1.2.0 release.
2 parents 8385b75 + 4c406f8 commit 8f8568e

File tree

6 files changed

+177
-80
lines changed

6 files changed

+177
-80
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 1.1.2 - TBD
5+
## 1.2.0 - 2019-12-27
66

77
### Added
88

9+
- [#23](https://github.com/zendframework/zend-config-aggregator/pull/23) adds the ability to specify the file mode for the generated cache file, when generating a cache file. The mode can be provided via the `Zend\ConfigAggregator\ConfigAggregator::CACHE_FILEMODE` configuration option. Modes should be expressed as octal values (e.g., `0600`).
10+
911
- [#21](https://github.com/zendframework/zend-config-aggregator/pull/21) adds support for PHP 7.3.
1012

1113
### Changed

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
},
2222
"require-dev": {
2323
"malukenho/docheader": "^0.1.5",
24-
"mikey179/vfsStream": "^1.6",
2524
"phpunit/phpunit": "^5.7.21 || ^6.3",
2625
"zendframework/zend-coding-standard": "~1.0.0",
2726
"zendframework/zend-config": "^2.6 || ^3.0",
@@ -47,8 +46,8 @@
4746
},
4847
"extra": {
4948
"branch-alias": {
50-
"dev-master": "1.1.x-dev",
51-
"dev-develop": "1.2.x-dev"
49+
"dev-master": "1.2.x-dev",
50+
"dev-develop": "1.3.x-dev"
5251
}
5352
},
5453
"scripts": {

composer.lock

Lines changed: 17 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/book/caching.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,28 @@ providers. Because of that it is very fast, but after it is enabled, you cannot
4444
make any changes to configuration without clearing the cache. **Caching should
4545
be used only in a production environment**, and your deployment process should
4646
clear the cache.
47+
48+
You can control the permissions used when creating the cache file by passing
49+
the [file mode](http://php.net/manual/en/function.chmod.php) in the
50+
`ConfigAggregator::CACHE_FILEMODE` configuration. Use this if your config
51+
contains sensitive information such as database passwords:
52+
53+
```php
54+
use Zend\ConfigAggregator\ArrayProvider;
55+
use Zend\ConfigAggregator\ConfigAggregator;
56+
use Zend\ConfigAggregator\PhpFileProvider;
57+
58+
$aggregator = new ConfigAggregator(
59+
[
60+
new ArrayProvider([
61+
ConfigAggregator::ENABLE_CACHE => true,
62+
ConfigAggregator::CACHE_FILEMODE => 0600 // only owner can read and write
63+
]),
64+
new PhpFileProvider('*.global.php'),
65+
],
66+
'data/config-cache.php'
67+
);
68+
```
69+
70+
Note that mode is an octal value. To ensure the expected operation, you need
71+
to prefix the file mode with a zero (e.g. `0644`).

src/ConfigAggregator.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
use Zend\Stdlib\ArrayUtils\MergeReplaceKeyInterface;
1515

1616
use function array_key_exists;
17+
use function chmod;
1718
use function class_exists;
1819
use function date;
20+
use function fclose;
1921
use function file_exists;
20-
use function file_put_contents;
22+
use function flock;
23+
use function fopen;
24+
use function fputs;
25+
use function ftruncate;
2126
use function get_class;
2227
use function gettype;
2328
use function is_array;
@@ -35,6 +40,8 @@ class ConfigAggregator
3540
{
3641
const ENABLE_CACHE = 'config_cache_enabled';
3742

43+
const CACHE_FILEMODE = 'config_cache_filemode';
44+
3845
const CACHE_TEMPLATE = <<< 'EOT'
3946
<?php
4047
/**
@@ -253,6 +260,7 @@ private function loadConfigFromCache($cachedConfigFile)
253260
*
254261
* @param array $config
255262
* @param null|string $cachedConfigFile
263+
* @param int $mode
256264
*/
257265
private function cacheConfig(array $config, $cachedConfigFile)
258266
{
@@ -264,12 +272,35 @@ private function cacheConfig(array $config, $cachedConfigFile)
264272
return;
265273
}
266274

267-
file_put_contents($cachedConfigFile, sprintf(
275+
$mode = isset($config[self::CACHE_FILEMODE]) ? $config[self::CACHE_FILEMODE] : null;
276+
$tempFile = sys_get_temp_dir() . '/' . basename($cachedConfigFile) . '.tmp';
277+
278+
$fh = fopen($tempFile, 'c');
279+
if (! $fh) {
280+
return;
281+
}
282+
if (! flock($fh, LOCK_EX | LOCK_NB)) {
283+
fclose($fh);
284+
return;
285+
}
286+
287+
if ($mode !== null) {
288+
chmod($tempFile, $mode);
289+
}
290+
291+
ftruncate($fh, 0);
292+
293+
fputs($fh, sprintf(
268294
self::CACHE_TEMPLATE,
269295
get_class($this),
270296
date('c'),
271297
var_export($config, true)
272298
));
299+
300+
rename($tempFile, $cachedConfigFile);
301+
302+
flock($fh, LOCK_UN);
303+
fclose($fh);
273304
}
274305

275306
/**

0 commit comments

Comments
 (0)