These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | |||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
42 | ClassMetadataFactoryInterface $classMetadataFactory = null, |
||
0 ignored issues
–
show
|
|||
43 | MetadataGrapherInterface $metadataGrapher = null |
||
44 | ) { |
||
45 | 4 | $this->entityManager = $entityManager; |
|
46 | 4 | $this->metadataFactory = $classMetadataFactory ? $classMetadataFactory : new ClassMetadataFactory(); |
|
47 | 4 | $this->metadataFactory->setEntityManager($this->entityManager); |
|
48 | 4 | $this->metadataGrapher = $metadataGrapher ? $metadataGrapher : new MetadataGrapher(); |
|
49 | 4 | } |
|
50 | |||
51 | /** |
||
52 | * Get doctrine metadata as yuml. |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | 1 | public function makeDslText() |
|
57 | { |
||
58 | 1 | return $this->generateGraph($this->getClasses()); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Use yuml.me to generate an image from yuml. |
||
63 | * |
||
64 | * @param string $dsl_text |
||
65 | * |
||
66 | * @return string The url of the generated image. |
||
67 | */ |
||
68 | 1 | public function getGraphUrl($dsl_text) |
|
69 | { |
||
70 | 1 | $curl = new Curl(self::YUML_POST_URL); |
|
71 | 1 | $curl->setPosts(array('dsl_text' => $dsl_text)); |
|
72 | 1 | $return = $curl->getResponse(); |
|
73 | |||
74 | 1 | return self::YUML_REDIRECT_URL . $return; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param string $graphUrl |
||
79 | * @param string $filename |
||
80 | * @return mixed |
||
81 | */ |
||
82 | 1 | public function downloadImage($graphUrl, $filename, CurlInterface $curl = null) |
|
83 | { |
||
84 | 1 | $curl = $curl ? $curl : new Curl($graphUrl); |
|
85 | 1 | $curl->setOutput($filename); |
|
86 | |||
87 | 1 | return $curl->getResponse(); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return array |
||
92 | */ |
||
93 | 1 | private function getMetadata() |
|
94 | { |
||
95 | 1 | return $this->metadataFactory->getAllMetadata(); |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return array |
||
100 | */ |
||
101 | 1 | private function getClasses() |
|
102 | { |
||
103 | 1 | $classes = array(); |
|
104 | /** @var ClassMetadata $class */ |
||
105 | 1 | foreach ($this->getMetadata() as $class) { |
|
106 | 1 | $classes[$class->getName()] = $class; |
|
107 | } |
||
108 | 1 | ksort($classes); |
|
109 | |||
110 | 1 | return $classes; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param $classes |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 1 | private function generateGraph($classes) |
|
119 | { |
||
120 | 1 | $graph = $this->metadataGrapher->generateFromMetadata($classes, false); |
|
121 | |||
122 | 1 | return $graph; |
|
123 | } |
||
124 | } |
||
125 |