code
stringlengths
31
1.39M
docstring
stringlengths
23
16.8k
func_name
stringlengths
1
126
language
stringclasses
1 value
repo
stringlengths
7
63
path
stringlengths
7
166
url
stringlengths
50
220
license
stringclasses
7 values
public function wsTest(): Response { return view('home/ws-test'); }
@RequestMapping("/wstest", method={"GET"}) @return Response @throws Throwable
wsTest
php
swoft-cloud/swoft
app/Http/Controller/HomeController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/HomeController.php
Apache-2.0
public function dataConfig(): array { return bean(GoodsData::class)->getConfig(); }
@RequestMapping("/dataConfig", method={"GET"}) @return array @throws Throwable
dataConfig
php
swoft-cloud/swoft
app/Http/Controller/HomeController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/HomeController.php
Apache-2.0
public function release(): array { sgo(function () { Redis::connection(); }); Redis::connection(); return ['release']; }
Only to use test. The wrong way to use it @RequestMapping("release") @return array @throws RedisException
release
php
swoft-cloud/swoft
app/Http/Controller/RedisController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/RedisController.php
Apache-2.0
public function exPipeline(): array { sgo(function () { Redis::pipeline(function () { throw new RuntimeException(''); }); }); Redis::pipeline(function () { throw new RuntimeException(''); }); return ['exPipeline']; }
Only to use test. The wrong way to use it @RequestMapping("ep") @return array
exPipeline
php
swoft-cloud/swoft
app/Http/Controller/RedisController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/RedisController.php
Apache-2.0
public function exTransaction(): array { sgo(function () { Redis::transaction(function () { throw new RuntimeException(''); }); }); Redis::transaction(function () { throw new RuntimeException(''); }); return ['exPipeline']; }
Only to use test. The wrong way to use it @RequestMapping("et") @return array
exTransaction
php
swoft-cloud/swoft
app/Http/Controller/RedisController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/RedisController.php
Apache-2.0
public function session(Response $response): Response { $sess = HttpSession::current(); $times = $sess->get('times', 0); $times++; $sess->set('times', $times); return $response->withData([ 'times' => $times, 'sessId' => $sess->getSessionId() ]); }
@RequestMapping("/session") @param Response $response @return Response
session
php
swoft-cloud/swoft
app/Http/Controller/SessionController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/SessionController.php
Apache-2.0
public function set(Response $response): Response { $sess = HttpSession::current(); $sess->set('testKey', 'test-value'); $sess->set('testKey1', ['k' => 'v', 'v1', 3]); return $response->withData(['testKey', 'testKey1']); }
@RequestMapping() @param Response $response @return Response
set
php
swoft-cloud/swoft
app/Http/Controller/SessionController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/SessionController.php
Apache-2.0
public function close(Response $response): Response { $sess = HttpSession::current(); return $response->withData(['destroy' => $sess->destroy()]); }
@RequestMapping() @param Response $response @return Response
close
php
swoft-cloud/swoft
app/Http/Controller/SessionController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/SessionController.php
Apache-2.0
public function deleteByCo(): array { $data = Task::co('testTask', 'delete', [12]); if (is_bool($data)) { return ['bool']; } return ['notBool']; }
@RequestMapping(route="deleteByCo") @return array @throws TaskException
deleteByCo
php
swoft-cloud/swoft
app/Http/Controller/TaskController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/TaskController.php
Apache-2.0
public function deleteByAsync(): array { $data = Task::async('testTask', 'delete', [12]); return [$data]; }
@RequestMapping(route="deleteByAsync") @return array @throws TaskException
deleteByAsync
php
swoft-cloud/swoft
app/Http/Controller/TaskController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/TaskController.php
Apache-2.0
public function validateAll(Request $request): array { $method = $request->getMethod(); if ($method == RequestMethod::GET) { return $request->getParsedQuery(); } return $request->getParsedBody(); }
Verify all defined fields in the TestValidator validator @RequestMapping() @Validate(validator="TestValidator") @param Request $request @return array
validateAll
php
swoft-cloud/swoft
app/Http/Controller/ValidatorController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/ValidatorController.php
Apache-2.0
public function validateType(Request $request): array { $method = $request->getMethod(); if ($method == RequestMethod::GET) { return $request->getParsedQuery(); } return $request->getParsedBody(); }
Verify only the type field in the TestValidator validator @RequestMapping() @Validate(validator="TestValidator", fields={"type"}) @param Request $request @return array
validateType
php
swoft-cloud/swoft
app/Http/Controller/ValidatorController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/ValidatorController.php
Apache-2.0
public function validatePassword(Request $request): array { $method = $request->getMethod(); if ($method == RequestMethod::GET) { return $request->getParsedQuery(); } return $request->getParsedBody(); }
Verify only the password field in the TestValidator validator @RequestMapping() @Validate(validator="TestValidator", fields={"password"}) @param Request $request @return array
validatePassword
php
swoft-cloud/swoft
app/Http/Controller/ValidatorController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/ValidatorController.php
Apache-2.0
public function validateCustomer(Request $request): array { $method = $request->getMethod(); if ($method == RequestMethod::GET) { return $request->getParsedQuery(); } return $request->getParsedBody(); }
Customize the validator with userValidator @RequestMapping() @Validate(validator="userValidator") @param Request $request @return array
validateCustomer
php
swoft-cloud/swoft
app/Http/Controller/ValidatorController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/ValidatorController.php
Apache-2.0
public function index(Response $response): Response { $response = $response->withContent('<html lang="en"><h1>Swoft framework</h1></html>'); $response = $response->withContentType(ContentType::HTML); return $response; }
@RequestMapping("index") @param Response $response @return Response
index
php
swoft-cloud/swoft
app/Http/Controller/ViewController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/ViewController.php
Apache-2.0
public function indexByViewTag(): array { return [ 'msg' => 'hello' ]; }
Will render view by annotation tag View @RequestMapping("/home") @View("home/index") @throws Throwable
indexByViewTag
php
swoft-cloud/swoft
app/Http/Controller/ViewController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Controller/ViewController.php
Apache-2.0
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $uriPath = $request->getUriPath(); $domain = $request->getUri()->getHost(); if (!isset($this->domain2paths[$domain])) { return context()->getResponse()->withStatus(404)->withContent('invalid request domain'); } foreach ($this->domain2paths[$domain] as $prefix) { // not match route prefix if (strpos($uriPath, $prefix) !== 0) { return context()->getResponse()->withStatus(404)->withContent('page not found'); } } return $handler->handle($request); }
Process an incoming server request. @param ServerRequestInterface|Request $request @param RequestHandlerInterface $handler @return ResponseInterface @inheritdoc
process
php
swoft-cloud/swoft
app/Http/Middleware/DomainLimitMiddleware.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Middleware/DomainLimitMiddleware.php
Apache-2.0
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $uriPath = $request->getUriPath(); $domain = $request->getUri()->getHost(); if ($request->getUriPath() === '/favicon.ico') { return context()->getResponse()->withStatus(404); } return $handler->handle($request); }
Process an incoming server request. @param ServerRequestInterface|Request $request @param RequestHandlerInterface $handler @return ResponseInterface @inheritdoc
process
php
swoft-cloud/swoft
app/Http/Middleware/FavIconMiddleware.php
https://github.com/swoft-cloud/swoft/blob/master/app/Http/Middleware/FavIconMiddleware.php
Apache-2.0
public function setId(?int $id): void { $this->id = $id; }
@param int|null $id @return void
setId
php
swoft-cloud/swoft
app/Model/Entity/Count.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Entity/Count.php
Apache-2.0
public function setUserId(?int $userId): void { $this->userId = $userId; }
@param int|null $userId @return void
setUserId
php
swoft-cloud/swoft
app/Model/Entity/Count.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Entity/Count.php
Apache-2.0
public function setCreateTime(?int $createTime): void { $this->createTime = $createTime; }
@param int|null $createTime @return void
setCreateTime
php
swoft-cloud/swoft
app/Model/Entity/Count.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Entity/Count.php
Apache-2.0
public function setAttributes(?string $attributes): void { $this->attributes = $attributes; }
@param string|null $attributes @return void
setAttributes
php
swoft-cloud/swoft
app/Model/Entity/Count.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Entity/Count.php
Apache-2.0
public function setUpdateTime(?string $updateTime): void { $this->updateTime = $updateTime; }
@param string|null $updateTime @return void
setUpdateTime
php
swoft-cloud/swoft
app/Model/Entity/Count.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Entity/Count.php
Apache-2.0
public function setId(?int $id): void { $this->id = $id; }
@param int|null $id @return void
setId
php
swoft-cloud/swoft
app/Model/Entity/User.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Entity/User.php
Apache-2.0
public function setTestJson(?array $testJson): void { $this->testJson = $testJson; }
@param array|null $testJson @return void
setTestJson
php
swoft-cloud/swoft
app/Model/Entity/User.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Entity/User.php
Apache-2.0
public function func(): string { // Do something throw new Exception('Breaker exception'); }
@Breaker(fallback="funcFallback") @return string @throws Exception
func
php
swoft-cloud/swoft
app/Model/Logic/BreakerLogic.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Logic/BreakerLogic.php
Apache-2.0
public function loop(): string { // Do something throw new Exception('Breaker exception'); }
@Breaker(fallback="loopFallback") @return string @throws Exception
loop
php
swoft-cloud/swoft
app/Model/Logic/BreakerLogic.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Logic/BreakerLogic.php
Apache-2.0
public function loopFallback(): string { // Do something throw new Exception('Breaker exception'); }
@Breaker(fallback="loopFallback2") @return string @throws Exception
loopFallback
php
swoft-cloud/swoft
app/Model/Logic/BreakerLogic.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Logic/BreakerLogic.php
Apache-2.0
public function loopFallback2(): string { // Do something throw new Exception('Breaker exception'); }
@Breaker(fallback="loopFallback3") @return string @throws Exception
loopFallback2
php
swoft-cloud/swoft
app/Model/Logic/BreakerLogic.php
https://github.com/swoft-cloud/swoft/blob/master/app/Model/Logic/BreakerLogic.php
Apache-2.0
public function run(Pool $pool, int $workerId): void { while (true) { CLog::info('worker-' . $workerId); Coroutine::sleep(3); } }
@param Pool $pool @param int $workerId
run
php
swoft-cloud/swoft
app/Process/Worker1Process.php
https://github.com/swoft-cloud/swoft/blob/master/app/Process/Worker1Process.php
Apache-2.0
public function run(Pool $pool, int $workerId): void { while (true) { // Database $user = User::find(1)->toArray(); CLog::info('user=' . json_encode($user)); // Redis Redis::set('test', 'ok'); CLog::info('test=' . Redis::get('test')); CLog::info('worker-' . $workerId . ' context=' . context()->getWorkerId()); Coroutine::sleep(3); } }
@param Pool $pool @param int $workerId @throws DbException
run
php
swoft-cloud/swoft
app/Process/Worker2Process.php
https://github.com/swoft-cloud/swoft/blob/master/app/Process/Worker2Process.php
Apache-2.0
public function process(RequestInterface $request, RequestHandlerInterface $requestHandler): ResponseInterface { return $requestHandler->handle($request); }
@param RequestInterface $request @param RequestHandlerInterface $requestHandler @return ResponseInterface
process
php
swoft-cloud/swoft
app/Rpc/Middleware/ServiceMiddleware.php
https://github.com/swoft-cloud/swoft/blob/master/app/Rpc/Middleware/ServiceMiddleware.php
Apache-2.0
public function getList(int $id, $type, int $count = 10): array { return ['name' => ['list']]; }
@param int $id @param mixed $type @param int $count @return array
getList
php
swoft-cloud/swoft
app/Rpc/Service/UserService.php
https://github.com/swoft-cloud/swoft/blob/master/app/Rpc/Service/UserService.php
Apache-2.0
public function getList(int $id, $type, int $count = 10): array { return [ 'name' => ['list'], 'v' => '1.2' ]; }
@param int $id @param mixed $type @param int $count @return array
getList
php
swoft-cloud/swoft
app/Rpc/Service/UserServiceV2.php
https://github.com/swoft-cloud/swoft/blob/master/app/Rpc/Service/UserServiceV2.php
Apache-2.0
public function test(string $name): string { return 'sync-test-' . $name; }
@TaskMapping() @param string $name @return string
test
php
swoft-cloud/swoft
app/Task/Task/SyncTask.php
https://github.com/swoft-cloud/swoft/blob/master/app/Task/Task/SyncTask.php
Apache-2.0
public function getList(int $id, string $default = 'def'): array { return [ 'list' => [1, 3, 3], 'id' => $id, 'default' => $default ]; }
@TaskMapping(name="list") @param int $id @param string $default @return array
getList
php
swoft-cloud/swoft
app/Task/Task/TestTask.php
https://github.com/swoft-cloud/swoft/blob/master/app/Task/Task/TestTask.php
Apache-2.0
public function delete(int $id): bool { if ($id > 10) { return true; } return false; }
@TaskMapping() @param int $id @return bool
delete
php
swoft-cloud/swoft
app/Task/Task/TestTask.php
https://github.com/swoft-cloud/swoft/blob/master/app/Task/Task/TestTask.php
Apache-2.0
public function returnNull(string $name) { return null; }
@TaskMapping() @param string $name @return null
returnNull
php
swoft-cloud/swoft
app/Task/Task/TestTask.php
https://github.com/swoft-cloud/swoft/blob/master/app/Task/Task/TestTask.php
Apache-2.0
public function list(Response $response): void { $response->setData('[list]allow command: list, echo, demo.echo'); }
@TcpMapping("list", root=true) @param Response $response
list
php
swoft-cloud/swoft
app/Tcp/Controller/DemoController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Tcp/Controller/DemoController.php
Apache-2.0
public function index(Request $request, Response $response): void { $str = $request->getPackage()->getDataString(); $response->setData('[demo.echo]hi, we received your message: ' . $str); }
@TcpMapping("echo") @param Request $request @param Response $response
index
php
swoft-cloud/swoft
app/Tcp/Controller/DemoController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Tcp/Controller/DemoController.php
Apache-2.0
public function strRev(Request $request, Response $response): void { $str = $request->getPackage()->getDataString(); $response->setData(strrev($str)); }
@TcpMapping("strrev", root=true) @param Request $request @param Response $response
strRev
php
swoft-cloud/swoft
app/Tcp/Controller/DemoController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Tcp/Controller/DemoController.php
Apache-2.0
public function echo(Request $request, Response $response): void { // $str = $request->getRawData(); $str = $request->getPackage()->getDataString(); $response->setData('[echo]hi, we received your message: ' . $str); }
@TcpMapping("echo", root=true) @param Request $request @param Response $response
echo
php
swoft-cloud/swoft
app/Tcp/Controller/DemoController.php
https://github.com/swoft-cloud/swoft/blob/master/app/Tcp/Controller/DemoController.php
Apache-2.0
public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $start = '>before '; CLog::info('before handle'); $resp = $handler->handle($request); $resp->setData($start . $resp->getData() . ' after>'); CLog::info('after handle'); return $resp; }
@param RequestInterface $request @param RequestHandlerInterface $handler @return ResponseInterface
process
php
swoft-cloud/swoft
app/Tcp/Middleware/DemoMiddleware.php
https://github.com/swoft-cloud/swoft/blob/master/app/Tcp/Middleware/DemoMiddleware.php
Apache-2.0
public function process(RequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $start = '>before '; CLog::info('before handle'); $resp = $handler->handle($request); $resp->setData($start . $resp->getData() . ' after>'); CLog::info('after handle'); return $resp; }
@param RequestInterface $request @param RequestHandlerInterface $handler @return ResponseInterface
process
php
swoft-cloud/swoft
app/Tcp/Middleware/GlobalTcpMiddleware.php
https://github.com/swoft-cloud/swoft/blob/master/app/Tcp/Middleware/GlobalTcpMiddleware.php
Apache-2.0
public function validate(array $data, array $params): array { $start = $data['start'] ?? null; $end = $data['end'] ?? null; if ($start === null && $end === null) { throw new ValidatorException('Start time and end time cannot be empty'); } if ($start > $end) { throw new ValidatorException('Start cannot be greater than the end time'); } return $data; }
@param array $data @param array $params @return array @throws ValidatorException
validate
php
swoft-cloud/swoft
app/Validator/CustomerValidator.php
https://github.com/swoft-cloud/swoft/blob/master/app/Validator/CustomerValidator.php
Apache-2.0
public function validate(array $data, string $propertyName, $item, $default = null, $strict = false): array { $message = $item->getMessage(); if (!isset($data[$propertyName]) && $default === null) { $message = (empty($message)) ? sprintf('%s must exist!', $propertyName) : $message; throw new ValidatorException($message); } $rule = '/^[A-Za-z0-9\-\_]+$/'; if (preg_match($rule, $data[$propertyName])) { return $data; } $message = (empty($message)) ? sprintf('%s must be a email', $propertyName) : $message; throw new ValidatorException($message); }
@param array $data @param string $propertyName @param object $item @param null $default @param bool $strict @return array @throws ValidatorException
validate
php
swoft-cloud/swoft
app/Validator/Rule/AlphaDashRule.php
https://github.com/swoft-cloud/swoft/blob/master/app/Validator/Rule/AlphaDashRule.php
Apache-2.0
public function onOpen(Request $request, int $fd): void { server()->push($request->getFd(), "Opened, welcome!(FD: $fd)"); $fullClass = Session::current()->getParserClass(); $className = basename($fullClass); $help = <<<TXT Message data parser: $className Send message example: ```json { "cmd": "home.index", "data": "hello" } ``` Description: - cmd `home.index` => App\WebSocket\Chat\HomeController::index() TXT; server()->push($fd, $help); }
@OnOpen() @param Request $request @param int $fd
onOpen
php
swoft-cloud/swoft
app/WebSocket/ChatModule.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/ChatModule.php
Apache-2.0
public function onOpen(Request $request, int $fd): void { Session::current()->push("Opened, welcome #{$fd}!"); }
@OnOpen() @param Request $request @param int $fd
onOpen
php
swoft-cloud/swoft
app/WebSocket/EchoModule.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/EchoModule.php
Apache-2.0
public function onMessage(Server $server, Frame $frame): void { $server->push($frame->fd, 'Recv: ' . $frame->data); }
@OnMessage() @param Server $server @param Frame $frame
onMessage
php
swoft-cloud/swoft
app/WebSocket/EchoModule.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/EchoModule.php
Apache-2.0
public function onOpen(Request $request, int $fd): void { Session::current()->push("Opened, welcome!(FD: $fd)"); }
@OnOpen() @param Request $request @param int $fd
onOpen
php
swoft-cloud/swoft
app/WebSocket/TestModule.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/TestModule.php
Apache-2.0
public function index(): void { Session::current()->push('hi, this is home.index'); }
Message command is: 'home.index' @return void @MessageMapping()
index
php
swoft-cloud/swoft
app/WebSocket/Chat/HomeController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Chat/HomeController.php
Apache-2.0
public function echo(string $data): void { Session::current()->push('(home.echo)Recv: ' . $data); }
Message command is: 'home.echo' @param string $data @MessageMapping()
echo
php
swoft-cloud/swoft
app/WebSocket/Chat/HomeController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Chat/HomeController.php
Apache-2.0
public function autoReply(string $data): string { return '(home.ar)Recv: ' . $data; }
Message command is: 'home.ar' @param string $data @MessageMapping("ar") @return string
autoReply
php
swoft-cloud/swoft
app/WebSocket/Chat/HomeController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Chat/HomeController.php
Apache-2.0
public function help(string $data): string { return '(home.ar)Recv: ' . $data; }
Message command is: 'help' @param string $data @MessageMapping("help", root=true) @return string
help
php
swoft-cloud/swoft
app/WebSocket/Chat/HomeController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Chat/HomeController.php
Apache-2.0
public function process(RequestInterface $request, MessageHandlerInterface $handler): ResponseInterface { $start = '>before '; CLog::info('before handle'); $resp = $handler->handle($request); $resp->setData($start . $resp->getData() . ' after>'); CLog::info('after handle'); return $resp; }
@param RequestInterface $request @param MessageHandlerInterface $handler @return ResponseInterface
process
php
swoft-cloud/swoft
app/WebSocket/Middleware/DemoMiddleware.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Middleware/DemoMiddleware.php
Apache-2.0
public function process(RequestInterface $request, MessageHandlerInterface $handler): ResponseInterface { $start = '>before '; CLog::info('before handle'); $resp = $handler->handle($request); $resp->setData($start . $resp->getData() . ' after>'); CLog::info('after handle'); \server()->log(__METHOD__, [], 'error'); return $resp; }
@param RequestInterface $request @param MessageHandlerInterface $handler @return ResponseInterface
process
php
swoft-cloud/swoft
app/WebSocket/Middleware/GlobalWsMiddleware.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Middleware/GlobalWsMiddleware.php
Apache-2.0
public function index(): void { Session::current()->push('hi, this is test.index'); }
Message command is: 'test.index' @return void @MessageMapping()
index
php
swoft-cloud/swoft
app/WebSocket/Test/TestController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Test/TestController.php
Apache-2.0
public function close(Message $msg): void { $data = $msg->getData(); /** @var Connection $conn */ $conn = Session::current(); $fd = is_numeric($data) ? (int)$data : $conn->getFd(); $conn->push("hi, will close conn $fd"); // disconnect $conn->getServer()->disconnect($fd); }
Message command is: 'test.index' @param Message $msg @return void @MessageMapping("close")
close
php
swoft-cloud/swoft
app/WebSocket/Test/TestController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Test/TestController.php
Apache-2.0
public function injectRequest(Request $req): void { $fd = $req->getFd(); Session::current()->push("(your FD: $fd)message data: " . json_encode($req->getMessage()->toArray())); }
Message command is: 'test.req' @param Request $req @return void @MessageMapping("req")
injectRequest
php
swoft-cloud/swoft
app/WebSocket/Test/TestController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Test/TestController.php
Apache-2.0
public function injectMessage(Message $msg): void { Session::current()->push('message data: ' . json_encode($msg->toArray())); }
Message command is: 'test.msg' @param Message $msg @return void @MessageMapping("msg")
injectMessage
php
swoft-cloud/swoft
app/WebSocket/Test/TestController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Test/TestController.php
Apache-2.0
public function echo(string $data): void { Session::current()->push('(echo)Recv: ' . $data); }
Message command is: 'echo' @param string $data @MessageMapping(root=true)
echo
php
swoft-cloud/swoft
app/WebSocket/Test/TestController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Test/TestController.php
Apache-2.0
public function hi(Request $req, Response $res): void { $fd = $req->getFd(); $ufd = (int)$req->getMessage()->getData(); if ($ufd < 1) { Session::current()->push('data must be an integer'); return; } $res->setFd($ufd)->setContent("Hi #{$ufd}, I am #{$fd}"); }
Message command is: 'echo' @param Request $req @param Response $res @MessageMapping(root=true)
hi
php
swoft-cloud/swoft
app/WebSocket/Test/TestController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Test/TestController.php
Apache-2.0
public function binary(string $data): string { // Session::current()->push('Binary: ' . $data, \WEBSOCKET_OPCODE_BINARY); return 'Binary: ' . $data; }
Message command is: 'bin' @MessageMapping("bin", root=true, opcode=2) @param string $data @return string
binary
php
swoft-cloud/swoft
app/WebSocket/Test/TestController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Test/TestController.php
Apache-2.0
public function pong(): void { Session::current()->push('pong!', WEBSOCKET_OPCODE_PONG); }
Message command is: 'ping' @MessageMapping("ping", root=true)
pong
php
swoft-cloud/swoft
app/WebSocket/Test/TestController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Test/TestController.php
Apache-2.0
public function autoReply(string $data): string { return '(home.ar)Recv: ' . $data; }
Message command is: 'test.ar' @MessageMapping("ar") @param string $data @return string
autoReply
php
swoft-cloud/swoft
app/WebSocket/Test/TestController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Test/TestController.php
Apache-2.0
public function testDie(): void { $wid = \server()->getPid('workerId'); \vdump($wid); \server()->stopWorker($wid); }
Message command is: 'test.ar' @MessageMapping("stop-worker")
testDie
php
swoft-cloud/swoft
app/WebSocket/Test/TestController.php
https://github.com/swoft-cloud/swoft/blob/master/app/WebSocket/Test/TestController.php
Apache-2.0
public function getPrefixDirs(): array { return [ __NAMESPACE__ => __DIR__, ]; }
Get namespace and dirs @return array
getPrefixDirs
php
swoft-cloud/swoft
test/testing/AutoLoader.php
https://github.com/swoft-cloud/swoft/blob/master/test/testing/AutoLoader.php
Apache-2.0
public static function new(string $basePath, array $config = []): SwoftApplication { return new static($basePath, $config); }
@param string $basePath @param array $config @return SwoftApplication
new
php
swoft-cloud/swoft
test/testing/TestApplication.php
https://github.com/swoft-cloud/swoft/blob/master/test/testing/TestApplication.php
Apache-2.0
public function __construct(string $basePath, array $config = []) { // RUN_SERVER_TEST=ws,tcp,http if (!defined('RUN_SERVER_TEST')) { define('RUN_SERVER_TEST', (string)getenv('RUN_SERVER_TEST')); } if (RUN_SERVER_TEST) { printf("ENV RUN_SERVER_TEST=%s\n", RUN_SERVER_TEST); } // tests: disable run console application $this->setStartConsole(false); $config = array_merge([ 'basePath' => $basePath, 'beanFile' => $basePath . '/app/bean.php', 'disabledAutoLoaders' => [ // \App\AutoLoader::class => 1, ], ], $config); parent::__construct($config); }
Class constructor. @param string $basePath @param array $config
__construct
php
swoft-cloud/swoft
test/testing/TestApplication.php
https://github.com/swoft-cloud/swoft/blob/master/test/testing/TestApplication.php
Apache-2.0
public function testChrOrd() { foreach (self::$utf8ValidityMap as $u => $t) { if ($t) { $this->assertSame($u, u::chr(u::ord($u))); } } }
@covers Patchwork\Utf8::chr @covers Patchwork\Utf8::ord
testChrOrd
php
tchwork/utf8
tests/Utf8Test.php
https://github.com/tchwork/utf8/blob/master/tests/Utf8Test.php
Apache-2.0
public function testIconvGetEncoding() { $a = array( 'input_encoding' => 'UTF-8', 'output_encoding' => 'UTF-8', 'internal_encoding' => 'UTF-8', ); foreach ($a as $t => $e) { $this->assertTrue(p::iconv_set_encoding($t, $e)); $this->assertSame($e, p::iconv_get_encoding($t)); } $this->assertSame($a, p::iconv_get_encoding('all')); $this->assertFalse(p::iconv_set_encoding('foo', 'UTF-8')); }
@covers Patchwork\PHP\Shim\Iconv::iconv_get_encoding @covers Patchwork\PHP\Shim\Iconv::iconv_set_encoding
testIconvGetEncoding
php
tchwork/utf8
tests/PHP/Shim/IconvTest.php
https://github.com/tchwork/utf8/blob/master/tests/PHP/Shim/IconvTest.php
Apache-2.0
public function testmb_stubs() { $this->assertFalse(p::mb_substitute_character('?')); $this->assertSame('none', p::mb_substitute_character()); $this->assertContains('UTF-8', p::mb_list_encodings()); $this->assertTrue(p::mb_internal_encoding('utf8')); $this->assertFalse(p::mb_internal_encoding('no-no')); $this->assertSame('UTF-8', p::mb_internal_encoding()); p::mb_encode_mimeheader(''); $this->assertFalse(true, 'mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead'); }
@covers Patchwork\PHP\Shim\Mbstring::mb_internal_encoding @covers Patchwork\PHP\Shim\Mbstring::mb_list_encodings @covers Patchwork\PHP\Shim\Mbstring::mb_substitute_character @expectedException PHPUnit\Framework\Error\Warning
testmb_stubs
php
tchwork/utf8
tests/PHP/Shim/MbstringTest.php
https://github.com/tchwork/utf8/blob/master/tests/PHP/Shim/MbstringTest.php
Apache-2.0
public function testmb_strpos_empty_delimiter() { try { mb_strpos('abc', ''); $this->assertFalse(true, 'The previous line should trigger a warning (Empty delimiter)'); } catch (\PHPUnit\Framework\Error\Warning $e) { p::mb_strpos('abc', ''); $this->assertFalse(true, 'The previous line should trigger a warning (Empty delimiter)'); } }
@covers Patchwork\PHP\Shim\Mbstring::mb_strpos @expectedException PHPUnit\Framework\Error\Warning
testmb_strpos_empty_delimiter
php
tchwork/utf8
tests/PHP/Shim/MbstringTest.php
https://github.com/tchwork/utf8/blob/master/tests/PHP/Shim/MbstringTest.php
Apache-2.0
public function testmb_strpos_negative_offset() { if (\PHP_VERSION_ID >= 70100) { $this->markTestSkipped('PHP >= 7.1 supports negative string offsets'); } try { mb_strpos('abc', 'a', -1); $this->assertFalse(true, 'The previous line should trigger a warning (Offset not contained in string)'); } catch (\PHPUnit\Framework\Error\Warning $e) { p::mb_strpos('abc', 'a', -1); $this->assertFalse(true, 'The previous line should trigger a warning (Offset not contained in string)'); } }
@covers Patchwork\PHP\Shim\Mbstring::mb_strpos @expectedException PHPUnit\Framework\Error\Warning
testmb_strpos_negative_offset
php
tchwork/utf8
tests/PHP/Shim/MbstringTest.php
https://github.com/tchwork/utf8/blob/master/tests/PHP/Shim/MbstringTest.php
Apache-2.0
public function testNormalizeConformance() { $t = file(__DIR__.'/NormalizationTest.'.$this->unicodeVersion.'.txt'); $c = array(); foreach ($t as $s) { $t = explode('#', $s); $t = explode(';', $t[0]); if (6 === count($t)) { foreach ($t as $k => $s) { $t = explode(' ', $s); $t = array_map('hexdec', $t); $t = array_map('Patchwork\Utf8::chr', $t); $c[$k] = implode('', $t); } $this->assertSame($c[1], pn::normalize($c[0], pn::NFC)); $this->assertSame($c[1], pn::normalize($c[1], pn::NFC)); $this->assertSame($c[1], pn::normalize($c[2], pn::NFC)); $this->assertSame($c[3], pn::normalize($c[3], pn::NFC)); $this->assertSame($c[3], pn::normalize($c[4], pn::NFC)); $this->assertSame($c[2], pn::normalize($c[0], pn::NFD)); $this->assertSame($c[2], pn::normalize($c[1], pn::NFD)); $this->assertSame($c[2], pn::normalize($c[2], pn::NFD)); $this->assertSame($c[4], pn::normalize($c[3], pn::NFD)); $this->assertSame($c[4], pn::normalize($c[4], pn::NFD)); $this->assertSame($c[3], pn::normalize($c[0], pn::NFKC)); $this->assertSame($c[3], pn::normalize($c[1], pn::NFKC)); $this->assertSame($c[3], pn::normalize($c[2], pn::NFKC)); $this->assertSame($c[3], pn::normalize($c[3], pn::NFKC)); $this->assertSame($c[3], pn::normalize($c[4], pn::NFKC)); $this->assertSame($c[4], pn::normalize($c[0], pn::NFKD)); $this->assertSame($c[4], pn::normalize($c[1], pn::NFKD)); $this->assertSame($c[4], pn::normalize($c[2], pn::NFKD)); $this->assertSame($c[4], pn::normalize($c[3], pn::NFKD)); $this->assertSame($c[4], pn::normalize($c[4], pn::NFKD)); } } }
@covers Patchwork\PHP\Shim\Normalizer::normalize @group unicode
testNormalizeConformance
php
tchwork/utf8
tests/PHP/Shim/NormalizerTest.php
https://github.com/tchwork/utf8/blob/master/tests/PHP/Shim/NormalizerTest.php
Apache-2.0
public function __construct(array $elements = []) { $this->elements = $elements; }
Construct new instance @param array $elements
__construct
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public static function create(array $elements = []) { return new static($elements); }
Create a new instance. @param array $elements @return AbstractArray Returns created instance
create
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public static function createFromJson($json, $options = 0, $depth = 512) { return new static(json_decode($json, true, $depth, $options)); }
Decode a JSON string to new instance. @param string $json The JSON string being decoded @param int $options Bitmask of JSON decode options @param int $depth Specified recursion depth @return AbstractArray The created array
createFromJson
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public static function createFromObject(ArrayAccess $elements) { $array = new static(); foreach ($elements as $key => $value) { $array[$key] = $value; } return $array; }
Create a new instance filled with values from an object implementing ArrayAccess. @param ArrayAccess $elements Object that implements ArrayAccess @return AbstractArray Returns created instance
createFromObject
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public static function createFromString($string, $separator) { return new static(explode($separator, $string)); }
Explode a string to new instance by specified separator. @param string $string Converted string @param string $separator Element's separator @return AbstractArray The created array
createFromString
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public static function createWithRange($low, $high, $step = 1) { return new static(range($low, $high, $step)); }
Create a new instance containing a range of elements. @param mixed $low First value of the sequence @param mixed $high The sequence is ended upon reaching the end value @param int $step Used as the increment between elements in the sequence @return AbstractArray The created array
createWithRange
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function contains($element) { return in_array($element, $this->elements, true); }
Check if the given value exists in the array. @param mixed $element Value to search for @return bool Returns true if the given value exists in the array, false otherwise
contains
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function containsKey($key) { return array_key_exists($key, $this->elements); }
Check if the given key/index exists in the array. @param mixed $key Key/index to search for @return bool Returns true if the given key/index exists in the array, false otherwise
containsKey
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function count() { return count($this->elements); }
Returns the number of values in the array. @link http://php.net/manual/en/function.count.php @return int total number of values
count
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function createClone() { return clone $this; }
Clone current instance to new instance. @deprecated Should be removed @return AbstractArray Shallow copy of $this
createClone
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function except(array $keys) { return new static(array_diff_key($this->elements, array_flip($keys))); }
Return slice of an array except given keys. @param array $keys List of keys to exclude @return AbstractArray The created array
except
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function exists(callable $func) { $isExists = false; foreach ($this->elements as $key => $value) { if ($func($key, $value)) { $isExists = true; break; } } return $isExists; }
Find the given value in An array using a closure @param callable $func @return bool Returns true if the given value is found, false otherwise
exists
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function find(callable $func) { $found = null; foreach ($this->elements as $key => $value) { if($func($value, $key)) { $found = $value; break; } } return $found; }
Returns the first occurrence of a value that satisfies the predicate $func. @param callable $func @return mixed The first occurrence found
find
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function getIterator() { return new ArrayIterator($this->elements); }
Create an iterator over this array. @link http://php.net/manual/en/iteratoraggregate.getiterator.php @return Traversable An instance of an object implementing <b>Iterator</b>
getIterator
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function getKeys() { return array_keys($this->elements); }
Return an array all the keys of this array. @return array An array of all keys
getKeys
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function getRandom() { return $this->offsetGet($this->getRandomKey()); }
Pick a random value out of this array. @return mixed Random value of array @throws \RangeException If array is empty
getRandom
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function getRandomKey() { return $this->getRandomKeys(1); }
Pick a random key/index from the keys of this array. @return mixed Random key/index of array @throws \RangeException If array is empty
getRandomKey
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function getRandomKeys($number) { $number = (int) $number; $count = $this->count(); if ($number === 0 || $number > $count) { throw new \RangeException(sprintf( 'Number of requested keys (%s) must be equal or lower than number of elements in this array (%s)', $number, $count )); } return array_rand($this->elements, $number); }
Pick a given number of random keys/indexes out of this array. @param int $number The number of keys/indexes (should be <= $this->count()) @return mixed Random keys or key of array @throws \RangeException
getRandomKeys
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function getRandomValues($number) { $values = []; $keys = $number > 1 ? $this->getRandomKeys($number) : [$this->getRandomKeys($number)]; foreach ($keys as $key) { $values[] = $this->offsetGet($key); } return $values; }
Pick a given number of random values with non-duplicate indexes out of the array. @param int $number The number of values (should be > 1 and < $this->count()) @return array Random values of array @throws \RangeException
getRandomValues
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function getValues() { return array_values($this->elements); }
Return an array of all values from this array numerically indexed. @return mixed An array of all values
getValues
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function indexOf($element) { return $this->search($element); }
Alias of search() method. Search for a given element and return the index of its first occurrence. @param mixed $element Value to search for @return mixed The corresponding key/index
indexOf
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function isAssoc() { $isAssoc = false; if (!$this->isEmpty()) { $isAssoc = $this->checkType('string'); } return $isAssoc; }
Check whether array is associative or not. @return bool Returns true if associative, false otherwise
isAssoc
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function isEmpty() { return !$this->elements; }
Check whether the array is empty or not. @return bool Returns true if empty, false otherwise
isEmpty
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
private function checkType($type) { $isInType = true; foreach ($this->getKeys() as $key) { if (gettype($key) !== $type) { $isInType = false; break; } } return $isInType; }
Check if an array keys are in a given variable type. @param string $type @return bool
checkType
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT
public function isNumeric() { $isNumeric = false; if (!$this->isEmpty()) { $isNumeric = $this->checkType('integer'); } return $isNumeric; }
Check whether array is numeric or not. @return bool Returns true if numeric, false otherwise
isNumeric
php
bocharsky-bw/Arrayzy
src/AbstractArray.php
https://github.com/bocharsky-bw/Arrayzy/blob/master/src/AbstractArray.php
MIT