BagistoApiServiceProvider.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. namespace Webkul\BagistoApi\Providers;
  3. use ApiPlatform\GraphQl\Resolver\Factory\ResolverFactoryInterface;
  4. use ApiPlatform\GraphQl\Resolver\QueryCollectionResolverInterface;
  5. use ApiPlatform\GraphQl\Resolver\QueryItemResolverInterface;
  6. use ApiPlatform\GraphQl\Type\Definition\IterableType;
  7. use ApiPlatform\Laravel\Eloquent\State\PersistProcessor;
  8. use ApiPlatform\Metadata\IdentifiersExtractorInterface;
  9. use ApiPlatform\Metadata\IriConverterInterface;
  10. use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
  11. use ApiPlatform\State\Pagination\Pagination;
  12. use ApiPlatform\State\ProcessorInterface;
  13. use ApiPlatform\State\ProviderInterface;
  14. use Illuminate\Support\ServiceProvider;
  15. use Webkul\BagistoApi\Console\Commands\GenerateStorefrontKey;
  16. use Webkul\BagistoApi\Facades\CartTokenFacade;
  17. use Webkul\BagistoApi\Http\Controllers\AdminGraphQLPlaygroundController;
  18. use Webkul\BagistoApi\Http\Controllers\GraphQLPlaygroundController;
  19. use Webkul\BagistoApi\Http\Middleware\VerifyStorefrontKey;
  20. use Webkul\BagistoApi\Metadata\CustomIdentifiersExtractor;
  21. use Webkul\BagistoApi\OpenApi\SplitOpenApiFactory;
  22. use Webkul\BagistoApi\Repositories\GuestCartTokensRepository;
  23. use Webkul\BagistoApi\Resolver\BaseQueryItemResolver;
  24. use Webkul\BagistoApi\Resolver\CategoryCollectionResolver;
  25. use Webkul\BagistoApi\Resolver\CustomerQueryResolver;
  26. use Webkul\BagistoApi\Resolver\Factory\ProductRelationResolverFactory;
  27. use Webkul\BagistoApi\Resolver\ProductCollectionResolver;
  28. use Webkul\BagistoApi\Resolver\SingleProductBagistoApiResolver;
  29. use Webkul\BagistoApi\Resolver\PageByUrlKeyResolver;
  30. use Webkul\BagistoApi\Routing\CustomIriConverter;
  31. use Webkul\BagistoApi\Serializer\TokenHeaderDenormalizer;
  32. use Webkul\BagistoApi\Services\CartTokenService;
  33. use Webkul\BagistoApi\Services\StorefrontKeyService;
  34. use Webkul\BagistoApi\Services\TokenHeaderService;
  35. use Webkul\BagistoApi\State\AttributeCollectionProvider;
  36. use Webkul\BagistoApi\State\AttributeOptionCollectionProvider;
  37. use Webkul\BagistoApi\State\AttributeOptionQueryProvider;
  38. use Webkul\BagistoApi\State\AttributeValueProcessor;
  39. use Webkul\BagistoApi\State\AuthenticatedCustomerProvider;
  40. use Webkul\BagistoApi\State\BundleOptionProductsProvider;
  41. use Webkul\BagistoApi\State\CartTokenMutationProvider;
  42. use Webkul\BagistoApi\State\CartTokenProcessor;
  43. use Webkul\BagistoApi\State\CategoryTreeProvider;
  44. use Webkul\BagistoApi\State\ChannelProvider;
  45. use Webkul\BagistoApi\State\CheckoutAddressProvider;
  46. use Webkul\BagistoApi\State\CheckoutProcessor;
  47. use Webkul\BagistoApi\State\CompareItemProcessor;
  48. use Webkul\BagistoApi\State\CountryStateCollectionProvider;
  49. use Webkul\BagistoApi\State\CountryStateQueryProvider;
  50. use Webkul\BagistoApi\State\CustomerAddressProvider;
  51. use Webkul\BagistoApi\State\CustomerAddressTokenProcessor;
  52. use Webkul\BagistoApi\State\CustomerProcessor;
  53. use Webkul\BagistoApi\State\CustomerProfileProcessor;
  54. use Webkul\BagistoApi\State\DefaultChannelProvider;
  55. use Webkul\BagistoApi\State\DownloadableLinksProvider;
  56. use Webkul\BagistoApi\State\DownloadableProductProcessor;
  57. use Webkul\BagistoApi\State\DownloadableSamplesProvider;
  58. use Webkul\BagistoApi\State\FilterableAttributesProvider;
  59. use Webkul\BagistoApi\State\ForgotPasswordProcessor;
  60. use Webkul\BagistoApi\State\GetCheckoutAddressCollectionProvider;
  61. use Webkul\BagistoApi\State\GroupedProductsProvider;
  62. use Webkul\BagistoApi\State\LoginProcessor;
  63. use Webkul\BagistoApi\State\LogoutProcessor;
  64. use Webkul\BagistoApi\State\PaymentMethodsProvider;
  65. use Webkul\BagistoApi\State\Processor\NewsletterSubscriptionProcessor;
  66. use Webkul\BagistoApi\State\Processor\ContactUsProcessor;
  67. use Webkul\BagistoApi\State\ProductBagistoApiProvider;
  68. use Webkul\BagistoApi\State\ProductGraphQLProvider;
  69. use Webkul\BagistoApi\State\ProductCustomerGroupPriceProcessor;
  70. use Webkul\BagistoApi\State\ProductCustomerGroupPriceProvider;
  71. use Webkul\BagistoApi\State\ProductProcessor;
  72. use Webkul\BagistoApi\State\ProductRelationProvider;
  73. use Webkul\BagistoApi\State\ProductReviewProcessor;
  74. use Webkul\BagistoApi\State\ProductReviewProvider;
  75. use Webkul\BagistoApi\State\ShippingRatesProvider;
  76. use Webkul\BagistoApi\State\VerifyTokenProcessor;
  77. use Webkul\BagistoApi\State\CompareItemProvider;
  78. use Webkul\BagistoApi\State\CustomerDownloadableProductProvider;
  79. use Webkul\BagistoApi\State\CustomerInvoiceProvider;
  80. use Webkul\BagistoApi\State\CustomerOrderProvider;
  81. use Webkul\BagistoApi\State\CustomerOrderShipmentProvider;
  82. use Webkul\BagistoApi\State\CustomerReviewProvider;
  83. use Webkul\BagistoApi\State\WishlistProcessor;
  84. use Webkul\BagistoApi\State\WishlistProvider;
  85. use Webkul\BagistoApi\State\MoveWishlistToCartProcessor;
  86. use Webkul\BagistoApi\State\DeleteAllWishlistsProcessor;
  87. use Webkul\BagistoApi\State\CancelOrderProcessor;
  88. use Webkul\BagistoApi\State\ReorderProcessor;
  89. use Webkul\BagistoApi\State\DeleteAllCompareItemsProcessor;
  90. use ApiPlatform\GraphQl\Serializer\SerializerContextBuilder as GraphQlSerializerContextBuilder;
  91. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  92. use Webkul\BagistoApi\GraphQl\Serializer\FixedSerializerContextBuilder;
  93. class BagistoApiServiceProvider extends ServiceProvider
  94. {
  95. /**
  96. * Register the service provider bindings.
  97. */
  98. public function register(): void
  99. {
  100. $this->app->singleton(IterableType::class);
  101. $this->app->tag(IterableType::class, 'api_platform.graphql.type');
  102. $this->app->singleton(StorefrontKeyService::class, function ($app) {
  103. return new StorefrontKeyService;
  104. });
  105. $this->app->extend(OpenApiFactoryInterface::class, function ($openApiFactory) {
  106. return new SplitOpenApiFactory($openApiFactory);
  107. });
  108. $this->app->singleton(TokenHeaderDenormalizer::class);
  109. $this->app->singleton('token-header-service', function ($app) {
  110. return new TokenHeaderService;
  111. });
  112. $this->app->alias('token-header-service', 'Webkul\BagistoApi\Services\TokenHeaderService');
  113. $this->app->singleton('cart-token-service', function ($app) {
  114. return new CartTokenService(
  115. $app->make('Webkul\Checkout\Repositories\CartRepository'),
  116. $app->make('Webkul\BagistoApi\Repositories\GuestCartTokensRepository'),
  117. $app->make('Webkul\Customer\Repositories\CustomerRepository')
  118. );
  119. });
  120. $this->app->alias('cart-token-service', CartTokenFacade::class);
  121. $this->app->singleton('Webkul\BagistoApi\Repositories\GuestCartTokensRepository', function ($app) {
  122. return new GuestCartTokensRepository($app);
  123. });
  124. $this->app->tag(ProductProcessor::class, ProcessorInterface::class);
  125. $this->app->tag(AttributeValueProcessor::class, ProcessorInterface::class);
  126. $this->app->tag(ProductCustomerGroupPriceProcessor::class, ProcessorInterface::class);
  127. $this->app->tag(CustomerProcessor::class, ProcessorInterface::class);
  128. $this->app->tag(LoginProcessor::class, ProcessorInterface::class);
  129. $this->app->tag(VerifyTokenProcessor::class, ProcessorInterface::class);
  130. $this->app->tag(LogoutProcessor::class, ProcessorInterface::class);
  131. $this->app->tag(ForgotPasswordProcessor::class, ProcessorInterface::class);
  132. $this->app->tag(CustomerProfileProcessor::class, ProcessorInterface::class);
  133. $this->app->tag(CustomerAddressTokenProcessor::class, ProcessorInterface::class);
  134. $this->app->tag(CartTokenProcessor::class, ProcessorInterface::class);
  135. $this->app->tag(CheckoutProcessor::class, ProcessorInterface::class);
  136. $this->app->tag(ProductReviewProcessor::class, ProcessorInterface::class);
  137. $this->app->tag(CompareItemProcessor::class, ProcessorInterface::class);
  138. $this->app->tag(DownloadableProductProcessor::class, ProcessorInterface::class);
  139. $this->app->tag(NewsletterSubscriptionProcessor::class, ProcessorInterface::class);
  140. $this->app->tag(WishlistProcessor::class, ProcessorInterface::class);
  141. $this->app->tag(MoveWishlistToCartProcessor::class, ProcessorInterface::class);
  142. $this->app->tag(DeleteAllWishlistsProcessor::class, ProcessorInterface::class);
  143. $this->app->tag(DeleteAllCompareItemsProcessor::class, ProcessorInterface::class);
  144. $this->app->tag(CancelOrderProcessor::class, ProcessorInterface::class);
  145. $this->app->tag(ReorderProcessor::class, ProcessorInterface::class);
  146. $this->app->tag(ContactUsProcessor::class, ProcessorInterface::class);
  147. $this->app->tag(TokenHeaderDenormalizer::class, 'serializer.normalizer');
  148. $this->app->singleton(ProductCustomerGroupPriceProcessor::class, function ($app) {
  149. return new ProductCustomerGroupPriceProcessor(
  150. $app->make(PersistProcessor::class)
  151. );
  152. });
  153. $this->app->singleton(CustomerProcessor::class, function ($app) {
  154. return new CustomerProcessor(
  155. $app->make('Webkul\Customer\Repositories\CustomerRepository'),
  156. $app->make('Webkul\BagistoApi\Validators\CustomerValidator')
  157. );
  158. });
  159. $this->app->singleton(LoginProcessor::class, function ($app) {
  160. return new LoginProcessor(
  161. $app->make('Webkul\BagistoApi\Validators\LoginValidator')
  162. );
  163. });
  164. $this->app->singleton(CustomerProfileProcessor::class, function ($app) {
  165. return new CustomerProfileProcessor(
  166. $app->make('Webkul\BagistoApi\Validators\CustomerValidator')
  167. );
  168. });
  169. $this->app->singleton(CartTokenProcessor::class, function ($app) {
  170. return new CartTokenProcessor(
  171. $app->make('Webkul\Checkout\Repositories\CartRepository'),
  172. $app->make('Webkul\BagistoApi\Repositories\GuestCartTokensRepository')
  173. );
  174. });
  175. $this->app->singleton(CheckoutProcessor::class, function ($app) {
  176. return new CheckoutProcessor(
  177. $app->make('Webkul\Customer\Repositories\CustomerRepository'),
  178. $app->make('Webkul\Sales\Repositories\OrderRepository'),
  179. $app->make('Webkul\Checkout\Repositories\CartRepository')
  180. );
  181. });
  182. $this->app->singleton(ProductReviewProcessor::class, function ($app) {
  183. return new ProductReviewProcessor(
  184. $app->make(PersistProcessor::class)
  185. );
  186. });
  187. $this->app->singleton(CompareItemProcessor::class, function ($app) {
  188. return new CompareItemProcessor(
  189. $app->make(PersistProcessor::class)
  190. );
  191. });
  192. $this->app->singleton(WishlistProcessor::class, function ($app) {
  193. return new WishlistProcessor(
  194. $app->make(PersistProcessor::class)
  195. );
  196. });
  197. $this->app->singleton(MoveWishlistToCartProcessor::class, function ($app) {
  198. return new MoveWishlistToCartProcessor(
  199. $app->make(PersistProcessor::class)
  200. );
  201. });
  202. $this->app->singleton(DeleteAllWishlistsProcessor::class, function ($app) {
  203. return new DeleteAllWishlistsProcessor(
  204. $app->make(PersistProcessor::class)
  205. );
  206. });
  207. $this->app->singleton(DeleteAllCompareItemsProcessor::class, function ($app) {
  208. return new DeleteAllCompareItemsProcessor(
  209. $app->make(PersistProcessor::class)
  210. );
  211. });
  212. $this->app->singleton(CancelOrderProcessor::class, function ($app) {
  213. return new CancelOrderProcessor(
  214. $app->make(PersistProcessor::class),
  215. $app->make('Webkul\Sales\Repositories\OrderRepository')
  216. );
  217. });
  218. $this->app->singleton(ReorderProcessor::class, function ($app) {
  219. return new ReorderProcessor(
  220. $app->make(PersistProcessor::class)
  221. );
  222. });
  223. $this->app->singleton(LogoutProcessor::class, function ($app) {
  224. return new LogoutProcessor();
  225. });
  226. $this->app->tag(CheckoutAddressProvider::class, ProviderInterface::class);
  227. $this->app->tag(CustomerAddressProvider::class, ProviderInterface::class);
  228. $this->app->tag(GetCheckoutAddressCollectionProvider::class, ProviderInterface::class);
  229. $this->app->tag(PaymentMethodsProvider::class, ProviderInterface::class);
  230. $this->app->tag(ShippingRatesProvider::class, ProviderInterface::class);
  231. $this->app->tag(AuthenticatedCustomerProvider::class, ProviderInterface::class);
  232. $this->app->tag(CartTokenMutationProvider::class, ProviderInterface::class);
  233. $this->app->tag(ChannelProvider::class, ProviderInterface::class);
  234. $this->app->tag(DefaultChannelProvider::class, ProviderInterface::class);
  235. $this->app->tag(ProductBagistoApiProvider::class, ProviderInterface::class);
  236. $this->app->tag(ProductGraphQLProvider::class, ProviderInterface::class);
  237. $this->app->tag(ProductCustomerGroupPriceProvider::class, ProviderInterface::class);
  238. $this->app->tag(ProductRelationProvider::class, ProviderInterface::class);
  239. $this->app->tag(BundleOptionProductsProvider::class, ProviderInterface::class);
  240. $this->app->tag(GroupedProductsProvider::class, ProviderInterface::class);
  241. $this->app->tag(DownloadableLinksProvider::class, ProviderInterface::class);
  242. $this->app->tag(DownloadableSamplesProvider::class, ProviderInterface::class);
  243. $this->app->tag(ProductReviewProvider::class, ProviderInterface::class);
  244. $this->app->tag(FilterableAttributesProvider::class, ProviderInterface::class);
  245. $this->app->tag(AttributeCollectionProvider::class, ProviderInterface::class);
  246. $this->app->tag(AttributeOptionCollectionProvider::class, ProviderInterface::class);
  247. $this->app->tag(AttributeOptionQueryProvider::class, ProviderInterface::class);
  248. $this->app->tag(CountryStateCollectionProvider::class, ProviderInterface::class);
  249. $this->app->tag(CountryStateQueryProvider::class, ProviderInterface::class);
  250. $this->app->tag(CategoryTreeProvider::class, ProviderInterface::class);
  251. $this->app->tag(WishlistProvider::class, ProviderInterface::class);
  252. $this->app->tag(CompareItemProvider::class, ProviderInterface::class);
  253. $this->app->tag(CustomerReviewProvider::class, ProviderInterface::class);
  254. $this->app->tag(CustomerOrderProvider::class, ProviderInterface::class);
  255. $this->app->tag(CustomerDownloadableProductProvider::class, ProviderInterface::class);
  256. $this->app->tag(CustomerInvoiceProvider::class, ProviderInterface::class);
  257. $this->app->tag(CustomerOrderShipmentProvider::class, ProviderInterface::class);
  258. $this->app->singleton(GetCheckoutAddressCollectionProvider::class, function ($app) {
  259. return new GetCheckoutAddressCollectionProvider(
  260. $app->make('ApiPlatform\State\Pagination\Pagination')
  261. );
  262. });
  263. $this->app->singleton(WishlistProvider::class, function ($app) {
  264. return new WishlistProvider(
  265. $app->make(Pagination::class)
  266. );
  267. });
  268. $this->app->singleton(CompareItemProvider::class, function ($app) {
  269. return new CompareItemProvider(
  270. $app->make(Pagination::class)
  271. );
  272. });
  273. $this->app->singleton(CustomerReviewProvider::class, function ($app) {
  274. return new CustomerReviewProvider(
  275. $app->make(Pagination::class)
  276. );
  277. });
  278. $this->app->singleton(CustomerOrderProvider::class, function ($app) {
  279. return new CustomerOrderProvider(
  280. $app->make(Pagination::class)
  281. );
  282. });
  283. $this->app->singleton(CustomerDownloadableProductProvider::class, function ($app) {
  284. return new CustomerDownloadableProductProvider(
  285. $app->make(Pagination::class)
  286. );
  287. });
  288. $this->app->singleton(CustomerInvoiceProvider::class, function ($app) {
  289. return new CustomerInvoiceProvider(
  290. $app->make(Pagination::class)
  291. );
  292. });
  293. $this->app->singleton(CustomerOrderShipmentProvider::class, function ($app) {
  294. return new CustomerOrderShipmentProvider(
  295. $app->make(Pagination::class)
  296. );
  297. });
  298. $this->app->singleton(CustomerAddressProvider::class, function ($app) {
  299. return new CustomerAddressProvider(
  300. $app->make(Pagination::class)
  301. );
  302. });
  303. $this->app->singleton(ProductBagistoApiProvider::class, function ($app) {
  304. return new ProductBagistoApiProvider(
  305. $app->make(Pagination::class)
  306. );
  307. });
  308. $this->app->singleton(ProductGraphQLProvider::class, function ($app) {
  309. return new ProductGraphQLProvider(
  310. $app->make(Pagination::class)
  311. );
  312. });
  313. $this->app->singleton(ProductRelationProvider::class, function ($app) {
  314. return new ProductRelationProvider(
  315. $app->make(Pagination::class)
  316. );
  317. });
  318. $this->app->singleton(ProductReviewProvider::class, function ($app) {
  319. return new ProductReviewProvider(
  320. $app->make(Pagination::class)
  321. );
  322. });
  323. $this->app->singleton(GroupedProductsProvider::class, function ($app) {
  324. return new GroupedProductsProvider(
  325. $app->make(Pagination::class)
  326. );
  327. });
  328. $this->app->singleton(DownloadableLinksProvider::class, function ($app) {
  329. return new DownloadableLinksProvider(
  330. $app->make(Pagination::class)
  331. );
  332. });
  333. $this->app->singleton(DownloadableSamplesProvider::class, function ($app) {
  334. return new DownloadableSamplesProvider(
  335. $app->make(Pagination::class)
  336. );
  337. });
  338. $this->app->singleton(FilterableAttributesProvider::class, function ($app) {
  339. return new FilterableAttributesProvider(
  340. $app->make(\ApiPlatform\State\Pagination\Pagination::class)
  341. );
  342. });
  343. $this->app->singleton(AttributeCollectionProvider::class, function ($app) {
  344. return new AttributeCollectionProvider(
  345. $app->make(Pagination::class)
  346. );
  347. });
  348. $this->app->singleton(AttributeOptionCollectionProvider::class, function ($app) {
  349. return new AttributeOptionCollectionProvider(
  350. $app->make(Pagination::class)
  351. );
  352. });
  353. $this->app->singleton(CountryStateCollectionProvider::class, function ($app) {
  354. return new CountryStateCollectionProvider(
  355. $app->make(Pagination::class)
  356. );
  357. });
  358. $this->app->singleton(ProductCollectionResolver::class);
  359. $this->app->tag(SingleProductBagistoApiResolver::class, QueryItemResolverInterface::class);
  360. $this->app->tag(CategoryCollectionResolver::class, QueryCollectionResolverInterface::class);
  361. $this->app->tag(BaseQueryItemResolver::class, QueryItemResolverInterface::class);
  362. $this->app->tag(CustomerQueryResolver::class, QueryItemResolverInterface::class);
  363. $this->app->tag(PageByUrlKeyResolver::class, QueryCollectionResolverInterface::class);
  364. $this->app->extend(ResolverFactoryInterface::class, function ($resolverFactory, $app) {
  365. return new ProductRelationResolverFactory(
  366. $resolverFactory,
  367. $app->make(ProductRelationProvider::class)
  368. );
  369. });
  370. $this->app->extend(IdentifiersExtractorInterface::class, function ($extractor) {
  371. return new CustomIdentifiersExtractor($extractor);
  372. });
  373. $this->app->extend(IriConverterInterface::class, function ($converter, $app) {
  374. return new CustomIriConverter(
  375. $converter,
  376. $app->make(\ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface::class)
  377. );
  378. });
  379. $this->app->extend(GraphQlSerializerContextBuilder::class, function ($builder, $app) {
  380. return new FixedSerializerContextBuilder(
  381. $builder,
  382. $app->make(NameConverterInterface::class)
  383. );
  384. });
  385. }
  386. /**
  387. * Bootstrap services.
  388. */
  389. public function boot(): void
  390. {
  391. $this->loadTranslationsFrom(__DIR__.'/../Resources/lang', 'bagistoapi');
  392. $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');
  393. $this->loadViewsFrom(__DIR__.'/../Resources/views', 'webkul');
  394. if ($this->isRunningAsVendorPackage()) {
  395. $this->publishes([
  396. __DIR__.'/../config/api-platform-vendor.php' => config_path('api-platform.php'),
  397. ], 'bagistoapi-config');
  398. } else {
  399. $this->publishes([
  400. __DIR__.'/../config/api-platform.php' => config_path('api-platform.php'),
  401. ], 'bagistoapi-config');
  402. }
  403. $this->publishes([
  404. __DIR__.'/../config/graphql-auth.php' => config_path('graphql-auth.php'),
  405. __DIR__.'/../config/storefront.php' => config_path('storefront.php'),
  406. ], 'bagistoapi-config');
  407. $this->publishes([
  408. __DIR__.'/../resources/views' => resource_path('views/vendor/webkul'),
  409. ], 'bagistoapi-views');
  410. $this->publishes([
  411. __DIR__.'/../Resources/assets' => public_path('themes/admin/default/assets'),
  412. ], 'bagistoapi-assets');
  413. $this->runInstallationIfNeeded();
  414. $this->registerApiResources();
  415. $this->registerApiDocumentationRoutes();
  416. $this->registerMiddlewareAliases();
  417. $this->registerServiceProviders();
  418. if ($this->app->runningInConsole()) {
  419. $this->registerCommands();
  420. }
  421. }
  422. /**
  423. * Register API documentation routes.
  424. */
  425. protected function registerApiDocumentationRoutes(): void
  426. {
  427. \Illuminate\Support\Facades\Route::get('/api', \Webkul\BagistoApi\Http\Controllers\ApiEntrypointController::class)
  428. ->name('bagistoapi.docs-index');
  429. \Illuminate\Support\Facades\Route::get('/api/shop', [
  430. \Webkul\BagistoApi\Http\Controllers\SwaggerUIController::class, 'shopApi',
  431. ])->name('bagistoapi.shop-docs')->where('_format', '^(?!json|xml|csv)');
  432. \Illuminate\Support\Facades\Route::get('/api/admin', [
  433. \Webkul\BagistoApi\Http\Controllers\SwaggerUIController::class, 'adminApi',
  434. ])->name('bagistoapi.admin-docs')->where('_format', '^(?!json|xml|csv)');
  435. \Illuminate\Support\Facades\Route::get('/api/shop/docs', [
  436. \Webkul\BagistoApi\Http\Controllers\SwaggerUIController::class, 'shopApiDocs',
  437. ])->name('bagistoapi.shop-api-spec');
  438. \Illuminate\Support\Facades\Route::get('/api/admin/docs', [
  439. \Webkul\BagistoApi\Http\Controllers\SwaggerUIController::class, 'adminApiDocs',
  440. ])->name('bagistoapi.admin-api-spec');
  441. \Illuminate\Support\Facades\Route::get('/api/graphiql', GraphQLPlaygroundController::class)
  442. ->name('bagistoapi.graphql-playground');
  443. \Illuminate\Support\Facades\Route::get('/api/graphql', GraphQLPlaygroundController::class)
  444. ->name('bagistoapi.api-graphql-playground');
  445. \Illuminate\Support\Facades\Route::get('/admin/graphiql', AdminGraphQLPlaygroundController::class)
  446. ->name('bagistoapi.admin-graphql-playground');
  447. \Illuminate\Support\Facades\Route::get('/api/shop/customer-invoices/{id}/pdf', \Webkul\BagistoApi\Http\Controllers\InvoicePdfController::class)
  448. ->where('id', '[0-9]+')
  449. ->middleware(['Webkul\BagistoApi\Http\Middleware\VerifyStorefrontKey'])
  450. ->name('bagistoapi.customer-invoice-pdf');
  451. }
  452. /**
  453. * Register API resources.
  454. */
  455. protected function registerApiResources(): void
  456. {
  457. if ($this->app->bound('api_platform.metadata_factory')) {
  458. }
  459. }
  460. /**
  461. * Run installation if needed.
  462. */
  463. protected function runInstallationIfNeeded(): void
  464. {
  465. if (file_exists(config_path('api-platform.php'))) {
  466. return;
  467. }
  468. if (! $this->app->runningInConsole() || ! $this->isComposerOperation()) {
  469. return;
  470. }
  471. try {
  472. $this->app['artisan']->call('bagisto-api-platform:install', ['--quiet' => true]);
  473. } catch (\Exception) {
  474. // Installation can be run manually if needed
  475. }
  476. }
  477. /**
  478. * Determine if running via Composer.
  479. */
  480. protected function isComposerOperation(): bool
  481. {
  482. $composerMemory = getenv('COMPOSER_MEMORY_LIMIT');
  483. $composerAuth = getenv('COMPOSER_AUTH');
  484. return ! empty($composerMemory) || ! empty($composerAuth) || defined('COMPOSER_BINARY_PATH');
  485. }
  486. /**
  487. * Register middleware aliases.
  488. */
  489. protected function registerMiddlewareAliases(): void
  490. {
  491. $this->app['router']->aliasMiddleware('storefront.key', VerifyStorefrontKey::class);
  492. $this->app['router']->aliasMiddleware('api.locale-channel', \Webkul\BagistoApi\Http\Middleware\SetLocaleChannel::class);
  493. $this->app['router']->aliasMiddleware('api.rate-limit', \Webkul\BagistoApi\Http\Middleware\RateLimitApi::class);
  494. $this->app['router']->aliasMiddleware('api.security-headers', \Webkul\BagistoApi\Http\Middleware\SecurityHeaders::class);
  495. $this->app['router']->aliasMiddleware('api.log-requests', \Webkul\BagistoApi\Http\Middleware\LogApiRequests::class);
  496. }
  497. /**
  498. * Register service providers.
  499. */
  500. protected function registerServiceProviders(): void
  501. {
  502. $this->app->register(ApiPlatformExceptionHandlerServiceProvider::class);
  503. $this->app->register(DatabaseQueryLoggingProvider::class);
  504. $this->app->register(ExceptionHandlerServiceProvider::class);
  505. }
  506. /**
  507. * Register console commands.
  508. */
  509. protected function registerCommands(): void
  510. {
  511. $this->commands([
  512. \Webkul\BagistoApi\Console\Commands\InstallApiPlatformCommand::class,
  513. \Webkul\BagistoApi\Console\Commands\ClearApiPlatformCacheCommand::class,
  514. GenerateStorefrontKey::class,
  515. \Webkul\BagistoApi\Console\Commands\ApiKeyManagementCommand::class,
  516. \Webkul\BagistoApi\Console\Commands\ApiKeyMaintenanceCommand::class,
  517. ]);
  518. }
  519. /**
  520. * Check if the package is running as a vendor package.
  521. */
  522. protected function isRunningAsVendorPackage(): bool
  523. {
  524. return str_contains(__DIR__, 'vendor');
  525. }
  526. }