ValidateSchemaCommand.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\ORM\Tools\Console\Command;
  22. use Symfony\Component\Console\Input\InputArgument,
  23. Symfony\Component\Console\Input\InputOption,
  24. Symfony\Component\Console;
  25. /**
  26. * Validate that the current mapping is valid
  27. *
  28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  29. * @link www.doctrine-project.com
  30. * @since 1.0
  31. * @version $Revision$
  32. * @author Benjamin Eberlei <kontakt@beberlei.de>
  33. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  34. * @author Jonathan Wage <jonwage@gmail.com>
  35. * @author Roman Borschel <roman@code-factory.org>
  36. */
  37. class ValidateSchemaCommand extends Console\Command\Command
  38. {
  39. /**
  40. * @see Console\Command\Command
  41. */
  42. protected function configure()
  43. {
  44. $this
  45. ->setName('orm:validate-schema')
  46. ->setDescription('Validate the mapping files.')
  47. ->setHelp(<<<EOT
  48. 'Validate that the mapping files are correct and in sync with the database.'
  49. EOT
  50. );
  51. }
  52. /**
  53. * @see Console\Command\Command
  54. */
  55. protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
  56. {
  57. $em = $this->getHelper('em')->getEntityManager();
  58. $validator = new \Doctrine\ORM\Tools\SchemaValidator($em);
  59. $errors = $validator->validateMapping();
  60. $exit = 0;
  61. if ($errors) {
  62. foreach ($errors AS $className => $errorMessages) {
  63. $output->write("<error>[Mapping] FAIL - The entity-class '" . $className . "' mapping is invalid:</error>\n");
  64. foreach ($errorMessages AS $errorMessage) {
  65. $output->write('* ' . $errorMessage . "\n");
  66. }
  67. $output->write("\n");
  68. }
  69. $exit += 1;
  70. } else {
  71. $output->write('<info>[Mapping] OK - The mapping files are correct.</info>' . "\n");
  72. }
  73. if (!$validator->schemaInSyncWithMetadata()) {
  74. $output->write('<error>[Database] FAIL - The database schema is not in sync with the current mapping file.</error>' . "\n");
  75. $exit += 2;
  76. } else {
  77. $output->write('<info>[Database] OK - The database schema is in sync with the mapping files.</info>' . "\n");
  78. }
  79. exit($exit);
  80. }
  81. }