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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
#!/usr/bin/php
<?php
/*
=head1 NAME
phpabtpl - generate phpab templates with dependency autoloading
=head1 SYNOPSIS
B<phpabtpl> [I<OPTION>...] [I<composer.json>]
=head1 DESCRIPTION
B<phpabtpl> is a command-line tool used to generate B<phpab> autoloader
templates. In addition to the regular autoloading of the package's own class
files, the generated template will also include automatic loading of
dependencies and statically loaded files, based on the specification read from
a I<composer.json> file.
=head1 OPTIONS
=over 4
=item B<-h, --help>
Print a help message and exit.
=item B<--basedir> I<directory>
Set the base directory for "files" autoloading.
This option can be used to adjust the paths for statically loaded files
(Composer's "files" section), to treat them as relative to the specified
directory rather than to the source's root. This would typical be used for
packages where the actual source files are kept in a sub-directory (commonly
"src").
Limitation: This directory must be somewhere above the to-be-loaded files in
the file hierarchy. It will not traverse upwards.
=item B<--require> I<composer_package>
Adds an additional required dependency to be loaded.
I<composer_package> is the name of a Composer package, e.g. "vendor/project".
It is treated as an extra entry in the composer.json "require" section.
This option can be repeated to add multiple dependencies.
=item B<--suggest> I<composer_package>
Adds an additional suggested dependency to be (optionally) loaded.
I<composer_package> is the name of a Composer package, e.g. "vendor/project".
It is treated as an extra entry in the composer.json "suggest" section.
This option can be repeated to add multiple dependencies.
=item B<--require-file> I<file>
Adds an additional file to be loaded.
The I<file> path will be adapted to B<--basedir>, if given.
It is treated as an extra entry in the composer.json "autoload.files" section.
This option can be repeated to add multiple files.
=back
=head1 EXAMPLES
=over 4
=item Generate a template with dependency autoloading:
phpabtpl composer.json >autoload.php.tpl
=item Generate a template for an autoloader to be placed in the "src" directory:
phpabtpl --basedir src composer.json >autoload.php.tpl
=item Generate a template with manually specified dependencies:
phpabtpl --require proj1/pack1 --require-file src/autoload.php
=back
=head1 FILES
=over 4
=item F<debian/pkg-php-tools-autoloaders>
Autoloaders that are not correctly identified by B<phpabtpl> by default can be
manually specified in this file.
The format is a set of lines, with each line having the following fields:
=over 6
=item I<vendor_name>
The Composer package's vendor name (e.g. "vendor" in "vendor/project").
=item I<project_name>
The Composer package's project name (e.g. "project" in "vendor/project").
=item I<autoloader_path>
The corresponding autoloader file (e.g. "Vendor/Project/autoload.php").
=back
It can be installed in B</usr/share/pkg-php-tools/autoloaders/>I<package>.
=back
=cut
*/
$usage = <<<USAGE
Usage: $argv[0] [OPTION...] [composer.json]
-h, --help Print a help message and exit.
--basedir Set the base directory for 'files' autoloading.
--require Load an additional required dependency (repeatable).
--suggest Load an additional suggested dependency (repeatable).
--require-file Load an additional required file (repeatable).
USAGE;
/*
Read user input.
*/
$options = getopt('h', [
'basedir:',
'help',
'require:',
'suggest:',
'require-file:',
], $argi);
# -h, --help
$help = isset($options['h']) || isset($options['help']);
if ($help) {
print $usage;
exit;
}
# --basedir
$basedir = $options['basedir'] ?? null;
if ($basedir !== null)
$basedir = rtrim($basedir, '/').'/';
# --require, --suggest, --require-file
$extra_requires = (array)($options['require'] ?? null);
$extra_suggests = (array)($options['suggest'] ?? null);
$extra_files = (array)($options['require-file'] ?? null);
# composer.json
$composer = $argv[$argi] ?? null;
if ($composer) {
$composer = file_get_contents($composer);
$composer = json_decode($composer, true, 512, JSON_THROW_ON_ERROR);
} else {
fwrite(STDERR, 'Proceeding without a composer.json file.');
$composer = [];
}
/*
Load known autoloaders from list files
1. /usr/share/pkg-php-tools/autoloaders/common
2. /usr/share/pkg-php-tools/autoloaders/*
3. debian/pkg-php-tools-autoloaders
*/
$autoloaders = [];
function load_list($file) {
global $autoloaders;
if (!file_exists($file))
return; # Ignore missing file
if (($fh = fopen($file, 'r')) === false)
throw new \RuntimeException("Unable to open '$file'");
while (($line = fgets($fh)) !== false) {
$line = trim($line);
if (!$line)
continue; # Ignore empty line
$fields = preg_split('/\s+/', $line, 3);
if (count($fields) < 2) { # 3rd field may be empty
fwrite(STDERR, "Ignoring malformed line '$line' in $file\n");
continue;
}
$dependency = $fields[0].'/'.$fields[1];
$autoloader = ($fields[2] ?? 'none') !== 'none' ? $fields[2] : null;
$autoloaders[$dependency] = $autoloader;
}
if (!feof($fh))
throw new \RuntimeException("Unable to read '$file'");
fclose($fh);
}
# Load list of common known autoloaders
$lists_dir = dirname(dirname(__FILE__)).'/share/pkg-php-tools/autoloaders';
load_list("$lists_dir/common");
# Load other installed lists of known autoloaders
if (($dh = opendir($lists_dir)) === false)
throw new \RuntimeException("Unable to open '$lists_dir'");
while (($file = readdir($dh)) !== false) {
if (!in_array($file, ['.', '..', 'common']))
load_list("$lists_dir/$file");
}
closedir($dh);
# Load list from current package
load_list('debian/pkg-php-tools-autoloaders');
/*
Figure out autoloaders based on
1. Known autoloaders
2. Guessed namespaces
*/
function get_autoloader($dependency) {
global $autoloaders;
# Ignore php and extensions
if (preg_match('/^(php(-cli)?$|ext-)/', $dependency))
return null;
# Known?
if (array_key_exists($dependency, $autoloaders))
return $autoloaders[$dependency];
# Guess
$ns = remove_repeated_parts($dependency);
$ns = ucwords($ns, '/-_.');
$ns = str_replace(['-', '_', '.'], '', $ns);
return "$ns/autoload.php";
}
function remove_repeated_parts($dependency) {
[$vendor, $project] = explode('/', $dependency, 2);
$vendor_parts = preg_split('/[-_.]/', $vendor);
$project_parts = preg_split('/[-_.]/', $project);
$n = count($vendor_parts);
# Remove vendor name from project name
if ($vendor_parts === array_slice($project_parts, 0, $n))
$project_parts = array_slice($project_parts, $n);
# Drop first part of project name if equal to last part of vendor name
else if ($vendor_parts[$n-1] === $project_parts[0])
array_shift($project_parts);
$project = implode('-', $project_parts);
return trim("$vendor/$project", '/');
}
/*
Collect autoloaders for all dependencies
*/
# Required packages
$required = [];
$dependencies = array_merge(
array_keys($composer['require'] ?? []),
$extra_requires,
);
foreach ($dependencies as $dependency) {
$path = get_autoloader($dependency);
if ($path !== null)
$required[] = "/usr/share/php/" . $path;
}
# Suggested packages
$suggested = [];
$dependencies = array_merge(
array_keys($composer['suggest'] ?? []),
$extra_suggests,
);
foreach ($dependencies as $dependency) {
$path = get_autoloader($dependency);
if ($path !== null)
$suggested[] = "/usr/share/php/" . $path;
}
# Statically loaded files
$files = array_merge(
$composer['autoload']['files'] ?? [],
$extra_files,
);
foreach ($files as &$file) {
if ($basedir !== null && strpos($file, $basedir) === 0)
$file = substr($file, strlen($basedir));
}
# Output autoloader template
sort($required);
sort($suggested);
sort($files);
################################################################################
echo "<?php\n"; ?>
// Require
<?php foreach ($required as $path) { ?>
require_once '<?=$path?>';
<?php } ?>
// Suggest
<?php foreach ($suggested as $path) { ?>
if (stream_resolve_include_path('<?=$path?>')) { include_once '<?=$path?>'; }
<?php } ?>
// @codingStandardsIgnoreFile
// @codeCoverageIgnoreStart
// this is an autogenerated file - do not edit
spl_autoload_register(
function($class) {
static $classes = null;
if ($classes === null) {
$classes = array(
___CLASSLIST___
);
}
$cn = strtolower($class);
if (isset($classes[$cn])) {
require ___BASEDIR___$classes[$cn];
}
},
___EXCEPTION___,
___PREPEND___
);
// @codeCoverageIgnoreEnd
// Files
<?php foreach ($files as $path) { ?>
<?php if (str_starts_with($path, '/')) { ?>
require_once realpath('<?=$path?>');
<?php } else { ?>
require_once __DIR__.'/<?=$path?>';
<?php } ?>
<?php } ?>
|