我有一個問題,與其問如何解決它,不如更好地了解正在發生的事情,以便我自己解決它。
使用 api-platform 2.6.6,我成功地向 SwaggerUI 添加了一個端點以檢索 JWT 令牌。我后來升級到 api-platform 2.7.x-dev 并且不再顯示端點。雖然我的最終目標是恢復端點,但我的直接目標是更好地了解 Symfony 是如何配置為它將呼叫服務的。
根據 2.6.6 api-platform 檔案添加端點,添加裝飾器并將其注冊為服務。
api/src/OpenApi/JwtDecorator.php
<?php
declare(strict_types=1);
namespace App\OpenApi;
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\Core\OpenApi\OpenApi;
use ApiPlatform\Core\OpenApi\Model;
final class JwtDecorator implements OpenApiFactoryInterface
{
public function __construct(
private OpenApiFactoryInterface $decorated
) {}
public function __invoke(array $context = []): OpenApi
{
// script goes here.
}
}
api/config/services.yaml
services:
# ...
App\OpenApi\JwtDecorator:
decorates: 'api_platform.openapi.factory'
arguments: ['@.inner']
為確保該單一服務單獨負責添加端點,我有意沒有對api/config/packages/security.yaml、api/config/routes.yaml、進行任何更改,并api/config/packages/api_platform.yaml確保它們不會導致端點在原始 2.6.6 版本上顯示,而事實并非如此。我還查看了使用 composer 安裝時所做的更改jwt-auth,但似乎沒有任何相關性。我還發現我可以將 JwtDecorator 重命名為 SomeOtherName 并排除類名具有特殊含義。
接下來,我在類外debug_print_backtrace()的App\OpenApi\JwtDecorator檔案中放置了一個,發現使用2.6.6或2.7.x-dev時,執行命令并且回溯是相同的。我的假設是,在任何請求時,symfony 將首先include()在php擴展名位于的每個檔案上執行src(除非可能明確指示不在某些組態檔中),檢查快取是否新鮮(如果沒有創建快取檔案),并使用結果填充服務容器。請確認這是否正確。
api/src/OpenApi/JwtDecorator.php 檔案中的回溯(不在建構式中)
#0 include() called at [/srv/api/vendor/symfony/error-handler/DebugClassLoader.php:349] # line 349 for 2.6.6 and 346 for 2.7.x-dev.
#1 Symfony\Component\ErrorHandler\DebugClassLoader->loadClass()
#2 ReflectionClass->__construct() called at [/srv/api/vendor/symfony/config/Resource/ReflectionClassResource.php:107]
#3 Symfony\Component\Config\Resource\ReflectionClassResource->computeHash() called at [/srv/api/vendor/symfony/config/Resource/ReflectionClassResource.php:54]
#4 Symfony\Component\Config\Resource\ReflectionClassResource->isFresh() called at [/srv/api/vendor/symfony/config/Resource/SelfCheckingResourceChecker.php:34]
#5 Symfony\Component\Config\Resource\SelfCheckingResourceChecker->isFresh() called at [/srv/api/vendor/symfony/config/ResourceCheckerConfigCache.php:99]
#6 Symfony\Component\Config\ResourceCheckerConfigCache->isFresh() called at [/srv/api/vendor/symfony/config/ConfigCache.php:60]
#7 Symfony\Component\Config\ConfigCache->isFresh() called at [/srv/api/vendor/symfony/http-kernel/Kernel.php:451]
#8 Symfony\Component\HttpKernel\Kernel->initializeContainer() called at [/srv/api/vendor/symfony/http-kernel/Kernel.php:786]
#9 Symfony\Component\HttpKernel\Kernel->preBoot() called at [/srv/api/vendor/symfony/http-kernel/Kernel.php:187]
#10 Symfony\Component\HttpKernel\Kernel->handle() called at [/srv/api/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:37]
#11 Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run() called at [/srv/api/vendor/autoload_runtime.php:35]
#12 require_once(/srv/api/vendor/autoload_runtime.php) called at [/srv/api/public/index.php:5]
接下來,我做了同樣的事情,但這次是在 JwtDecorator 的建構式中,它只在 2.6.6 而不是 2.7.x-dev 中執行。現在我知道沒有顯示端點,因為沒有呼叫裝飾器。
I then start looking at the classes in ContainerIx5JWFD, and I find the name of the class is always "get" servicesClassName "service". Now I see why I was able to change the name of the service from JwtDecorator to SomeOtherName and it still worked. I also find that the ContainerIx5JWFD\getJwtDecoratorService class only exists for 2.6.6 and not for 2.7.x-dev.
2.6.6 backtrace JwtDecorator::__construct()
#0 App\OpenApi\JwtDecorator->__construct() called at [/srv/api/var/cache/dev/ContainerIx5JWFD/getJwtDecoratorService.php:25]
#1 ContainerIx5JWFD\getJwtDecoratorService::do() called at [/srv/api/var/cache/dev/ContainerIx5JWFD/App_KernelDevDebugContainer.php:644]
#2 ContainerIx5JWFD\App_KernelDevDebugContainer->load() called at [/srv/api/var/cache/dev/ContainerIx5JWFD/getApiPlatform_SwaggerUi_ActionService.php:23]
#3 ContainerIx5JWFD\getApiPlatform_SwaggerUi_ActionService::do() called at [/srv/api/var/cache/dev/ContainerIx5JWFD/App_KernelDevDebugContainer.php:644]
#4 ContainerIx5JWFD\App_KernelDevDebugContainer->load() called at [/srv/api/var/cache/dev/ContainerIx5JWFD/getApiPlatform_Swagger_Action_UiService.php:22]
#5 ContainerIx5JWFD\getApiPlatform_Swagger_Action_UiService::do() called at [/srv/api/var/cache/dev/ContainerIx5JWFD/App_KernelDevDebugContainer.php:644]
#6 ContainerIx5JWFD\App_KernelDevDebugContainer->load() called at [/srv/api/vendor/symfony/dependency-injection/Container.php:237]
#7 Symfony\Component\DependencyInjection\Container->make() called at [/srv/api/vendor/symfony/dependency-injection/Container.php:219]
#8 Symfony\Component\DependencyInjection\Container->get() called at [/srv/api/vendor/symfony/http-kernel/Controller/ContainerControllerResolver.php:53]
#9 Symfony\Component\HttpKernel\Controller\ContainerControllerResolver->instantiateController() called at [/srv/api/vendor/symfony/framework-bundle/Controller/ControllerResolver.php:29]
#10 Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver->instantiateController() called at [/srv/api/vendor/symfony/http-kernel/Controller/ControllerResolver.php:108]
#11 Symfony\Component\HttpKernel\Controller\ControllerResolver->createController() called at [/srv/api/vendor/symfony/http-kernel/Controller/ContainerControllerResolver.php:42]
#12 Symfony\Component\HttpKernel\Controller\ContainerControllerResolver->createController() called at [/srv/api/vendor/symfony/http-kernel/Controller/ControllerResolver.php:86]
#13 Symfony\Component\HttpKernel\Controller\ControllerResolver->getController() called at [/srv/api/vendor/symfony/http-kernel/Controller/TraceableControllerResolver.php:38]
#14 Symfony\Component\HttpKernel\Controller\TraceableControllerResolver->getController() called at [/srv/api/vendor/symfony/http-kernel/HttpKernel.php:139]
#15 Symfony\Component\HttpKernel\HttpKernel->handleRaw() called at [/srv/api/vendor/symfony/http-kernel/HttpKernel.php:78]
#16 Symfony\Component\HttpKernel\HttpKernel->handle() called at [/srv/api/vendor/symfony/http-kernel/Kernel.php:199]
#17 Symfony\Component\HttpKernel\Kernel->handle() called at [/srv/api/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:37]
#18 Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run() called at [/srv/api/vendor/autoload_runtime.php:35]
#19 require_once(/srv/api/vendor/autoload_runtime.php) called at [/srv/api/public/index.php:5]
So, to troubleshoot this, I shouldn't be following this code path but really the code which creates these cached files. I am stuck and begging for help. What triggers Symfony to create these classes so that the service is called? How do I determine what is different between the 2.6.6 and 2.7.x-dev so it is only called on 2.6.6?
uj5u.com熱心網友回復:
最重要的資訊:等待該庫的正式發布,因為它將包含一個升級串列。重命名服務(就像這里發生的那樣)可能會在那里列出。
切入正題:通過在api_platform.openapi.factory源代碼中搜索,我發現該服務已在pull request 中重命名。新的服務名稱是api_platform.openapi.factory.next
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/322537.html
標籤:php symfony dependency-injection service api-platform.com
