OrmSchemaProvider.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Migrations\Provider;
  20. use Doctrine\ORM\EntityManager;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use Doctrine\ORM\Tools\SchemaTool;
  23. /**
  24. * A schema provider that uses the doctrine ORM to generate schemas.
  25. *
  26. * @since 1.0.0-alpha3
  27. */
  28. final class OrmSchemaProvider implements SchemaProviderInterface
  29. {
  30. /**
  31. * @var EntityManagerInterface
  32. */
  33. private $entityManager;
  34. public function __construct($em)
  35. {
  36. if (!$this->isEntityManager($em)) {
  37. throw new \InvalidArgumentException(sprintf(
  38. '$em is not a valid Doctrine ORM Entity Manager, got "%s"',
  39. is_object($em) ? get_class($em) : gettype($em)
  40. ));
  41. }
  42. $this->entityManager = $em;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function createSchema()
  48. {
  49. $metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
  50. if (empty($metadata)) {
  51. throw new \UnexpectedValueException('No mapping information to process');
  52. }
  53. $tool = new SchemaTool($this->entityManager);
  54. return $tool->getSchemaFromMetadata($metadata);
  55. }
  56. /**
  57. * Doctrine's EntityManagerInterface was introduced in version 2.4, since this
  58. * library allows those older version we need to be able to check for those
  59. * old ORM versions. Hence the helper method.
  60. *
  61. * No need to check to see if EntityManagerInterface exists first here, PHP
  62. * doesn't care.
  63. *
  64. * @param mixed $manager Hopefully an entity manager, but it may be anything
  65. * @return boolean
  66. */
  67. private function isEntityManager($manager)
  68. {
  69. return $manager instanceof EntityManagerInterface || $manager instanceof EntityManager;
  70. }
  71. }