首页 > php教程 > php手册 > 正文

Phar PHP 打包工具

php中文网
发布: 2016-06-06 19:37:29
原创
2059人浏览过

PharPHP打包工具 无 #!/usr/bin/env php?phpdefine("PHAR_BUILDER_VERSION", "0.1.0");function usage($self, $ln = PHP_EOL) { echo "Usage: {$self} phar [options]{$ln}"; echo "phar Path to an existing Phar archive or to-be-created archive.{$ln}";

Phar PHP 打包工具
#!/usr/bin/env php
<?php

define("PHAR_BUILDER_VERSION", "0.1.0");

function usage($self, $ln = PHP_EOL) {
    echo "Usage: {$self} phar [options]{$ln}";
    echo "phar            Path to an existing Phar archive or to-be-created archive.{$ln}";
    echo "                The file name's extension must contain .phar.{$ln}";
    echo "options:{$ln}";
    echo "  --alias       Alias with which this Phar archive{$ln}";
    echo "                should be referred to in calls to stream functionality.{$ln}";
    echo "                default value is basename(path).{$ln}";
    echo "  --path        The full or relative path to the directory{$ln}";
    echo "                that contains all files to add to the archive.{$ln}";
    echo "  --filter      An optional pcre regular expression that is used to filter the list of files.{$ln}";
    echo "                Only file paths matching the regular expression will be included in the archive.{$ln}";
    echo "  --files       files that add to the archive, seperator is ','{$ln}";
    echo "  --compress    gz or bz2{$ln}";
    echo "  --index       Relative path within the phar archive to run if accessed on the command-line{$ln}";
    echo "  --webindex    Relative path within the phar archive to run if accessed through a web browser{$ln}";
    echo "  --stub        A string or file path handle to use as the executable stub for this phar archive.{$ln}";
    echo "{$ln}";
    exit(1);
}

function error($message, $ln = PHP_EOL) {
    echo "Error: {$message}{$ln}";
    exit(1);
}

function info($message, $ln = PHP_EOL) {
    echo "{$message}{$ln}";
}

/**
 * 解释参数,可以解释以下类型:
 *     -p
 *     -pVALUE
 *     -p value
 *     --param value
 *     -p=value
 *     --param=value
 *     param=value
 * @param array $argv
 * @return array
 */
function args_parse($argv) {
    if (!is_array($argv) || empty($argv)) {
        return array();
    }
    $argc = count($argv);
    $ret = array();
    for ($i = 0; $i < $argc; ++$i) {
        $arg = $argv[$i];
        if (strpos($arg, '=') > 0) { // -p=value --param=value param=value
            list($arg_name, $arg_value) = explode('=', ltrim($arg, '-'), 2);
            $ret[$arg_name] = $arg_value;
            continue;
        }
        if ($arg{0} !== '-') {
            continue;
        }
        if (($arg{1} !== '-') && isset($arg{2})) {// -pVALUE
            $ret[$arg{1}] = substr($arg, 2);
            continue;
        } else if (isset($argv[$i + 1]) && ($argv[$i + 1]{0} !== '-') && (false === strpos($arg, '='))) {
            $ret[ltrim($arg, '-')] = $argv[$i + 1];
            ++$i;
        } else {
            $ret[ltrim($arg, '-')] = true;
        }
    }
    return $ret;
}

info("Phar Builder " . PHAR_BUILDER_VERSION);

if ('cli' !== PHP_SAPI) {
    error("Run for command line only.");
}

if (false === Phar::canWrite()) {
    error("Phar can not write, Set \"phar.readonly = Off\" in php.ini.");
}

$self = array_shift($argv);
if (empty($argv[0])) {
    usage($self);
}

$path = array_shift($argv);
$args = args_parse($argv);
$stub = empty($args['stub']) ? '' : $args['stub'];
$flags = 0;
$files = empty($args['files']) ? '' : $args['files'];
$alias = empty($args['alias']) ? basename($path) : $args['alias'];
$regex = empty($args['filter']) ? null : $args['filter'];
$base_dir = empty($args['path']) ? '' : $args['path'];
$arg_compress = empty($args['compress']) ? '' : $args['compress'];
$index = empty($args['index']) ? '' : $args['index'];
$webindex = empty($args['webindex']) ? '' : $args['webindex'];
switch ($arg_compress) {
    case 'gz':
        $compress = Phar::GZ;
        $compress_type = 'gz';
        break;
    case 'bz2':
        $compress = Phar::BZ2;
        $compress_type = 'bz2';
        break;
    default :
        $compress = Phar::NONE;
        $compress_type = 'none';
        break;
}

if (!empty($base_dir) && !is_dir($base_dir)) {
    error("Dir not Exists!");
}

try {
    
    $p = new Phar($path, $flags, $alias);
    $p->startBuffering();
    $p->compress($compress);

    info("API Version: " . Phar::apiVersion());
    info("File: {$path}");
    info("Alias: {$alias}");
    info("Compress: {$compress_type}");
    if (!empty($base_dir)) {
        info("Build From: {$base_dir}");
        if ($regex) {
            info("Filter: {$regex}");
        }
        $p->buildFromDirectory($base_dir, $regex);
    }
    if (!empty($files)) {
        foreach (explode(',', $files) as $file) {
            info("Add File: {$file}");
            $p->addFile($file, basename($file));
        }
    }
    
    if ($index && $webindex) {
        info("Index: {$index}");
        info("Web Index: {$webindex}");
        $p->setDefaultStub($index, $webindex);
    } else if ($index) {
        info("Index: {$index}");
        $p->setDefaultStub($index);
    } else if ($webindex) {
        info("Web Index: {$webindex}");
        $p->setDefaultStub(null, $webindex);
    }
    
    if ($stub) {
        info("Stub: {$stub}");
        if (is_file($stub)) {
            $stub = file_get_contents($stub);
        }
        $p->setStub($stub);
    }

    $p->stopBuffering();
    info("Files: {$p->count()}");
    
} catch (\Exception $e) {
    error($e->getMessage());
}
登录后复制
相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号