@@ -58,7 +58,12 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
58
58
59
59
$ classes = array_unique ($ classes );
60
60
61
- $ cache = $ cacheDir .'/ ' .$ name .$ extension ;
61
+ // cache the core classes
62
+ if (!is_dir ($ cacheDir ) && !@mkdir ($ cacheDir , 0777 , true ) && !is_dir ($ cacheDir )) {
63
+ throw new \RuntimeException (sprintf ('Class Collection Loader was not able to create directory "%s" ' , $ cacheDir ));
64
+ }
65
+ $ cacheDir = rtrim (realpath ($ cacheDir ), '/ ' .DIRECTORY_SEPARATOR );
66
+ $ cache = $ cacheDir .DIRECTORY_SEPARATOR .$ name .$ extension ;
62
67
63
68
// auto-reload
64
69
$ reload = false ;
@@ -99,31 +104,50 @@ public static function load($classes, $cacheDir, $name, $autoReload, $adaptive =
99
104
}
100
105
}
101
106
107
+ $ c = '(?:\s*+(?:(?:#|//)[^\n]*+\n|/\*(?:(?<!\*/).)++)?+)*+ ' ;
108
+ $ strictTypesRegex = str_replace ('. ' , $ c , "'^<\?php\s.declare.\(.strict_types.=.1.\).;'is " );
109
+
110
+ $ cacheDir = explode (DIRECTORY_SEPARATOR , $ cacheDir );
102
111
$ files = array ();
103
112
$ content = '' ;
104
113
foreach (self ::getOrderedClasses ($ classes ) as $ class ) {
105
114
if (in_array ($ class ->getName (), $ declared )) {
106
115
continue ;
107
116
}
108
117
109
- $ files [] = $ class ->getFileName ();
118
+ $ files [] = $ file = $ class ->getFileName ();
119
+ $ c = file_get_contents ($ file );
110
120
111
- $ c = preg_replace (array ('/^\s*<\?php/ ' , '/\?>\s*$/ ' ), '' , file_get_contents ($ class ->getFileName ()));
121
+ if (preg_match ($ strictTypesRegex , $ c )) {
122
+ $ file = explode (DIRECTORY_SEPARATOR , $ file );
112
123
113
- // fakes namespace declaration for global code
114
- if (!$ class ->inNamespace ()) {
115
- $ c = "\nnamespace \n{ \n" .$ c ."\n} \n" ;
116
- }
124
+ for ($ i = 0 ; isset ($ file [$ i ], $ cacheDir [$ i ]); ++$ i ) {
125
+ if ($ file [$ i ] !== $ cacheDir [$ i ]) {
126
+ break ;
127
+ }
128
+ }
129
+ if (1 >= $ i ) {
130
+ $ file = var_export (implode (DIRECTORY_SEPARATOR , $ file ), true );
131
+ } else {
132
+ $ file = array_slice ($ file , $ i );
133
+ $ file = str_repeat ('.. ' .DIRECTORY_SEPARATOR , count ($ cacheDir ) - $ i ).implode (DIRECTORY_SEPARATOR , $ file );
134
+ $ file = '__DIR__. ' .var_export (DIRECTORY_SEPARATOR .$ file , true );
135
+ }
117
136
118
- $ c = self ::fixNamespaceDeclarations ('<?php ' .$ c );
119
- $ c = preg_replace ('/^\s*<\?php/ ' , '' , $ c );
137
+ $ c = "\nnamespace {require $ file;} " ;
138
+ } else {
139
+ $ c = preg_replace (array ('/^\s*<\?php/ ' , '/\?>\s*$/ ' ), '' , $ c );
120
140
121
- $ content .= $ c ;
122
- }
141
+ // fakes namespace declaration for global code
142
+ if (!$ class ->inNamespace ()) {
143
+ $ c = "\nnamespace \n{ \n" .$ c ."\n} \n" ;
144
+ }
123
145
124
- // cache the core classes
125
- if (!is_dir ($ cacheDir ) && !@mkdir ($ cacheDir , 0777 , true ) && !is_dir ($ cacheDir )) {
126
- throw new \RuntimeException (sprintf ('Class Collection Loader was not able to create directory "%s" ' , $ cacheDir ));
146
+ $ c = self ::fixNamespaceDeclarations ('<?php ' .$ c );
147
+ $ c = preg_replace ('/^\s*<\?php/ ' , '' , $ c );
148
+ }
149
+
150
+ $ content .= $ c ;
127
151
}
128
152
self ::writeCacheFile ($ cache , '<?php ' .$ content );
129
153
0 commit comments