| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace LaravelUi5\OData\Service\Cache; |
| 6: | |
| 7: | use LaravelUi5\OData\Edm\Contracts\EdmxInterface; |
| 8: | use LaravelUi5\OData\Service\Contracts\ODataServiceInterface; |
| 9: | use ReflectionClass; |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | final class EdmxLoader |
| 18: | { |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | public static function forService(ODataServiceInterface $service): ?EdmxInterface |
| 25: | { |
| 26: | |
| 27: | |
| 28: | |
| 29: | $cacheFile = self::cacheDir($service) . '/Edmx.php'; |
| 30: | if (!file_exists($cacheFile)) { |
| 31: | return null; |
| 32: | } |
| 33: | |
| 34: | $edmxClass = self::edmxClassName($service); |
| 35: | |
| 36: | if (!class_exists($edmxClass)) { |
| 37: | return null; |
| 38: | } |
| 39: | |
| 40: | $instance = new $edmxClass(); |
| 41: | |
| 42: | return $instance instanceof EdmxInterface ? $instance : null; |
| 43: | } |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | public static function edmxClassName(ODataServiceInterface $service): string |
| 51: | { |
| 52: | $refl = new ReflectionClass($service); |
| 53: | return $refl->getNamespaceName() . '\\Edm\\Edmx'; |
| 54: | } |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | public static function cacheDir(ODataServiceInterface $service): string |
| 62: | { |
| 63: | $refl = new ReflectionClass($service); |
| 64: | return dirname($refl->getFileName()) . '/Edm'; |
| 65: | } |
| 66: | } |
| 67: | |