-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathExportInclude.php
More file actions
60 lines (49 loc) · 1.83 KB
/
ExportInclude.php
File metadata and controls
60 lines (49 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace DrupalCodeBuilder\Storage;
use DrupalCodeBuilder\Exception\StorageException;
/**
* Provides a storage handler that writes PHP declarations of data in files.
*
* Slightly better than ExportEval, as the resulting files get syntax
* highlighting in text editors.
*/
class ExportInclude extends StorageBase {
/**
* {@inheritdoc}
*/
public function store($key, $data) {
$directory = $this->environment->getDataDirectory();
$export = '<?php $data =' . "\n" . var_export($data, TRUE) . ';';
file_put_contents("{$directory}/{$key}_processed.php", $export);
}
/**
* {@inheritdoc}
*/
public function retrieve($key) {
$directory = $this->environment->getDataDirectory();
// Convert the public:// scheme to an absolute path, as using include on
// a stream on PHP ^7.4 causes it to call streamWrapper::stream_set_option()
// which for the public:// wrapper on Drupal causes a warning in
// LocalStream::stream_set_option().
$directory = str_replace(
"public://",
\Drupal::service('file_system')->realpath("public://") . '/',
$directory
);
$data_file = "$directory/{$key}_processed.php";
if (file_exists($data_file)) {
// Don't use include_once, in case callers are neglecting to cache their
// data an come here several times for the same key. (Which they really
// shouldn't, but it's not a nice way to catch the problem.)
include $data_file;
// The included file must declare the $data variable.
if (!isset($data)) {
throw new StorageException("Included data file {$data_file} did not execute correctly as PHP.");
}
return $data;
}
// Incremental code analysis will call this method before anything has been
// saved, so if the file doesn't exist just return an empty array.
return [];
}
}