ObjectManager.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. use Magento\Framework\ObjectManager\FactoryInterface;
  8. /**
  9. * Direct usage of this class is strictly discouraged.
  10. *
  11. * Wrapper around object manager with workarounds to access it in client code.
  12. * Provides static access to objectManager, that is required for unserialization of objects.
  13. *
  14. * @api
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. * @since 100.0.2
  17. */
  18. class ObjectManager extends \Magento\Framework\ObjectManager\ObjectManager
  19. {
  20. /**
  21. * @var ObjectManager
  22. */
  23. protected static $_instance;
  24. /**
  25. * Retrieve object manager
  26. *
  27. * @return ObjectManager
  28. * @throws \RuntimeException
  29. */
  30. public static function getInstance()
  31. {
  32. if (!self::$_instance instanceof \Magento\Framework\ObjectManagerInterface) {
  33. throw new \RuntimeException('ObjectManager isn\'t initialized');
  34. }
  35. return self::$_instance;
  36. }
  37. /**
  38. * Set object manager instance
  39. *
  40. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  41. * @throws \LogicException
  42. * @return void
  43. */
  44. public static function setInstance(\Magento\Framework\ObjectManagerInterface $objectManager)
  45. {
  46. self::$_instance = $objectManager;
  47. }
  48. /**
  49. * @param FactoryInterface $factory
  50. * @param \Magento\Framework\ObjectManager\ConfigInterface $config
  51. * @param array $sharedInstances
  52. */
  53. public function __construct(
  54. FactoryInterface $factory,
  55. \Magento\Framework\ObjectManager\ConfigInterface $config,
  56. array &$sharedInstances = []
  57. ) {
  58. parent::__construct($factory, $config, $sharedInstances);
  59. self::$_instance = $this;
  60. }
  61. }