/
Command.php
112 lines (100 loc) · 3.43 KB
/
Command.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
*
* This file is part of the Aura project for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Framework\Cli\CacheClassmap;
use Aura\Framework\Cli\AbstractCommand;
use Aura\Framework\System as System;
/**
*
* Caches the map of package source class files to `tmp/cache/classmap.php`.
*
* @package Aura.Framework
*
*/
class Command extends AbstractCommand
{
/**
*
* The System object.
*
* @var System
*
*/
protected $system;
/**
*
* Sets the system object.
*
* @param System $system The system object.
*
*/
public function setSystem(System $system)
{
$this->system = $system;
}
/**
*
* Caches the map of package source class files to
* `tmp/cache/classmap.php`.
*
* @return mixed
*
*/
public function action()
{
$this->stdio->outln("Caching package class map ... ");
// the eventual class map
$classmap = [];
// get the list of all packages in the system
$package_glob = $this->system->getPackagePath('*');
$package_list = glob($package_glob, GLOB_ONLYDIR);
// retain the actual path to the packages, and create a "fake" path
// with a literal '$system' in it, for use in the cached file.
$package_path = $this->system->getPackagePath();
$package_fake = str_replace(
$this->system->getRootPath(),
'{$system}',
$package_path
);
// go through each package in the system ...
foreach ($package_list as $package_dir) {
// ... and iterate over its src dir
$path = $package_dir . DIRECTORY_SEPARATOR . 'src';
$items = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path),
\RecursiveIteratorIterator::SELF_FIRST
);
$skip = strlen($path) + 1;
foreach ($items as $file => $object) {
$base = substr($file, $skip);
if (preg_match('/^[A-Z].*\.php$/', $base)) {
$base = substr($base, 0, -4);
$base = str_replace(DIRECTORY_SEPARATOR, '\\', $base);
$class = $base;
// convert the file's *real* absolute path to a fake
// with '$system` in it. this allows us to cache file
// locations relative to $system and keep them in code
// repositories for deployment to other systems.
$file = str_replace($package_path, $package_fake, $file);
$classmap[$class] = $file;
$this->stdio->outln($class . ' => ' . $file);
}
}
}
// create and/or clear the cached classmap.php file
$cache = $this->system->getTmpPath("cache/classmap.php");
@mkdir(dirname($cache), 0777, true);
// export the classmap, and convert single quotes to double quotes
// so that $system is interpolated properly
$output = "<?php return " . var_export($classmap, true) . ';';
$output = str_replace("'", '"', $output);
file_put_contents($cache, $output);
$this->stdio->outln("Cached to $cache.");
$this->stdio->outln('Done.');
}
}