Exceptions
2 years ago
Formatters
2 years ago
Payments
2 years ago
Routes
2 years ago
Schemas
2 years ago
Utilities
2 years ago
Authentication.php
2 years ago
Formatters.php
2 years ago
Legacy.php
2 years ago
RoutesController.php
2 years ago
SchemaController.php
2 years ago
SessionHandler.php
2 years ago
StoreApi.php
2 years ago
deprecated.php
2 years ago
functions.php
2 years ago
StoreApi.php
110 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Registry\Container; |
| 5 | use Automattic\WooCommerce\StoreApi\Formatters; |
| 6 | use Automattic\WooCommerce\StoreApi\Authentication; |
| 7 | use Automattic\WooCommerce\StoreApi\Legacy; |
| 8 | use Automattic\WooCommerce\StoreApi\Formatters\CurrencyFormatter; |
| 9 | use Automattic\WooCommerce\StoreApi\Formatters\HtmlFormatter; |
| 10 | use Automattic\WooCommerce\StoreApi\Formatters\MoneyFormatter; |
| 11 | use Automattic\WooCommerce\StoreApi\RoutesController; |
| 12 | use Automattic\WooCommerce\StoreApi\SchemaController; |
| 13 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 14 | |
| 15 | /** |
| 16 | * StoreApi Main Class. |
| 17 | */ |
| 18 | final class StoreApi { |
| 19 | /** |
| 20 | * Init and hook in Store API functionality. |
| 21 | */ |
| 22 | public function init() { |
| 23 | add_action( |
| 24 | 'rest_api_init', |
| 25 | function() { |
| 26 | self::container()->get( Legacy::class )->init(); |
| 27 | self::container()->get( RoutesController::class )->register_all_routes(); |
| 28 | } |
| 29 | ); |
| 30 | // Runs on priority 11 after rest_api_default_filters() which is hooked at 10. |
| 31 | add_action( |
| 32 | 'rest_api_init', |
| 33 | function() { |
| 34 | self::container()->get( Authentication::class )->init(); |
| 35 | }, |
| 36 | 11 |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Loads the DI container for Store API. |
| 42 | * |
| 43 | * @internal This uses the Blocks DI container. If Store API were to move to core, this container could be replaced |
| 44 | * with a different compatible container. |
| 45 | * |
| 46 | * @param boolean $reset Used to reset the container to a fresh instance. Note: this means all dependencies will be reconstructed. |
| 47 | * @return mixed |
| 48 | */ |
| 49 | public static function container( $reset = false ) { |
| 50 | static $container; |
| 51 | |
| 52 | if ( $reset ) { |
| 53 | $container = null; |
| 54 | } |
| 55 | |
| 56 | if ( $container ) { |
| 57 | return $container; |
| 58 | } |
| 59 | |
| 60 | $container = new Container(); |
| 61 | $container->register( |
| 62 | Authentication::class, |
| 63 | function () { |
| 64 | return new Authentication(); |
| 65 | } |
| 66 | ); |
| 67 | $container->register( |
| 68 | Legacy::class, |
| 69 | function () { |
| 70 | return new Legacy(); |
| 71 | } |
| 72 | ); |
| 73 | $container->register( |
| 74 | RoutesController::class, |
| 75 | function ( $container ) { |
| 76 | return new RoutesController( |
| 77 | $container->get( SchemaController::class ) |
| 78 | ); |
| 79 | } |
| 80 | ); |
| 81 | $container->register( |
| 82 | SchemaController::class, |
| 83 | function ( $container ) { |
| 84 | return new SchemaController( |
| 85 | $container->get( ExtendSchema::class ) |
| 86 | ); |
| 87 | } |
| 88 | ); |
| 89 | $container->register( |
| 90 | ExtendSchema::class, |
| 91 | function ( $container ) { |
| 92 | return new ExtendSchema( |
| 93 | $container->get( Formatters::class ) |
| 94 | ); |
| 95 | } |
| 96 | ); |
| 97 | $container->register( |
| 98 | Formatters::class, |
| 99 | function () { |
| 100 | $formatters = new Formatters(); |
| 101 | $formatters->register( 'money', MoneyFormatter::class ); |
| 102 | $formatters->register( 'html', HtmlFormatter::class ); |
| 103 | $formatters->register( 'currency', CurrencyFormatter::class ); |
| 104 | return $formatters; |
| 105 | } |
| 106 | ); |
| 107 | return $container; |
| 108 | } |
| 109 | } |
| 110 |