Skip to content

Commit 61385f5

Browse files
committed
init project
Signed-off-by: inhere <in.798@qq.com>
0 parents  commit 61385f5

File tree

7 files changed

+1157
-0
lines changed

7 files changed

+1157
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.idea/
2+
.phpintel/
3+
vendor/
4+
!README.md
5+
!.gitkeep
6+
composer.lock
7+
*.swp
8+
*.log
9+
*.zip
10+
*.pid
11+
*.patch
12+
.DS_Store

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 inhere
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "toolkit/extlib",
3+
"type": "library",
4+
"description": "some useful http client library of the php",
5+
"keywords": [
6+
"library",
7+
"tool",
8+
"http",
9+
"curl"
10+
],
11+
"homepage": "https://github.com/php-toolkit/extlib",
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "inhere",
16+
"email": "in.798@qq.com",
17+
"homepage": "https://github.com/inhere"
18+
}
19+
],
20+
"require": {
21+
"php": ">7.1.0",
22+
"toolkit/cli-utils": "^1.0",
23+
"toolkit/stdlib": "^1.0"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"Toolkit\\Extlib\\": "src/"
28+
}
29+
},
30+
"suggest": {
31+
"php-comp/http-message": "Very lightweight PSR-7 implements http message component"
32+
}
33+
}

phpunit.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
bootstrap="test/boot.php"
6+
colors="false"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false"
11+
>
12+
<testsuites>
13+
<testsuite name="php-comp http-client Test Suite">
14+
<directory>test</directory>
15+
</testsuite>
16+
</testsuites>
17+
18+
<filter>
19+
<whitelist>
20+
<directory suffix=".php">src</directory>
21+
</whitelist>
22+
</filter>
23+
</phpunit>

src/Concern/NameAliasTrait.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-02-28
6+
* Time: 9:20
7+
*/
8+
9+
namespace Toolkit\Extlib\Concern;
10+
11+
use InvalidArgumentException;
12+
13+
/**
14+
* Class NameAliasTrait
15+
*
16+
* @package Inhere\Console\Concern
17+
*/
18+
trait NameAliasTrait
19+
{
20+
/**
21+
* @var array
22+
*/
23+
private $aliases = [];
24+
25+
/**
26+
* set name alias(es)
27+
*
28+
* @param string $name
29+
* @param string|array $alias
30+
* @param bool $validate
31+
*/
32+
public function setAlias(string $name, $alias, bool $validate = false): void
33+
{
34+
foreach ((array)$alias as $aliasName) {
35+
if (!isset($this->aliases[$aliasName])) {
36+
$this->aliases[$aliasName] = $name;
37+
} elseif ($validate) {
38+
$oldName = $this->aliases[$aliasName];
39+
throw new InvalidArgumentException("Alias '{$aliasName}' has been registered by '{$oldName}', cannot assign to the '{$name}'");
40+
}
41+
}
42+
}
43+
44+
/**
45+
* Get real name by alias
46+
*
47+
* @param string $alias
48+
*
49+
* @return mixed
50+
*/
51+
public function resolveAlias(string $alias): string
52+
{
53+
return $this->aliases[$alias] ?? $alias;
54+
}
55+
56+
/**
57+
* @param string $alias
58+
*
59+
* @return bool
60+
*/
61+
public function hasAlias(string $alias): bool
62+
{
63+
return isset($this->aliases[$alias]);
64+
}
65+
66+
/**
67+
* @param string $name
68+
*
69+
* @return array
70+
*/
71+
public function getAliases(string $name = ''): array
72+
{
73+
if ($name) {
74+
$aliases = [];
75+
foreach ($this->aliases as $alias => $n) {
76+
if ($name === $n) {
77+
$aliases[] = $alias;
78+
}
79+
}
80+
81+
return $aliases;
82+
}
83+
84+
return $this->aliases;
85+
}
86+
}
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-03-27
6+
* Time: 16:17
7+
*/
8+
9+
namespace Toolkit\Extlib\Concern;
10+
11+
use function count;
12+
use function in_array;
13+
14+
/**
15+
* Class SimpleEventStaticTrait
16+
*
17+
* @package Inhere\Console\Concern
18+
*/
19+
trait SimpleEventAwareTrait
20+
{
21+
/**
22+
* set the supported events, if you need.
23+
* if it is empty, will allow register any event.
24+
*
25+
* @var array
26+
*/
27+
protected static $supportedEvents = [];
28+
29+
/**
30+
* registered Events
31+
*
32+
* @var array
33+
* [
34+
* 'event' => bool, // is once event
35+
* ]
36+
*/
37+
private static $events = [];
38+
39+
/**
40+
* events and handlers
41+
*
42+
* @var array
43+
* [
44+
* 'event' => callable, // event handler
45+
* ]
46+
*/
47+
private static $eventHandlers = [];
48+
49+
/**
50+
* register a event handler
51+
*
52+
* @param string $event
53+
* @param callable $handler
54+
* @param bool $once
55+
*/
56+
public function on(string $event, callable $handler, bool $once = false): void
57+
{
58+
if (self::isSupportedEvent($event)) {
59+
self::$eventHandlers[$event][] = $handler;
60+
61+
if (!isset(self::$events[$event])) {
62+
self::$events[$event] = $once;
63+
}
64+
}
65+
}
66+
67+
/**
68+
* register a once event handler
69+
*
70+
* @param string $event
71+
* @param callable $handler
72+
*/
73+
public function once(string $event, callable $handler): void
74+
{
75+
$this->on($event, $handler, true);
76+
}
77+
78+
/**
79+
* trigger event
80+
*
81+
* @param string $event
82+
* @param mixed ...$args
83+
*
84+
* @return bool
85+
*/
86+
public function fire(string $event, ...$args): bool
87+
{
88+
if (!isset(self::$events[$event])) {
89+
return false;
90+
}
91+
92+
// call event handlers of the event.
93+
/** @var mixed $return */
94+
$return = true;
95+
foreach ((array)self::$eventHandlers[$event] as $cb) {
96+
$return = $cb(...$args);
97+
// return FALSE to stop go on handle.
98+
if (false === $return) {
99+
break;
100+
}
101+
}
102+
103+
// is a once event, remove it
104+
if (self::$events[$event]) {
105+
return $this->off($event);
106+
}
107+
108+
return (bool)$return;
109+
}
110+
111+
/**
112+
* remove event and it's handlers
113+
*
114+
* @param $event
115+
*
116+
* @return bool
117+
*/
118+
public function off(string $event): bool
119+
{
120+
if ($this->hasEvent($event)) {
121+
unset(self::$events[$event], self::$eventHandlers[$event]);
122+
123+
return true;
124+
}
125+
126+
return false;
127+
}
128+
129+
/**
130+
* @param string $event
131+
*
132+
* @return bool
133+
*/
134+
public function hasEvent(string $event): bool
135+
{
136+
return isset(self::$events[$event]);
137+
}
138+
139+
/**
140+
* @param string $event
141+
*
142+
* @return bool
143+
*/
144+
public function isOnce(string $event): bool
145+
{
146+
if ($this->hasEvent($event)) {
147+
return self::$events[$event];
148+
}
149+
150+
return false;
151+
}
152+
153+
/**
154+
* check $name is a supported event name
155+
*
156+
* @param string $event
157+
*
158+
* @return bool
159+
*/
160+
public static function isSupportedEvent(string $event): bool
161+
{
162+
if (!$event || !preg_match('/[a-zA-z][\w-]+/', $event)) {
163+
return false;
164+
}
165+
166+
if ($ets = self::$supportedEvents) {
167+
return in_array($event, $ets, true);
168+
}
169+
170+
return true;
171+
}
172+
173+
/**
174+
* @return array
175+
*/
176+
public static function getSupportEvents(): array
177+
{
178+
return self::$supportedEvents;
179+
}
180+
181+
/**
182+
* @param array $supportedEvents
183+
*/
184+
public static function setSupportEvents(array $supportedEvents): void
185+
{
186+
self::$supportedEvents = $supportedEvents;
187+
}
188+
189+
/**
190+
* @return array
191+
*/
192+
public static function getEvents(): array
193+
{
194+
return self::$events;
195+
}
196+
197+
/**
198+
* @return int
199+
*/
200+
public static function countEvents(): int
201+
{
202+
return count(self::$events);
203+
}
204+
}

0 commit comments

Comments
 (0)