Skip to content

Commit f0794d6

Browse files
committed
Update
1 parent 47210d3 commit f0794d6

3 files changed

Lines changed: 30 additions & 21 deletions

File tree

src/Autoload.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,18 @@ class Autoload
3333
{
3434
public static function start(?string $arg = null, ?Server $server = null): void
3535
{
36-
if (in_array($arg ?? '', ['start', 'restart', 'stop', 'status', 'reload', 'connections'])) {
37-
// Предзагрузка
38-
Config::reloadAll(['route', 'container']);
39-
static::system();
40-
} else if ($server instanceof Server) {
41-
// При старте сервера
42-
Config::reloadAll(['route']);
43-
register_shutdown_function(fn($s): int|bool => (time() - $s <= 1) ? sleep(1) : true, time());
44-
static::system();
45-
static::files();
46-
Bootstrap::start($server);
36+
Config::reloadAll(['route']);
37+
static::system();
38+
39+
if (($server instanceof Server)
40+
|| !in_array($arg ?? '', ['start', 'restart', 'stop', 'status', 'reload', 'connections'])
41+
) {
42+
if ($server instanceof Server) {
43+
register_shutdown_function(fn($s): int|bool => (time() - $s <= 0.1) ? sleep(1) : true, time());
44+
}
4745
Context::init();
48-
} else {
49-
// CLI
50-
Config::reloadAll(['route']);
51-
set_error_handler(fn($l, $m, $f = '', $n = 0): bool => (error_reporting() & $l) ? throw new ErrorException($m, 0, $l, $f, $n) : true);
52-
static::system();
5346
static::files();
54-
Bootstrap::start();
47+
Bootstrap::start($server);
5548
}
5649
}
5750

@@ -60,6 +53,7 @@ private static function system(): void
6053
ini_set('display_errors', 'on');
6154
error_reporting(config('server.error_reporting', E_ALL));
6255
date_default_timezone_set(config('server.default_timezone', config('app.default_timezone', 'Europe/Moscow')));
56+
set_error_handler(fn($l, $m, $f = '', $n = 0): bool => (error_reporting() & $l) ? throw new ErrorException($m, 0, $l, $f, $n) : true);
6357
}
6458

6559
public static function files(): void

src/Context.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use localzet\Server\Events\Linux;
3333
use localzet\Server\Events\Swoole;
3434
use localzet\Server\Events\Swow;
35-
use SplObjectStorage;
3635
use StdClass;
3736
use WeakMap;
3837
use function property_exists;
@@ -46,7 +45,7 @@ class Context
4645
{
4746
protected static ?WeakMap $objectStorage = null;
4847

49-
protected static StdClass $object;
48+
protected static ?StdClass $object = null;
5049

5150
/**
5251
* Получить значение из контекста
@@ -63,7 +62,7 @@ public static function get(?string $key = null): mixed
6362
public static function init(): void
6463
{
6564
if (!(static::$objectStorage instanceof WeakMap)) {
66-
static::$objectStorage = class_exists(WeakMap::class) ? new WeakMap() : new SplObjectStorage();
65+
static::$objectStorage = new WeakMap();
6766
static::$object = new StdClass;
6867
}
6968
}
@@ -124,11 +123,27 @@ public static function has(string $key): bool
124123
return $key && ($obj = static::getObject()) && property_exists($obj, $key);
125124
}
126125

126+
/**
127+
* @param $callback
128+
* @return void
129+
*/
130+
public static function onDestroy($callback): void
131+
{
132+
$obj = static::getObject();
133+
$obj->destroyCallbacks[] = $callback;
134+
}
135+
127136
/**
128137
* Уничтожить контекст
129138
*/
130139
public static function destroy(): void
131140
{
141+
$obj = static::getObject();
142+
if (isset($obj->destroyCallbacks)) {
143+
foreach ($obj->destroyCallbacks as $callback) {
144+
$callback();
145+
}
146+
}
132147
if ($key = static::getKey()) {
133148
unset(static::$objectStorage[$key]);
134149
}

src/Install/master

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use Triangle\Engine\Autoload;
2929
use Triangle\Engine\Environment;
3030
use Triangle\Engine\Path;
3131

32-
chdir(__DIR__);
32+
if (!Phar::running()) chdir(__DIR__);
3333
require_once __DIR__ . '/vendor/autoload.php';
3434

3535
// Основная директория проекта

0 commit comments

Comments
 (0)