Git Product home page Git Product logo

testcomposer's Introduction

制作 composer 包

0x01 初始化composer.json

composer init

填写包的各类信息,包括name、description、keywords、type、version、autoload、authors、license、require等。

composer.json 架构可参考 点击

0x02 编写代码文件

以本项目为例,在src目录下,创建子目录Decrypt/Lib,放核心功能类库代码。

src/Decrypt/Signature.php 为例,代码如下:

<?php

namespace Snoopy\TestSignature\Decrypt\Lib;

class Signature
{
    public static function getSignature($params, $secret)
    {
        $signature = '';
        ksort($params);
        foreach ($params as $k => $v) {
            if ($v instanceof \SplFileInfo) {
                $v = md5_file($v->getPathname());
            } else if ($v instanceof \CURLFile) {
                $v = md5_file($v->name);
            }
            $signature .= sprintf('%s=%s&', $k, $v);
        }
        $signature .= $secret;

        return md5($signature);
    }

    public static function getToken($app, $ticket, $time)
    {
        return md5(sprintf("%s%d%s", $app, $time, $ticket));
    }
}

注意到namespaceSnoopy\TestSignature\Decrypt\Lib,这是因为composer.json里的authoload.psr-4,有设置命名空间对应的代码路径映射关系:"Snoopy\\TestSignature\\": "src/"

0x03 推送到github仓库

git init
git remote add origin [email protected]:snoopyGIT/testcomposer.git
git commit -am "first commit"
git push -u origin main

0x04 提交到packagist.org

首先需要注册一个账号,邮箱激活以后,登录账号,在这个地址 https://packagist.org/packages/submit 把github仓库地址复制进去,点击提交。不出意外,就能提交成功了。如有意外,比如名字和别已有的包重复啦,类似啦,那就改一下,更新到github,再提交到packagist。

0x05 使用

在你的项目里: composer require snoopyebo/test_signature、 然后在你的代码里这样引用:(注意是用https://packagist.org源)

use Snoopy\TestSignature\Decrypt\Lib\Signature;

$params = [
    'id' => 1,
];
$secret = '123456';
$signature = Signature::getSignature($params, $secret);

echo $signature;

testcomposer's People

Contributors

snoopygit avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.