PHP code example of jhq0113 / roach
1. Go to this page and download the library: Download jhq0113/roach library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jhq0113 / roach example snippets
namespace app\model;
/**
* Class User
* @datetime 2020/7/2 11:23 PM
* @author roach
* @email [email protected]
*/
class User
{
/**
* @var string
* @datetime 2020/7/2 11:22 PM
* @author roach
* @email [email protected]
*/
public $userName;
/**
* @var string
* @datetime 2020/7/2 11:22 PM
* @author roach
* @email [email protected]
*/
public $password;
/**
* @var int
* @datetime 2020/7/2 11:24 PM
* @author roach
* @email [email protected]
*/
protected $_currentTime;
/**
* User constructor.
* @param string $userName
*/
public function __construct($userName = '')
{
$this->userName = $userName;
}
/**
* @datetime 2020/7/6 10:49 上午
* @author roach
* @email [email protected]
*/
public function init()
{
$this->_currentTime = time();
}
/**
* @param int $time
* @datetime 2020/7/3 1:40 下午
* @author roach
* @email [email protected]
*/
public function setTime($time)
{
$this->_currentTime = $time;
}
/**
* @return int
* @datetime 2020/7/2 11:24 PM
* @author roach
* @email [email protected]
*/
public function getCurrentTime()
{
return $this->_currentTime;
}
}
/**
* @var app\model\User $user
*/
$user = Container::createRoach([
'class' => 'app\model\User',
'userName' => 'lao zhou',
'password' => hash('sha1', '123456')
]);
echo json_encode([
'userName' => $user->userName,
'password' => $user->password,
], JSON_UNESCAPED_UNICODE).PHP_EOL;
/**
* @var app\model\User $user
*/
$user = Container::createRoach([
'class' => 'app\model\User',
'calls' => [
'__construct' => ['xiao mage']
],
]);
exit(json_encode([
'userName' => $user->userName,
'password' => $user->password,
], JSON_UNESCAPED_UNICODE).PHP_EOL);
/**
* @var \app\model\User $user2
*/
$user2 = Container::createRoach([
'class' => 'app\model\User',
'calls' => [
'init'
],
]);
echo json_encode([
'currentTime' => $user2->getCurrentTime()
], JSON_UNESCAPED_UNICODE).PHP_EOL;
/**
* @var \app\model\User $user2
*/
$user2 = Container::createRoach([
'class' => 'app\model\User',
'calls' => [
'__construct' => ['boss zhou'],
'setTime' => [ time() ],
],
]);
echo json_encode([
'userName' => $user2->userName,
'currentTime' => $user2->getCurrentTime(),
], JSON_UNESCAPED_UNICODE).PHP_EOL;
/**
* @var \app\model\User $user3
*/
$user3 = Container::createRoach([
'class' => 'app\model\User',
'calls' => [
'__construct' => ['boss zhou'],
[
'method' => 'setTime',
'params' => [ time() ],
],
[
'method' => 'setTime',
'params' => [ time() ],
],
],
]);
echo json_encode([
'userName' => $user3->userName,
'currentTime' => $user3->getCurrentTime(),
], JSON_UNESCAPED_UNICODE).PHP_EOL;
//将app\model\User放入容器,app\model\User对象并未创建
Container::set('user', [
'class' => 'app\model\User',
'userName' => 'platform',
'password' => hash_hmac('md5', 'roach', uniqid()),
'calls' => [
'setTime' => [ time() ]
]
]);
/**
* @var User $singleUser
*/
$singleUser = Container::get('user');
echo json_encode([
'userName' => $singleUser->userName,
'password' => $singleUser->password,
'currentTime' => $singleUser->getCurrentTime()
], JSON_UNESCAPED_UNICODE).PHP_EOL;
$singleUser->userName = 'single';
/**
* @var User $reGetUser
*/
$reGetUser = Container::get('user');
echo json_encode([
'userName' => $reGetUser->userName,
'password' => $reGetUser->password,
'currentTime' => $reGetUser->getCurrentTime()
], JSON_UNESCAPED_UNICODE).PHP_EOL;
Container::set('startTime', time());
Container::set('config', [
'appName' => 'roach',
'version' => '1.0.0'
]);
Container::set('user1', new User());
echo Container::get('startTime').PHP_EOL;
echo json_encode(Container::get('config'), JSON_UNESCAPED_UNICODE).PHP_EOL;
echo json_encode(Container::get('user1'), JSON_UNESCAPED_UNICODE).PHP_EOL;
use roach\Container;
//注入异常处理handler
Container::set('errorHandler', [
'class' => 'roach\exceptions\ErrorHandler',
//当捕获未被try...catch捕获的异常,会交由此handler处理
'handler' => function(\Throwable $exception) {
//打日志
//报警。。。
exit(json_encode([
'code' => $exception->getCode(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
],JSON_UNESCAPED_UNICODE).PHP_EOL);
}
]);
/**
* @var \roach\exceptions\ErrorHandler $errorHandler
*/
$errorHandler = Container::get('errorHandler');
//注册通用异常处理
$errorHandler->run();
//出发调用未定义方法错误
fun();
//$a =5/0;
$res = (new Request())
->setUrl('https://www.baidu.com')
->setTimeout(3)
->setParams([
'userName' => 'sdfafd'
])
->post();
\roach\extensions\ECli::error($res->getBody());
/**
* @var Request $request
*/
$request = Container::createRoach([
'class' => 'roach\http\Request',
'url' => 'https://www.baidu.com',
'params' => [
'from' => time()
]
]);
$response = $request->get();
\roach\extensions\ECli::info($response->getBody());
$req1 = Container::createRoach([
'class' => 'roach\http\Request',
'url' => 'https://www.360.cn',
]);
$req2 = Container::createRoach([
'class' => 'roach\http\Request',
'url' => 'http://www.sina.com',
'params' => [
'from' => time()
]
]);
$req3 = Container::createRoach([
'class' => 'roach\http\Request',
'url' => 'https://www.baidu.com',
'params' => [
'from' => time()
]
]);
$respList = Request::multiRequest($req1, $req2, $req3);
\roach\extensions\ECli::info($respList[0]->get('url'));
\roach\extensions\ECli::info($respList[1]->get('url'));
\roach\extensions\ECli::info($respList[2]->get('url'));
text
[2020-07-03 07:00:14] info:[ https://www.360.cn ]
[2020-07-03 07:00:14] info:[ http://www.sina.com?from=1593759614 ]
[2020-07-03 07:00:14] info:[ https://www.baidu.com?from=1593759614 ]