1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onurb\Bundle\YumlBundle\Yuml; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
8
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory as ClassMetadataFactoryInterface; |
9
|
|
|
use Onurb\Bundle\YumlBundle\Curl\Curl; |
10
|
|
|
use Onurb\Bundle\YumlBundle\Curl\CurlInterface; |
11
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YUMLMetadataGrapher as MetadataGrapher; |
12
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YUMLMetadataGrapherInterface as MetadataGrapherInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Utility to generate Yuml compatible strings from metadata graphs |
16
|
|
|
* Adaptation of DoctrineORMModule\Yuml\YumlController for ZendFramework-Zend-Developer-Tools |
17
|
|
|
* |
18
|
|
|
* @license MIT |
19
|
|
|
* @link http://www.doctrine-project.org/ |
20
|
|
|
* @author Marco Pivetta <[email protected]> |
21
|
|
|
* @author Bruno Heron <[email protected]> |
22
|
|
|
**/ |
23
|
|
|
class YumlClient implements YumlClientInterface |
24
|
|
|
{ |
25
|
|
|
const YUML_POST_URL = 'https://yuml.me/diagram/plain/class'; |
26
|
|
|
const YUML_REDIRECT_URL = 'https://yuml.me/'; |
27
|
|
|
|
28
|
|
|
protected $entityManager; |
29
|
|
|
|
30
|
|
|
protected $metadataFactory; |
31
|
|
|
|
32
|
|
|
protected $metadataGrapher; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param EntityManagerInterface $entityManager |
36
|
|
|
* @param ClassMetadataFactoryInterface|null $classMetadataFactory |
37
|
|
|
* @param MetadataGrapherInterface|null $metadataGrapher |
38
|
|
|
*/ |
39
|
4 |
|
public function __construct( |
40
|
|
|
EntityManagerInterface $entityManager, |
41
|
|
|
ClassMetadataFactoryInterface $classMetadataFactory = null, |
42
|
|
|
MetadataGrapherInterface $metadataGrapher = null |
43
|
|
|
) { |
44
|
4 |
|
$this->entityManager = $entityManager; |
45
|
4 |
|
$this->metadataFactory = $classMetadataFactory ? $classMetadataFactory : new ClassMetadataFactory(); |
46
|
4 |
|
$this->metadataFactory->setEntityManager($this->entityManager); |
|
|
|
|
47
|
4 |
|
$this->metadataGrapher = $metadataGrapher ? $metadataGrapher : new MetadataGrapher(); |
48
|
4 |
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get doctrine metadata as yuml. |
53
|
|
|
* |
54
|
|
|
* @param bool $showDetail |
55
|
|
|
* @param array $colors |
56
|
|
|
* @param array $notes |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
1 |
|
public function makeDslText($showDetail = false, $colors = array(), $notes = array()) |
60
|
|
|
{ |
61
|
1 |
|
return $this->metadataGrapher->generateFromMetadata( |
62
|
1 |
|
$this->getClasses(), |
63
|
|
|
$showDetail, |
64
|
|
|
$colors, |
65
|
|
|
$notes |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Use yuml.me to generate an image from yuml. |
71
|
|
|
* |
72
|
|
|
* @param string $dsl_text |
73
|
|
|
* |
74
|
|
|
* @return string The url of the generated image. |
75
|
|
|
*/ |
76
|
1 |
|
public function getGraphUrl($dsl_text) |
77
|
|
|
{ |
78
|
1 |
|
$curl = new Curl(self::YUML_POST_URL); |
79
|
1 |
|
$curl->setPosts(array('dsl_text' => $dsl_text)); |
80
|
1 |
|
$return = $curl->getResponse(); |
81
|
|
|
|
82
|
1 |
|
return self::YUML_REDIRECT_URL . $return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $graphUrl |
87
|
|
|
* @param string $filename |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
1 |
|
public function downloadImage($graphUrl, $filename, CurlInterface $curl = null) |
91
|
|
|
{ |
92
|
1 |
|
$curl = $curl ? $curl : new Curl($graphUrl); |
93
|
1 |
|
$curl->setOutput($filename); |
94
|
|
|
|
95
|
1 |
|
return $curl->getResponse(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
1 |
|
private function getMetadata() |
102
|
|
|
{ |
103
|
1 |
|
return $this->metadataFactory->getAllMetadata(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
1 |
|
private function getClasses() |
110
|
|
|
{ |
111
|
1 |
|
$classes = array(); |
112
|
|
|
/** @var ClassMetadata $class */ |
113
|
1 |
|
foreach ($this->getMetadata() as $class) { |
114
|
1 |
|
$classes[$class->getName()] = $class; |
115
|
|
|
} |
116
|
1 |
|
ksort($classes); |
117
|
|
|
|
118
|
1 |
|
return $classes; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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: