php-makephar/src/MakePhar/Application.php

124 lines
3.6 KiB
PHP

<?php
namespace MakePhar;
class Application
{
protected $config;
private function parseCommandLine()
{
$opts = "hnq";
$long = [ 'help', 'new' ];
$parsed = getopt($opts, $long);
$config = [
'init' => false,
'help' => false,
'manifest' => getcwd()."/makephar.sdl",
'output' => null,
'compress' => false,
'quiet' => false,
];
foreach ($parsed as $k=>$v) switch ($k) {
case 'h':
case 'help':
$config['help'] = true;
break;
case 'n':
case 'new':
$config['init'] = true;
break;
case 'q':
$config['quiet'] = true;
break;
}
$this->config = (object)$config;
}
private function printHelp()
{
printf("Usage: makephar [opts]\n");
printf("Options: -h,--help Show this help\n");
printf(" -n,--new Create a new configuration file\n");
printf(" -q Quiet operation\n");
}
public function run()
{
$this->parseCommandLine();
if ($this->config->quiet) {
define("QUIET",true);
}
if ($this->config->help) {
$this->printHelp();
return;
}
if ($this->config->init) {
if (!file_exists("composer.json")) {
error_log("No composer.json in current directory");
return 1;
}
$proj = json_decode(file_get_contents("composer.json"));
$proj_name = basename(getcwd()).".phar";
$proj_files = [];
$proj_dirs = [];
printf("// makephar.sdl generated for %s\n", basename(getcwd()));
printf("phar \"%s\" {\n", $proj_name);
printf(" include {\n");
if (!empty($proj->autoload)) {
foreach ((array)$proj->autoload as $type=>$loaders) {
if ($type == 'files') {
$proj_files = array_merge($proj_files, $loaders);
} else {
foreach ($loaders as $ns=>$path) {
$dir = rtrim($path," /");
if (!in_array($dir,$proj_dirs)) {
$proj_dirs[] = $dir;
}
}
}
}
foreach ($proj_files as $file)
printf(" file \"%s\";\n", $file);
foreach ($proj_dirs as $dir)
printf(" dir \"%s\";\n", $dir);
printf(" dir \"vendor\";\n");
} else {
printf(" // file \"filename.php\" src=\"path/file.php\";\n");
printf(" // dir \"path\";\n");
}
printf(" }\n");
printf(" // stub \"src/path-to-stub.php\";\n");
printf("}\n");
return;
}
log_info("MakePhar 2.0 - (c) 2016-2017, NoccyLabs");
$this->buildManifest();
}
protected function buildManifest()
{
$manifests = Manifest::createFromFile($this->config->manifest);
foreach ($manifests as $manifest) {
$type = $manifest->getIsLibrary()?"library":"application";
log_info("Building %s %s", $type, $manifest->getOutput());
$builder = new PharBuilder($manifest);
$builder->prepare();
$builder->build();
$builder->finalize();
}
}
}