昨天做了phpCAS客户端简单封装,为了能让更多人使用,决定做成composer扩展
参考链接: https://www.jianshu.com/p/535af12c31ca
以下是以我的第一个composer扩展php-cas-client为例
1. 创建目录
mkdir d:/www/bingher/php-cas-client
cd d:/www/bingher/php-cas-client2. 初始化composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [bingher/php-cas-client]:
Description []: php cas client
Author [hbh112233abc <hbh112233abc@163.com>, n to skip]:
Minimum Stability []: dev
Package Type (e.g. library, project, metapackage, composer-plugin) []: library
License []: MIT
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]? n
Would you like to define your dev dependencies (require-dev) interactively [yes]? n
{
"name": "bingher/php-cas-client",
"description": "php cas client",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "hbh112233abc",
"email": "hbh112233abc@163.com"
}
],
"minimum-stability": "dev",
"require": {}
}
Do you confirm generation [yes]? yes在当前目录就生成了composer.json文件,我们继续修改,加入依赖包配置{
"name": "bingher/php-cas-client",
"description": "php cas client from phpCAS",
"type": "library",
"require": {
"php": ">=5.4",
"jasig/phpcas": "^1.3"
},
"license": "MIT",
"authors": [
{
"name": "hbh112233abc",
"email": "hbh112233abc@163.com"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"bingher\\phpcas\\": "src"
}
}
}3. 创建类文件 src/Cas.php<?php
namespace bingher\phpcas;
/**
* CAS 客户端
* $cas = new \bingher\phpcas\Cas($conf);
*
* 1 登录
* $user = $cas->login();
*
* 2 获取用户信息
* $user = $cas->user();
*
* 3 退出
* $cas->logout();
*/
class Cas
{
protected $conf = [
'debug' => true,
'host' => 'cas.server.com',
'real_hosts' => ['cas.server.com'],
'context' => '/cas',
'port' => 8443,
'ca_cert_file' => '',
'log_file' => '',
'cas_version' => '3.0',
'lang' => 'CAS_Languages_ChineseSimplified',
];
public function __construct($config = [])
{
if (!function_exists('curl_init')) {
throw new \Exception('You need to install the PHP module curl to be able to use CAS authentication.');
}
$this->conf = array_merge($this->conf, $config);
if ($this->conf['debug']) {
\phpCAS::setDebug($this->conf['log_file']); //开启调试模式,设置日志文件路径
\phpCAS::setVerbose(true); //是否显示错误信息
} else {
\phpCAS::setDebug('');
\phpCAS::setVerbose(false); //是否显示错误信息
}
\phpCAS::client($this->conf['cas_version'], $this->conf['host'], $this->conf['port'], $this->conf['context']); //配置客户端
\phpCAS::setLang($this->conf['lang']); //支持中文
\phpCAS::setNoCasServerValidation();
if (is_file($this->conf['ca_cert_file'])) {
\phpCAS::setCasServerCACert($this->conf['ca_cert_file'], false);
}
\phpCAS::handleLogoutRequests(); //同步退出
}
/**
* 登录操作
* 测试环境 测试账号test2 123456
* @return array 用户信息
*/
public function login()
{
\phpCAS::forceAuthentication(); //调用登录页面
return self::user();
}
/**
* 重新授权
*
*/
public function renew()
{
\phpCAS::renewAuthentication();
return self::user();
}
/**
* 获得用户信息
* @return array 用户信息
*/
public static function user()
{
if (!\phpCAS::isAuthenticated()) {
return false;
}
return [
'userInfo' => \phpCAS::getUser(), //获取用户信息
'userAttr' => \phpCAS::getAttributes(), //获取用户附加信息
];
}
/**
* 退出
*/
public function logout($url = '')
{
if (empty($url)) {
$url = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER["SERVER_NAME"];
}
\phpCAS::logoutWithRedirectService($url);
}
}4. 测试安装composer install此时会在vendor/composer/autoload_psr4.php中生成命名空间和目录的映射关系<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'bingher\\phpcas\\' => array($baseDir . '/src'),
);增加.gitignore为git忽略一些文件.idea
vendor/
composer.lock增加readme.md为项目增加描述5. 提交代码到github
创建自己的仓库 hbh112233abc/php-cas-client
git init
git add -A
git commit -am "init && dev package"
git remote add origin git@github.com:hbh112233abc/php-cas-client.git
git push -u origin master6. 登录自己的packagist submithttps://packagist.org/packages/submit(自己注册)提交github地址

提交确认后,就生成了自己的composer包

7. 测试使用自己开发的包扩展
到我们的tp5.1目录
d:/www/tp51执行composer安装composer require bingher/php-cas-client dev-master执行成功后,我们的vendor将会加入jasig,bingher两个目录,说明安装成功查看vendor/composer/autoload_prs4.php,增加了bingher/phpcas命名空间
'bingher\\phpcas\\' => array($vendorDir . '/bingher/php-cas-client/src'),8. 测试使用创建配置文件
config/cas.php<?php
return [
'debug' => true,
'host' => 'cas.server.com',
'real_hosts' => ['cas.server.com'],
'context' => '/cas',
'port' => 8443,
'ca_cert_file' => \Env::get('config_path') . '/cas.pem',
'log_file' => \Env::get('runtime_path') . '/log/' . date('Ym') . '/' . date('d') . '_cas.log',
];创建控制器application/index/controller/Demo.php<?php
namespace app\index\controller;
use bingher\phpcas\Cas;
use think\facade\Config;
class Demo
{
/**
* http://www.tp.com/index/demo/login
* @return [type] [description]
*/
public function login()
{
$cas = new Cas(Config::get('cas.'));
$user = $cas->login();
return view();
}
/**
* 退出登录
* @return [type] [description]
*/
public function logout()
{
$cas = new Cas(Config::get('cas.'));
$cas->logout();
}
}验证登录

大功告成
本文只是抛砖引玉,希望广大tper行动起来,自己开发composer扩展包,可以广播出来大家共同使用,共同进步.
