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 MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ORM\Tools\Console\Command; |
21
|
|
|
|
22
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
25
|
|
|
use Symfony\Component\Console\Command\Command; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Show information about mapped entities. |
29
|
|
|
* |
30
|
|
|
* @link www.doctrine-project.org |
31
|
|
|
* @since 2.1 |
32
|
|
|
* @author Benjamin Eberlei <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class InfoCommand extends Command |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
1 |
|
protected function configure() |
40
|
|
|
{ |
41
|
|
|
$this |
42
|
1 |
|
->setName('orm:info') |
43
|
1 |
|
->setDescription('Show basic information about all mapped entities') |
44
|
1 |
|
->setHelp(<<<EOT |
45
|
1 |
|
The <info>%command.name%</info> shows basic information about which |
46
|
|
|
entities exist and possibly if their mapping information contains errors or |
47
|
|
|
not. |
48
|
|
|
EOT |
49
|
|
|
); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
56
|
|
|
{ |
57
|
|
|
/* @var $entityManager \Doctrine\ORM\EntityManager */ |
58
|
1 |
|
$entityManager = $this->getHelper('em')->getEntityManager(); |
|
|
|
|
59
|
|
|
|
60
|
1 |
|
$entityClassNames = $entityManager->getConfiguration() |
61
|
1 |
|
->getMetadataDriverImpl() |
62
|
1 |
|
->getAllClassNames(); |
63
|
|
|
|
64
|
1 |
|
if (!$entityClassNames) { |
65
|
|
|
throw new \Exception( |
66
|
|
|
'You do not have any mapped Doctrine ORM entities according to the current configuration. '. |
67
|
|
|
'If you have entities or mapping files you should check your mapping configuration for errors.' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$output->writeln(sprintf("Found <info>%d</info> mapped entities:", count($entityClassNames))); |
72
|
|
|
|
73
|
1 |
|
$failure = false; |
74
|
|
|
|
75
|
1 |
|
foreach ($entityClassNames as $entityClassName) { |
76
|
|
|
try { |
77
|
1 |
|
$entityManager->getClassMetadata($entityClassName); |
78
|
1 |
|
$output->writeln(sprintf("<info>[OK]</info> %s", $entityClassName)); |
79
|
|
|
} catch (MappingException $e) { |
80
|
|
|
$output->writeln("<error>[FAIL]</error> ".$entityClassName); |
81
|
|
|
$output->writeln(sprintf("<comment>%s</comment>", $e->getMessage())); |
82
|
|
|
$output->writeln(''); |
83
|
|
|
|
84
|
1 |
|
$failure = true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return $failure ? 1 : 0; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: