|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LaravelDoctrine\ORM\Console\ConfigMigrations; |
| 4 | + |
| 5 | +use Illuminate\Contracts\View\Factory; |
| 6 | +use LaravelDoctrine\ORM\Utilities\ArrayUtil; |
| 7 | + |
| 8 | +class AtrauzziMigrator implements ConfigurationMigrator |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var Factory |
| 12 | + */ |
| 13 | + protected $viewFactory; |
| 14 | + |
| 15 | + /** |
| 16 | + * @param Factory $viewFactory |
| 17 | + */ |
| 18 | + public function __construct(Factory $viewFactory) |
| 19 | + { |
| 20 | + $this->viewFactory = $viewFactory; |
| 21 | + $this->viewFactory->addNamespace('atrauzzi', realpath(__DIR__ . '/templates/atrauzzi')); |
| 22 | + $this->viewFactory->addNamespace('laraveldoctrine', realpath(__DIR__ . '/templates/laraveldoctrine')); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Convert a configuration array from another laravel-doctrine project in to a string representation of a php array configuration for this project |
| 27 | + * |
| 28 | + * @param array $sourceArray |
| 29 | + * |
| 30 | + * @return string |
| 31 | + */ |
| 32 | + public function convertConfiguration($sourceArray) |
| 33 | + { |
| 34 | + $dqls = $this->convertDQL($sourceArray); |
| 35 | + $cache = $this->convertCache($sourceArray); |
| 36 | + $customTypes = $this->convertCustomTypes($sourceArray); |
| 37 | + $managers = [$this->convertManager($sourceArray)]; |
| 38 | + |
| 39 | + $results = $this->viewFactory->make('laraveldoctrine.master', [ |
| 40 | + 'managers' => $managers, |
| 41 | + 'cache' => $cache, |
| 42 | + 'dqls' => $dqls, |
| 43 | + 'customTypes' => $customTypes |
| 44 | + ])->render(); |
| 45 | + |
| 46 | + return $this->unescape($results); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param $sourceArray |
| 51 | + * |
| 52 | + * @return string |
| 53 | + */ |
| 54 | + public function convertManager($sourceArray) |
| 55 | + { |
| 56 | + $proxySettings = ArrayUtil::get($sourceArray['proxy_classes']); |
| 57 | + $defaultRepo = ArrayUtil::get($sourceArray['default_repository']); |
| 58 | + $namespaces = []; |
| 59 | + $connection = ArrayUtil::get($sourceArray['default']); |
| 60 | + |
| 61 | + // Non default configuration |
| 62 | + if (count($sourceArray['metadata']) > 1) { |
| 63 | + $hasNamespaces = false; |
| 64 | + $driver = null; |
| 65 | + $sameDriver = true; |
| 66 | + |
| 67 | + foreach ($sourceArray['metadata'] as $key => $item) { |
| 68 | + if (is_null($driver)) { |
| 69 | + if (is_array($item)) { |
| 70 | + $driver = $item['driver']; |
| 71 | + } elseif ($key == 'driver') { |
| 72 | + $driver = $item; |
| 73 | + } |
| 74 | + } else { |
| 75 | + if (is_array($item) && $item['driver'] != $driver) { |
| 76 | + $sameDriver = false; |
| 77 | + } |
| 78 | + } |
| 79 | + if (is_array($item) && isset($item['namespace'])) { |
| 80 | + $hasNamespaces = true; |
| 81 | + } |
| 82 | + } |
| 83 | + // Only do this if all the same driver |
| 84 | + if ($hasNamespaces && $sameDriver) { |
| 85 | + $driver = $sourceArray['metadata'][0]['driver']; |
| 86 | + |
| 87 | + // Convert each metadata entry into a namespace entry |
| 88 | + foreach ($sourceArray['metadata'] as $item) { |
| 89 | + if (isset($item['alias'])) { |
| 90 | + $namespaces[$item['alias']] = $item['namespace']; |
| 91 | + } else { |
| 92 | + array_push($namespaces, $item['namespace']); |
| 93 | + } |
| 94 | + } |
| 95 | + // Only specifying one non-default EM |
| 96 | + } else { |
| 97 | + if (isset($sourceArray['metadata']['namespace'])) { |
| 98 | + if (isset($sourceArray['metadata']['alias'])) { |
| 99 | + $namespaces[$sourceArray['metadata']['alias']] = $sourceArray['metadata']['namespace']; |
| 100 | + } else { |
| 101 | + $namespaces[] = $sourceArray['metadata']['namespace']; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + // One EM, default |
| 106 | + } else { |
| 107 | + $driver = $sourceArray['metadata']['driver']; |
| 108 | + } |
| 109 | + |
| 110 | + $results = $this->viewFactory->make('atrauzzi.manager', [ |
| 111 | + 'namespaces' => $namespaces, |
| 112 | + 'proxySettings' => $proxySettings, |
| 113 | + 'defaultRepo' => $defaultRepo, |
| 114 | + 'driver' => $driver, |
| 115 | + 'connection' => $connection |
| 116 | + ])->render(); |
| 117 | + |
| 118 | + return $this->unescape($results); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param $sourceArray |
| 123 | + * |
| 124 | + * @return string |
| 125 | + */ |
| 126 | + public function convertCustomTypes($sourceArray) |
| 127 | + { |
| 128 | + $results = $this->viewFactory->make('atrauzzi.customTypes', [ |
| 129 | + 'sourceArray' => $sourceArray |
| 130 | + ])->render(); |
| 131 | + |
| 132 | + return $this->unescape($results); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Convert a cache section from mitchellvanw/laravel-doctrine to a string representation of a php array configuration for a cache section for this project |
| 137 | + * |
| 138 | + * @param array $sourceArray |
| 139 | + * |
| 140 | + * @return string |
| 141 | + */ |
| 142 | + public function convertCache($sourceArray) |
| 143 | + { |
| 144 | + if (isset($sourceArray['cache']['provider'])) { |
| 145 | + $cacheProvider = ArrayUtil::get($sourceArray['cache']['provider']); |
| 146 | + $results = $this->viewFactory->make('atrauzzi.cache', [ |
| 147 | + 'cacheProvider' => $cacheProvider, |
| 148 | + 'extras' => count($sourceArray['cache']) > 1 |
| 149 | + //if user is mimicking cache arrays here we need to tell them to move these to cache.php |
| 150 | + ])->render(); |
| 151 | + |
| 152 | + return $this->unescape($results); |
| 153 | + } |
| 154 | + |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Convert the dql sections from the entity managers in a configuration from atruazzi/laravel-doctrine into a string representation of a php array configuration for custom string/numeric/datetime functions |
| 160 | + * Returns null if no dql sections were found. |
| 161 | + * |
| 162 | + * @param $sourceArray |
| 163 | + * |
| 164 | + * @return null|string |
| 165 | + */ |
| 166 | + public function convertDQL($sourceArray) |
| 167 | + { |
| 168 | + $results = $this->viewFactory->make('atrauzzi.dql', ['dql' => $sourceArray])->render(); |
| 169 | + |
| 170 | + return $this->unescape($results); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @param $results |
| 175 | + * |
| 176 | + * @return string |
| 177 | + */ |
| 178 | + protected function unescape($results) |
| 179 | + { |
| 180 | + return html_entity_decode($results, ENT_QUOTES); |
| 181 | + } |
| 182 | +} |
0 commit comments