rcs_us/php-git-server 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rcs_us/php-git-server

Composer 安装命令:

composer require rcs_us/php-git-server

包简介

DAV based PHP based Git server. No dependencies on git command.

README 文档

README

DAV based PHP based Git server. No dependencies on git command.

Install

Install latest version using composer.

$ composer require rcs_us/php-git-server

Usage

The server extends sabre.io's DAV server(http://sabre.io/dav/) and nikic's FastRoute (https://github.com/nikic/FastRoute).

A basic implementation would look like the following:

if (PHP_SAPI == "cli-server") {
    // To help the built-in PHP dev server, check if the request was actually for
    // something which should probably be served as a static file
    $file = __DIR__ . $_SERVER['REQUEST_URI'];
    if (is_file($file)) {
        return false;
    }
}

// Define source path ( built in PHP server doesn't like relative paths )
defined('SOURCE_PATH')
|| define('SOURCE_PATH', (getenv('SOURCE_PATH') ? getenv('SOURCE_PATH') : dirname(__DIR__)));

defined('REPOSITORY_PATH')
|| define('REPOSITORY_PATH', (getenv('REPOSITORY_PATH') ? getenv('REPOSITORY_PATH') : "/tmp/git-repositories"));

$loader = require SOURCE_PATH . "/vendor/autoload.php";

$rcsGitServer = new \RCS\Git\Server();

$serverAuthBackend = new \Sabre\DAV\Auth\Backend\BasicCallBack(function($user, $pass){
    $args = [$user, $pass];
    error_log("args: " . print_r($args, true) );
    return true;
});

$serverAuthPlugin  = new \Sabre\DAV\Auth\Plugin($serverAuthBackend);
   
// OPTIONALLY, you can add authentication this way     
// $rcsGitServer->addServerPlugin($serverAuthPlugin);

$dispatcher = \FastRoute\simpleDispatcher(function(\FastRoute\RouteCollector $r) {

    // This can also be a single addRoute(['PROPFIND','MKCOL',....])
    $r->addRoute('PROPFIND', '/{repository}.git/[{path:.+}]', 'webdav');
    $r->addRoute('MKCOL', '/{repository}.git/[{path:.+}]', 'webdav');
    $r->addRoute('LOCK', '/{repository}.git/[{path:.+}]', 'webdav');
    $r->addRoute('PUT', '/{repository}.git/[{path:.+}]', 'webdav');
    $r->addRoute('UNLOCK', '/{repository}.git/[{path:.+}]', 'webdav');
    $r->addRoute('GET', '/{repository}.git/[{path:.+}]', 'webdav');
    $r->addRoute('MOVE', '/{repository}.git/[{path:.+}]', 'webdav');
    
});

// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
    $uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
    case \FastRoute\Dispatcher::NOT_FOUND:
        error_log("\FastRoute\Dispatcher::NOT_FOUND");
        // ... 404 Not Found
        break;
    case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
        error_log("\FastRoute\Dispatcher::METHOD_NOT_ALLOWED");
        $allowedMethods = $routeInfo[1];
        // ... 405 Method Not Allowed
        break;
    case \FastRoute\Dispatcher::FOUND:
        error_log("\FastRoute\Dispatcher::FOUND");
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];
        // ... call $handler with $vars
        
        // list($class, $method) = explode(":", $handler, 2);
        // call_user_func_array(array(new $class, $method), $vars);
        call_user_func(array($rcsGitServer, $handler), $vars);
        
        break;
}

// So you can see the requests as they come in on the PHP built in server
if (PHP_SAPI == "cli-server") {
    error_log($_SERVER["REQUEST_METHOD"] . "::" . $_SERVER["REQUEST_URI"]."\n");
}

This can be tested with PHP's built in server using ( you can substitute 8888 for whatever port you want ):

php -S localhost:8888 -t public index.php

统计信息

  • 总下载量: 16
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固