Skip to content

Commit

Permalink
Merge c1223c7 into 73f5866
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Aug 25, 2020
2 parents 73f5866 + c1223c7 commit 139f627
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#15](https://github.com/laminas/laminas-modulemanager/pull/15) adds support for caching closures when caching configuration.

### Changed

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"require": {
"php": "^7.3",
"brick/varexporter": "^0.3.2",
"laminas/laminas-config": "^3.1 || ^2.6",
"laminas/laminas-eventmanager": "^3.2 || ^2.6.3",
"laminas/laminas-stdlib": "^3.1 || ^2.7",
Expand Down
13 changes: 12 additions & 1 deletion src/Listener/AbstractListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Laminas\ModuleManager\Listener;

use Brick\VarExporter\ExportException;
use Brick\VarExporter\VarExporter;
use Laminas\ModuleManager\Listener\Exception\ConfigCannotBeCachedException;
use Webimpress\SafeWriter\FileWriter;

/**
Expand Down Expand Up @@ -62,7 +65,15 @@ public function setOptions(ListenerOptions $options)
*/
protected function writeArrayToFile($filePath, $array)
{
$content = "<?php\nreturn " . var_export($array, true) . ';';
try {
$content = "<?php\n" . VarExporter::export(
$array,
VarExporter::ADD_RETURN | VarExporter::CLOSURE_SNAPSHOT_USES
) . ';';
} catch (ExportException $e) {
throw ConfigCannotBeCachedException::fromExporterException($e);
}

FileWriter::writeFile($filePath, $content);

return $this;
Expand Down
40 changes: 40 additions & 0 deletions src/Listener/Exception/ConfigCannotBeCachedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @see https://github.com/laminas/laminas-modulemanager for the canonical source repository
* @copyright https://github.com/laminas/laminas-modulemanager/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-modulemanager/blob/master/LICENSE.md New BSD License
*/

namespace Laminas\ModuleManager\Listener\Exception;

use Brick\VarExporter\ExportException;
use RuntimeException;
use Throwable;

use function sprintf;

final class ConfigCannotBeCachedException extends RuntimeException
{
private function __construct($message = "", $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}

/**
* @internal
*
* @return self
*/
public static function fromExporterException(ExportException $exportException): self
{
return new self(
sprintf(
'Cannot export config into a cache file. Config contains uncacheable entries: %s',
$exportException->getMessage()
),
$exportException->getCode(),
$exportException
);
}
}
13 changes: 13 additions & 0 deletions test/Listener/_files/good/merge3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* @see https://github.com/laminas/laminas-modulemanager for the canonical source repository
* @copyright https://github.com/laminas/laminas-modulemanager/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-modulemanager/blob/master/LICENSE.md New BSD License
*/

return [
'toUpper' => function ($input) {
return strtoupper($input);
},
];

0 comments on commit 139f627

Please sign in to comment.