Skip to content

Commit

Permalink
улучшена совместная работа с Laravel
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Surkov committed Aug 28, 2021
1 parent 6224af3 commit e0af3b1
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dissonance/full",
"description": "Полная сборка пакетов фреймворка Dissonance.",
"license": "BSD-3-Clause",
"version": "1.1.0",
"version": "1.1.1",
"authors": [{
"name": "Surkov Sergey",
"role": "creator"
Expand Down
2 changes: 1 addition & 1 deletion src/Container/ItemsContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function has(string|int $key): bool
* @return bool
* @info
*/
private function hasBy(string|int $key, \ArrayAccess|array &$items): bool
private function hasBy(string|int $key, array &$items): bool
{
return isset($items[$key]) // isset в 4 раза быстрее array_key_exists
|| (is_array($items) && array_key_exists($key, $items))
Expand Down
24 changes: 20 additions & 4 deletions src/Container/SubContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ public function bind(string $abstract, Closure|string $concrete = null, bool $sh
// If the abstract type was already resolved in this container we'll fire the
// rebound listener so that any objects which have already gotten resolved
// can have their copy of the object updated via the listener callbacks.
if ($this->resolved($abstract)) {
$alias = $this->getAlias($abstract);
if (isset($this->resolved[$alias]) || isset($this->instances[$alias])) {
$this->rebound($abstract);
}

Expand All @@ -184,6 +185,16 @@ public function rebinding(string $abstract, Closure $callback)
public function resolve(string $abstract, array $parameters = [], bool $raiseEvents = true)
{
$alias = $this->getAlias($abstract);
/// сначала получаем по ключу у нас
/// иначе вернет из ядра по алиасу
/// проблема одноименнных сервисов с алиасами
if (!$parameters && isset($this->instances[$abstract])) {
return $this->instances[$abstract];
}
if (isset($this->bindings[$abstract])) {
return $this->app->build($this->bindings[$abstract]['concrete']);
}
// передаем родителю
if (!$parameters && isset($this->instances[$alias])) {
return $this->instances[$alias];
}
Expand Down Expand Up @@ -363,8 +374,10 @@ public function resolving($abstract, callable $callback = null)
if (is_string($abstract)) {
$abstract = $this->getAlias($abstract);
}

$this->app->resolving($abstract, $callback);
// нам не нужен чужой контейнер, ставим текущий
$this->app->resolving($abstract, function (object $object, $app) use ($callback) {
return $callback($object, $this);
});
}

/**
Expand All @@ -380,7 +393,10 @@ public function afterResolving($abstract, callable $callback = null)
$abstract = $this->getAlias($abstract);
}

$this->app->afterResolving($abstract, $callback);
// нам не нужен чужой контейнер, ставим текущий
$this->app->afterResolving($abstract, function (object $object, $app) use ($callback) {
return $callback($object, $this);
});
}


Expand Down
1 change: 0 additions & 1 deletion src/Core/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public static function registerNamespaces()
protected static function loadPackages($dir)
{
/*foreach(new \DirectoryIterator($dir) as $fileInfo) {*/

$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_SELF));
$iterator->setMaxDepth(2);
foreach ($iterator as $fileInfo) {
Expand Down
38 changes: 34 additions & 4 deletions src/Http/Kernel/HttpRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function run(): void
* @var CoreInterface $app
*/
$app = $this->app;
$symbiosis = \_DS\config('symbiosis',false);
try {
$request_interface = ServerRequestInterface::class;
$request = $app[PsrHttpFactory::class]->createServerRequestFromGlobals();
Expand Down Expand Up @@ -54,25 +55,49 @@ public function run(): void
})
);
$response = $handler->handle($request);

// Определяем нужно ли отдавать ответ
if (!$app('destroy_response', false) || !\_DS\config('symbiosis')) {
if (!$app('destroy_response', false) || !$symbiosis) {
$this->sendResponse($response);
// при режиме симбиоза не даем другим скриптам продолжить работу, т.к. отдали наш ответ
if (\_DS\config('symbiosis')) {
if ($symbiosis) {
exit;// завершаем работу
}
} else {
// Открываем проксирование буфера через нас
}

} catch (\Throwable $e) {
// при режиме симбиоза не отдаем ответ с ошибкой, запишем выше в лог
if (!\_DS\config('symbiosis')) {
if (!$symbiosis) {
$this->sendResponse($app[HttpKernelInterface::class]->response(500, $e));
} else {
// TODO:
// TODO:log
}
}
}


/**
* Laravel close buffers
* @param int $targetLevel
* @param bool $flush
*/
public static function closeOutputBuffers(int $targetLevel, bool $flush): void
{
$status = ob_get_status(true);
$level = \count($status);
$flags = \PHP_OUTPUT_HANDLER_REMOVABLE | ($flush ? \PHP_OUTPUT_HANDLER_FLUSHABLE : \PHP_OUTPUT_HANDLER_CLEANABLE);

while ($level-- > $targetLevel && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || ($s['flags'] & $flags) === $flags : $s['del'])) {
if ($flush) {
ob_end_flush();
} else {
ob_end_clean();
}
}
}

protected function prepareBaseUrl(ServerRequestInterface $request): string
{
$server = $request->getServerParams();
Expand Down Expand Up @@ -106,6 +131,11 @@ public function sendResponse(ResponseInterface $response)
{
$sender = new ResponseSender($response);
$sender->render();
if (\function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
} elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
static::closeOutputBuffers(0, true);
}
}

}
2 changes: 1 addition & 1 deletion src/SimpleCacheFilesystem/SimpleCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(string $cache_directory, int $default_ttl = 600)
{
if (!is_dir($cache_directory)) {
$uMask = umask(0);
mkdir($cache_directory, 0755, true);
@mkdir($cache_directory, 0755, true);
umask($uMask);
}
if (!is_dir($cache_directory) || !is_writable($cache_directory)) {
Expand Down
1 change: 0 additions & 1 deletion src/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
'storage_path' => $basePath . '/storage', // Если убрать то кеш отключится
'packages_paths' => [
$basePath . '/vendor', // Папка для приложений
$basePath . '/modules', // Папка для приложений
],
'bootstrappers' => [
\Dissonance\Develop\Bootstrap\DebugBootstrap::class,/// debug only
Expand Down

0 comments on commit e0af3b1

Please sign in to comment.