first commit

This commit is contained in:
Mr.Qin
2022-08-19 19:48:37 +08:00
commit afdd648b65
3275 changed files with 631084 additions and 0 deletions

21
vendor/webman/event/LICENSE vendored Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 webman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

4
vendor/webman/event/README.md vendored Normal file
View File

@@ -0,0 +1,4 @@
# event
webman event plugin
https://www.workerman.net/plugin/64

13
vendor/webman/event/composer.json vendored Normal file
View File

@@ -0,0 +1,13 @@
{
"name": "webman/event",
"type": "library",
"license": "MIT",
"description": "Webman event plugin",
"require": {
},
"autoload": {
"psr-4": {
"Webman\\Event\\": "src"
}
}
}

79
vendor/webman/event/src/BootStrap.php vendored Normal file
View File

@@ -0,0 +1,79 @@
<?php
namespace Webman\Event;
use support\Container;
use support\Log;
class BootStrap implements \Webman\Bootstrap
{
/**
* @var array
*/
protected static $events = [];
/**
* @param $worker
* @return mixed|void
*/
public static function start($worker)
{
static::getEvents([config()]);
}
protected static function convertCallable($callback)
{
if (\is_array($callback)) {
$callback = \array_values($callback);
if (isset($callback[1]) && \is_string($callback[0]) && \class_exists($callback[0])) {
$callback = [Container::get($callback[0]), $callback[1]];
}
}
return $callback;
}
/**
* @param $configs
* @return void
*/
protected static function getEvents($configs)
{
foreach ($configs as $config) {
if (!is_array($config)) {
continue;
}
if (isset($config['event']) && is_array($config['event']) && !isset($config['event']['app']['enable'])) {
$events = [];
foreach ($config['event'] as $event_name => $callbacks) {
$callbacks = static::convertCallable($callbacks);
if (is_callable($callbacks)) {
$events[$event_name] = [$callbacks];
Event::on($event_name, $callbacks);
continue;
}
if (!is_array($callbacks)) {
$msg = "Events: $event_name => " .var_export($callbacks, true) . " is not callable\n";
echo $msg;
Log::error($msg);
continue;
}
foreach ($callbacks as $callback) {
$callback = static::convertCallable($callback);
if (is_callable($callback)) {
$events[$event_name][] = $callback;
Event::on($event_name, $callback);
continue;
}
$msg = "Events: $event_name => " . var_export($callback, true) . " is not callable\n";
echo $msg;
Log::error($msg);
}
}
static::$events = array_merge_recursive(static::$events, $events);
unset($config['event']);
}
static::getEvents($config);
}
}
}

138
vendor/webman/event/src/Event.php vendored Normal file
View File

@@ -0,0 +1,138 @@
<?php
namespace Webman\Event;
use Psr\Log\LoggerInterface;
use support\Log;
class Event
{
/**
* @var array
*/
protected static $eventMap = [];
/**
* @var array
*/
protected static $prefixEventMap = [];
/**
* @var int
*/
protected static $id = 0;
/**
* @var LoggerInterface
*/
protected static $logger;
/**
* @param $event_name
* @param $callback
* @return int
*/
public static function on($event_name, callable $callback): int
{
$is_prefix_name = $event_name[strlen($event_name)-1] === '*';
if ($is_prefix_name) {
static::$prefixEventMap[substr($event_name, 0, -1)][++static::$id] = $callback;
} else {
static::$eventMap[$event_name][++static::$id] = $callback;
}
return static::$id;
}
/**
* @param $event_name
* @param $id
* @return int
*/
public static function off($event_name, int $id): int
{
if (isset(static::$eventMap[$event_name][$id])) {
unset(static::$eventMap[$event_name][$id]);
return 1;
}
return 0;
}
/**
* @param mixed $event_name
* @param mixed $data
* @param bool $halt
* @return array|null|mixed
*/
public static function emit($event_name, $data, $halt = false)
{
$success_count = 0;
$listeners = static::getListeners($event_name);
$responses = [];
foreach ($listeners as $listener) {
try {
$response = $listener($data, $event_name);
} catch (\Throwable $th) {
if (!static::$logger && is_callable('\support\Log::error')) {
static::$logger = Log::channel();
}
if (static::$logger) {
static::$logger->error($th->getMessage());
}
continue;
}
$responses[] = $response;
if ($halt && !is_null($response)) {
return $response;
}
}
return $halt ? null : $responses;
}
/**
* @return array
*/
public static function list(): array
{
$callbacks = [];
foreach (static::$eventMap as $event_name => $callback_items) {
foreach ($callback_items as $id => $callback_item) {
$callbacks[$id] = [$event_name, $callback_item];
}
}
foreach (static::$prefixEventMap as $event_name => $callback_items) {
foreach ($callback_items as $id => $callback_item) {
$callbacks[$id] = [$event_name.'*', $callback_item];
}
}
ksort($callbacks);
return $callbacks;
}
/**
* @param mixed $event_name
* @return callable[]
*/
public static function getListeners($event_name): array
{
$listeners = static::$eventMap[$event_name] ?? [];
foreach (static::$prefixEventMap as $name => $callback_items) {
if (strpos($event_name, $name) === 0) {
$listeners = array_merge($listeners, $callback_items);
}
}
ksort($listeners);
return $listeners;
}
/**
* @param mixed $event_name
* @return bool
*/
public static function hasListener($event_name): bool
{
return !empty(static::getListeners($event_name));
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Webman\Event;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class EventListCommand extends Command
{
protected static $defaultName = 'event:list';
protected static $defaultDescription = 'Show event list';
/**
* @return void
*/
protected function configure()
{
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$headers = ['id', 'event_name', 'callback'];
$rows = [];
foreach (Event::list() as $id => $item) {
$event_name = $item[0];
$callback = $item[1];
if (is_array($callback) && is_object($callback[0])) {
$callback[0] = get_class($callback[0]);
}
$cb = $callback instanceof \Closure ? 'Closure' : (is_array($callback) ? json_encode($callback) : var_export($callback, 1));
$rows[] = [$id, $event_name, $cb];
}
$table = new Table($output);
$table->setHeaders($headers);
$table->setRows($rows);
$table->render();
return self::SUCCESS;
}
}

82
vendor/webman/event/src/Install.php vendored Normal file
View File

@@ -0,0 +1,82 @@
<?php
namespace Webman\Event;
class Install
{
const WEBMAN_PLUGIN = true;
/**
* @var array
*/
protected static $pathRelation = array (
'config/plugin/webman/event' => 'config/plugin/webman/event',
);
/**
* Install
* @return void
*/
public static function install()
{
static::installByRelation();
$event_config_path = config_path() . '/event.php';
if (!is_file($event_config_path)) {
file_put_contents($event_config_path, "<?php\n\nreturn [\n \n];\n");
}
}
/**
* Uninstall
* @return void
*/
public static function uninstall()
{
$event_config_path = config_path() . '/event.php';
if (is_file($event_config_path)) {
unlink($event_config_path);
}
self::uninstallByRelation();
}
/**
* installByRelation
* @return void
*/
public static function installByRelation()
{
foreach (static::$pathRelation as $source => $dest) {
if ($pos = strrpos($dest, '/')) {
$parent_dir = base_path().'/'.substr($dest, 0, $pos);
if (!is_dir($parent_dir)) {
mkdir($parent_dir, 0777, true);
}
}
//symlink(__DIR__ . "/$source", base_path()."/$dest");
copy_dir(__DIR__ . "/$source", base_path()."/$dest");
echo "Create $dest
";
}
}
/**
* uninstallByRelation
* @return void
*/
public static function uninstallByRelation()
{
foreach (static::$pathRelation as $source => $dest) {
$path = base_path()."/$dest";
if (!is_dir($path) && !is_file($path)) {
continue;
}
echo "Remove $dest
";
if (is_file($path) || is_link($path)) {
unlink($path);
continue;
}
remove_dir($path);
}
}
}

View File

@@ -0,0 +1,4 @@
<?php
return [
'enable' => true,
];

View File

@@ -0,0 +1,17 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
return [
Webman\Event\BootStrap::class,
];

View File

@@ -0,0 +1,7 @@
<?php
use Webman\Event\EventListCommand;
return [
EventListCommand::class
];