Skip to content

Commit b9a84a0

Browse files
committed
Allow optimised autoloader generation
1 parent 5195958 commit b9a84a0

File tree

2 files changed

+102
-86
lines changed

2 files changed

+102
-86
lines changed

source/Composer/Plugin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use ReflectionClass;
1313
use RecursiveDirectoryIterator;
1414
use RecursiveIteratorIterator;
15+
use function Pre\Plugin\compile;
1516

1617
class Plugin implements PluginInterface, EventSubscriberInterface
1718
{
@@ -92,7 +93,7 @@ public function onPreAutoloadDump(Event $event)
9293
$pre = $file->getPathname();
9394
$php = preg_replace("/pre$/", "php", $pre);
9495

95-
Pre\Plugin\compile(
96+
compile(
9697
$pre, $php, $format = true, $comment = false
9798
);
9899
}

source/functions.php

Lines changed: 100 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,126 +2,141 @@
22

33
namespace Pre\Plugin;
44

5-
function find($file, $iterations = 10, $prefix = __DIR__)
6-
{
7-
$folder = "../";
5+
if (!function_exists("\\Pre\\Plugin\\find")) {
6+
function find($file, $iterations = 10, $prefix = __DIR__) {
7+
$folder = "../";
88

9-
if ($prefix) {
10-
$folder = "{$prefix}/{$folder}";
11-
}
9+
if ($prefix) {
10+
$folder = "{$prefix}/{$folder}";
11+
}
1212

13-
for ($i = 0; $i < $iterations; $i++) {
14-
$try = "{$folder}{$file}";
13+
for ($i = 0; $i < $iterations; $i++) {
14+
$try = "{$folder}{$file}";
1515

16-
if (file_exists($try)) {
17-
return realpath($try);
18-
}
16+
if (file_exists($try)) {
17+
return realpath($try);
18+
}
1919

20-
$folder .= "../";
20+
$folder .= "../";
21+
}
2122
}
2223
}
2324

24-
function base()
25-
{
26-
$vendor = find("vendor");
27-
return realpath("{$vendor}/../");
25+
if (!function_exists("\\Pre\\Plugin\\base")) {
26+
function base() {
27+
$vendor = find("vendor");
28+
return realpath("{$vendor}/../");
29+
}
2830
}
2931

30-
function defer($code)
31-
{
32-
$hidden = realpath(__DIR__ . "/../hidden/vendor/autoload.php");
33-
$visible = find("autoload.php");
34-
35-
if (!$visible) {
36-
// the plugin is being used/tested directly
37-
$visible = __DIR__ . "/../vendor/autoload.php";
38-
}
3932

40-
$defer = "
41-
require '{$hidden}';
42-
require '{$visible}';
33+
if (!function_exists("\\Pre\\Plugin\\defer")) {
34+
function defer($code) {
35+
$hidden = realpath(__DIR__ . "/../hidden/vendor/autoload.php");
36+
$visible = find("autoload.php");
4337

44-
\$function = function() {
45-
{$code};
46-
};
38+
if (!$visible) {
39+
// the plugin is being used/tested directly
40+
$visible = __DIR__ . "/../vendor/autoload.php";
41+
}
4742

48-
print base64_encode(serialize(\$function()));
49-
";
43+
$defer = "
44+
require '{$hidden}';
45+
require '{$visible}';
5046
51-
$result = exec(
52-
"php -r 'eval(base64_decode(\"" . base64_encode($defer) . "\"));'"
53-
);
54-
55-
return unserialize(base64_decode($result));
56-
}
47+
\$function = function() {
48+
{$code};
49+
};
50+
51+
print base64_encode(serialize(\$function()));
52+
";
5753

58-
function instance()
59-
{
60-
static $instance = null;
54+
$result = exec(
55+
"php -r 'eval(base64_decode(\"" . base64_encode($defer) . "\"));'"
56+
);
6157

62-
if ($instance === null) {
63-
$instance = new Parser();
58+
return unserialize(base64_decode($result));
6459
}
60+
}
61+
62+
if (!function_exists("\\Pre\\Plugin\\instance")) {
63+
function instance() {
64+
static $instance = null;
65+
66+
if ($instance === null) {
67+
$instance = new Parser();
68+
}
6569

66-
return $instance;
70+
return $instance;
71+
}
6772
}
6873

69-
function process($from)
70-
{
71-
$instance = instance();
72-
return $instance->process($from);
74+
if (!function_exists("\\Pre\\Plugin\\process")) {
75+
function process($from) {
76+
$instance = instance();
77+
return $instance->process($from);
78+
}
7379
}
7480

75-
function compile($from, $to, $format = true, $comment = true)
76-
{
77-
$instance = instance();
78-
return $instance->compile($from, $to, $format, $comment);
81+
if (!function_exists("\\Pre\\Plugin\\compile")) {
82+
function compile($from, $to, $format = true, $comment = true) {
83+
$instance = instance();
84+
return $instance->compile($from, $to, $format, $comment);
85+
}
7986
}
8087

81-
function parse($code)
82-
{
83-
$instance = instance();
84-
return $instance->parse($code);
88+
if (!function_exists("\\Pre\\Plugin\\parse")) {
89+
function parse($code) {
90+
$instance = instance();
91+
return $instance->parse($code);
92+
}
8593
}
8694

87-
function format($code)
88-
{
89-
$instance = instance();
90-
return $instance->format($code);
95+
if (!function_exists("\\Pre\\Plugin\\format")) {
96+
function format($code) {
97+
$instance = instance();
98+
return $instance->format($code);
99+
}
91100
}
92101

93-
function addMacro($macro)
94-
{
95-
$instance = instance();
96-
return $instance->addMacro($macro);
102+
if (!function_exists("\\Pre\\Plugin\\addMacro")) {
103+
function addMacro($macro) {
104+
$instance = instance();
105+
return $instance->addMacro($macro);
106+
}
97107
}
98108

99-
function removeMacro($macro)
100-
{
101-
$instance = instance();
102-
return $instance->removeMacro($macro);
109+
if (!function_exists("\\Pre\\Plugin\\removeMacro")) {
110+
function removeMacro($macro) {
111+
$instance = instance();
112+
return $instance->removeMacro($macro);
113+
}
103114
}
104115

105-
function addCompiler($compiler, $callable)
106-
{
107-
$instance = instance();
108-
return $instance->addCompiler($compiler, $callable);
116+
if (!function_exists("\\Pre\\Plugin\\addCompiler")) {
117+
function addCompiler($compiler, $callable) {
118+
$instance = instance();
119+
return $instance->addCompiler($compiler, $callable);
120+
}
109121
}
110122

111-
function removeCompiler($compiler)
112-
{
113-
$instance = instance();
114-
return $instance->removeCompiler($compiler);
123+
if (!function_exists("\\Pre\\Plugin\\removeCompiler")) {
124+
function removeCompiler($compiler) {
125+
$instance = instance();
126+
return $instance->removeCompiler($compiler);
127+
}
115128
}
116129

117-
function addFunction(...$args)
118-
{
119-
$instance = instance();
120-
return $instance->addFunction(...$args);
130+
if (!function_exists("\\Pre\\Plugin\\addFunction")) {
131+
function addFunction(...$args) {
132+
$instance = instance();
133+
return $instance->addFunction(...$args);
134+
}
121135
}
122136

123-
function getFunction(...$args)
124-
{
125-
$instance = instance();
126-
return $instance->getFunction(...$args);
137+
if (!function_exists("\\Pre\\Plugin\\getFunction")) {
138+
function getFunction(...$args) {
139+
$instance = instance();
140+
return $instance->getFunction(...$args);
141+
}
127142
}

0 commit comments

Comments
 (0)