This commit is contained in:
LayFi.de
2025-01-27 20:49:47 +08:00
commit 4f3242bca7
2745 changed files with 309418 additions and 0 deletions
+21
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
View File
@@ -0,0 +1,4 @@
# event
webman event plugin
https://www.workerman.net/plugin/64
+13
View File
@@ -0,0 +1,13 @@
{
"name": "webman/event",
"type": "library",
"license": "MIT",
"description": "Webman event plugin",
"require": {
},
"autoload": {
"psr-4": {
"Webman\\Event\\": "src"
}
}
}
+90
View File
@@ -0,0 +1,90 @@
<?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()]);
foreach (static::$events as $name => $events) {
// 支持排序,1 2 3 ... 9 a b c...z
ksort($events, SORT_NATURAL);
foreach ($events as $callbacks) {
foreach ($callbacks as $callback) {
Event::on($name, $callback);
}
}
}
}
/**
* @param $callbacks
* @return array|mixed
*/
protected static function convertCallable($callbacks)
{
if (\is_array($callbacks)) {
$callback = \array_values($callbacks);
if (isset($callback[1]) && \is_string($callback[0]) && \class_exists($callback[0])) {
return [Container::get($callback[0]), $callback[1]];
}
}
return $callbacks;
}
/**
* @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'])) {
foreach ($config['event'] as $event_name => $callbacks) {
$callbacks = static::convertCallable($callbacks);
if (is_callable($callbacks)) {
static::$events[$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;
}
ksort($callbacks, SORT_NATURAL);
foreach ($callbacks as $id => $callback) {
$callback = static::convertCallable($callback);
if (is_callable($callback)) {
static::$events[$event_name][$id][] = $callback;
continue;
}
$msg = "Events: $event_name => " . var_export($callback, true) . " is not callable\n";
echo $msg;
Log::error($msg);
}
}
unset($config['event']);
}
static::getEvents($config);
}
}
}
+161
View File
@@ -0,0 +1,161 @@
<?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 mixed $event_name
* @param callable $listener
* @return int
*/
public static function on($event_name, callable $listener): int
{
$is_prefix_name = $event_name[strlen($event_name) - 1] === '*';
if ($is_prefix_name) {
static::$prefixEventMap[substr($event_name, 0, -1)][++static::$id] = $listener;
} else {
static::$eventMap[$event_name][++static::$id] = $listener;
}
return static::$id;
}
/**
* @param mixed $event_name
* @param integer $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, bool $halt = false)
{
$listeners = static::getListeners($event_name);
$responses = [];
foreach ($listeners as $listener) {
try {
$response = $listener($data, $event_name);
} catch (\Throwable $e) {
$responses[] = $e;
if (!static::$logger && is_callable('\support\Log::error')) {
static::$logger = Log::channel();
}
if (static::$logger) {
static::$logger->error($e);
}
continue;
}
$responses[] = $response;
if ($halt && !is_null($response)) {
return $response;
}
if ($response === false) {
break;
}
}
return $halt ? null : $responses;
}
/**
* @param mixed $event_name
* @param mixed $data
* @param bool $halt
* @return array|null|mixed
*/
public static function dispatch($event_name, $data, bool $halt = false)
{
$listeners = static::getListeners($event_name);
$responses = [];
foreach ($listeners as $listener) {
$response = $listener($data, $event_name);
$responses[] = $response;
if ($halt && !is_null($response)) {
return $response;
}
if ($response === false) {
break;
}
}
return $halt ? null : $responses;
}
/**
* @return array
*/
public static function list(): array
{
$listeners = [];
foreach (static::$eventMap as $event_name => $callback_items) {
foreach ($callback_items as $id => $callback_item) {
$listeners[$id] = [$event_name, $callback_item];
}
}
foreach (static::$prefixEventMap as $event_name => $callback_items) {
foreach ($callback_items as $id => $callback_item) {
$listeners[$id] = [$event_name . '*', $callback_item];
}
}
ksort($listeners);
return $listeners;
}
/**
* @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));
}
}
+50
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): int
{
$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
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);
}
}
}
@@ -0,0 +1,4 @@
<?php
return [
'enable' => true,
];
@@ -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,
];
@@ -0,0 +1,7 @@
<?php
use Webman\Event\EventListCommand;
return [
EventListCommand::class
];