factory.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. .. _customize.factory:
  2. ===========================
  3. Replace the Default Factory
  4. ===========================
  5. In many of the examples throughout this documentation, we've seen how to
  6. configure the factory and then use that factory to generate and work with UUIDs.
  7. For example:
  8. .. code-block:: php
  9. :caption: Configure the factory and use it to generate a version 1 UUID
  10. :name: customize.factory.example
  11. use Ramsey\Uuid\Codec\OrderedTimeCodec;
  12. use Ramsey\Uuid\UuidFactory;
  13. $factory = new UuidFactory();
  14. $codec = new OrderedTimeCodec($factory->getUuidBuilder());
  15. $factory->setCodec($codec);
  16. $orderedTimeUuid = $factory->uuid1();
  17. When doing this, the default behavior of ramsey/uuid is left intact. If we call
  18. ``Uuid::uuid1()`` to generate a version 1 UUID after configuring the factory as
  19. shown above, it won't use :ref:`OrderedTimeCodec <customize.ordered-time-codec>`
  20. to generate the UUID.
  21. .. code-block:: php
  22. :caption: The behavior differs between $factory->uuid1() and Uuid::uuid1()
  23. :name: customize.factory.behavior-example
  24. $orderedTimeUuid = $factory->uuid1();
  25. printf(
  26. "UUID: %s\nBytes: %s\n\n",
  27. $orderedTimeUuid->toString(),
  28. bin2hex($orderedTimeUuid->getBytes())
  29. );
  30. $uuid = Uuid::uuid1();
  31. printf(
  32. "UUID: %s\nBytes: %s\n\n",
  33. $uuid->toString(),
  34. bin2hex($uuid->getBytes())
  35. );
  36. In this example, we print out details for two different UUIDs. The first was
  37. generated with the :ref:`OrderedTimeCodec <customize.ordered-time-codec>` using
  38. ``$factory->uuid1()``. The second was generated using ``Uuid::uuid1()``. It
  39. looks something like this:
  40. .. code-block:: text
  41. UUID: 2ff06620-6251-11ea-9791-0242ac130003
  42. Bytes: 11ea62512ff0662097910242ac130003
  43. UUID: 2ff09730-6251-11ea-ba64-0242ac130003
  44. Bytes: 2ff09730625111eaba640242ac130003
  45. Notice the arrangement of the bytes. The first set of bytes has been rearranged,
  46. according to the ordered-time codec rules, but the second set of bytes remains
  47. in the same order as the UUID string.
  48. *Configuring the factory does not change the default behavior.*
  49. If we want to change the default behavior, we must *replace* the factory used
  50. by the Uuid static methods, and we can do this using the
  51. :php:meth:`Uuid::setFactory() <Ramsey\\Uuid\\Uuid::setFactory>` static method.
  52. .. code-block:: php
  53. :caption: Replace the factory to globally affect Uuid behavior
  54. :name: customize.factory.replace-factory-example
  55. Uuid::setFactory($factory);
  56. $uuid = Uuid::uuid1();
  57. Now, every time we call :php:meth:`Uuid::uuid() <Ramsey\\Uuid\\Uuid::uuid1>`,
  58. ramsey/uuid will use the factory configured with the :ref:`OrderedTimeCodec
  59. <customize.ordered-time-codec>` to generate version 1 UUIDs.
  60. .. warning::
  61. Calling :php:meth:`Uuid::setFactory() <Ramsey\\Uuid\\Uuid::setFactory>` to
  62. replace the factory will change the behavior of Uuid no matter where it is
  63. used, so keep this in mind when replacing the factory. If you replace the
  64. factory deep inside a method somewhere, any later code that calls a static
  65. method on :php:class:`Ramsey\\Uuid\\Uuid` will use the new factory to
  66. generate UUIDs.