@@ -56,7 +56,7 @@ |
||
| 56 | 56 | /** |
| 57 | 57 | * Get the services provided by the provider. |
| 58 | 58 | * |
| 59 | - * @return array |
|
| 59 | + * @return string[] |
|
| 60 | 60 | */ |
| 61 | 61 | public function provides() |
| 62 | 62 | { |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | { |
| 37 | 37 | // Experimenting autoloading proxies |
| 38 | 38 | |
| 39 | - $this->app->singleton('analogue', function ($app) { |
|
| 39 | + $this->app->singleton('analogue', function($app) { |
|
| 40 | 40 | $db = $app['db']; |
| 41 | 41 | |
| 42 | 42 | $connectionProvider = new IlluminateConnectionProvider($db); |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | return $manager; |
| 76 | 76 | }); |
| 77 | 77 | |
| 78 | - $this->app->bind(Manager::class, function ($app) { |
|
| 78 | + $this->app->bind(Manager::class, function($app) { |
|
| 79 | 79 | return $app->make('analogue'); |
| 80 | 80 | }); |
| 81 | 81 | } |
@@ -15,79 +15,79 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class AnalogueServiceProvider extends ServiceProvider |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Indicates if loading of the provider is deferred. |
|
| 20 | - * |
|
| 21 | - * @var bool |
|
| 22 | - */ |
|
| 23 | - protected $defer = false; |
|
| 18 | + /** |
|
| 19 | + * Indicates if loading of the provider is deferred. |
|
| 20 | + * |
|
| 21 | + * @var bool |
|
| 22 | + */ |
|
| 23 | + protected $defer = false; |
|
| 24 | 24 | |
| 25 | - public function boot() |
|
| 26 | - { |
|
| 27 | - // |
|
| 28 | - } |
|
| 25 | + public function boot() |
|
| 26 | + { |
|
| 27 | + // |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Register the service provider. |
|
| 32 | - * |
|
| 33 | - * @return void |
|
| 34 | - */ |
|
| 35 | - public function register() |
|
| 36 | - { |
|
| 37 | - // Experimenting autoloading proxies |
|
| 30 | + /** |
|
| 31 | + * Register the service provider. |
|
| 32 | + * |
|
| 33 | + * @return void |
|
| 34 | + */ |
|
| 35 | + public function register() |
|
| 36 | + { |
|
| 37 | + // Experimenting autoloading proxies |
|
| 38 | 38 | |
| 39 | - $this->app->singleton('analogue', function ($app) { |
|
| 40 | - $db = $app['db']; |
|
| 39 | + $this->app->singleton('analogue', function ($app) { |
|
| 40 | + $db = $app['db']; |
|
| 41 | 41 | |
| 42 | - $connectionProvider = new IlluminateConnectionProvider($db); |
|
| 42 | + $connectionProvider = new IlluminateConnectionProvider($db); |
|
| 43 | 43 | |
| 44 | - $illuminate = new IlluminateDriver($connectionProvider); |
|
| 44 | + $illuminate = new IlluminateDriver($connectionProvider); |
|
| 45 | 45 | |
| 46 | - $driverManager = new DriverManager(); |
|
| 46 | + $driverManager = new DriverManager(); |
|
| 47 | 47 | |
| 48 | - $driverManager->addDriver($illuminate); |
|
| 48 | + $driverManager->addDriver($illuminate); |
|
| 49 | 49 | |
| 50 | - $event = $app->make('events'); |
|
| 50 | + $event = $app->make('events'); |
|
| 51 | 51 | |
| 52 | - $manager = new Manager($driverManager, $event); |
|
| 52 | + $manager = new Manager($driverManager, $event); |
|
| 53 | 53 | |
| 54 | - $manager->registerPlugin(\Analogue\ORM\Plugins\Timestamps\TimestampsPlugin::class); |
|
| 55 | - $manager->registerPlugin(\Analogue\ORM\Plugins\SoftDeletes\SoftDeletesPlugin::class); |
|
| 54 | + $manager->registerPlugin(\Analogue\ORM\Plugins\Timestamps\TimestampsPlugin::class); |
|
| 55 | + $manager->registerPlugin(\Analogue\ORM\Plugins\SoftDeletes\SoftDeletesPlugin::class); |
|
| 56 | 56 | |
| 57 | - // If the cache is pre laravel 5.5, it doesn't implements PSR-16, so we'll skip it. |
|
| 58 | - $cache = $app->make(CacheRepository::class); |
|
| 57 | + // If the cache is pre laravel 5.5, it doesn't implements PSR-16, so we'll skip it. |
|
| 58 | + $cache = $app->make(CacheRepository::class); |
|
| 59 | 59 | |
| 60 | - if ($cache instanceof CacheInterface) { |
|
| 61 | - $manager->setCache($cache); |
|
| 62 | - } |
|
| 60 | + if ($cache instanceof CacheInterface) { |
|
| 61 | + $manager->setCache($cache); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - $proxyPath = storage_path('framework/analogue/proxies'); |
|
| 64 | + $proxyPath = storage_path('framework/analogue/proxies'); |
|
| 65 | 65 | |
| 66 | - if (!file_exists($proxyPath)) { |
|
| 67 | - mkdir($proxyPath, 0777, true); |
|
| 68 | - } |
|
| 66 | + if (!file_exists($proxyPath)) { |
|
| 67 | + mkdir($proxyPath, 0777, true); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - $proxyConfig = new \ProxyManager\Configuration(); |
|
| 71 | - $proxyConfig->setProxiesTargetDir($proxyPath); |
|
| 72 | - spl_autoload_register($proxyConfig->getProxyAutoloader()); |
|
| 70 | + $proxyConfig = new \ProxyManager\Configuration(); |
|
| 71 | + $proxyConfig->setProxiesTargetDir($proxyPath); |
|
| 72 | + spl_autoload_register($proxyConfig->getProxyAutoloader()); |
|
| 73 | 73 | |
| 74 | - $manager->setProxyPath($proxyPath); |
|
| 74 | + $manager->setProxyPath($proxyPath); |
|
| 75 | 75 | |
| 76 | - return $manager; |
|
| 77 | - }); |
|
| 76 | + return $manager; |
|
| 77 | + }); |
|
| 78 | 78 | |
| 79 | - $this->app->bind(Manager::class, function ($app) { |
|
| 80 | - return $app->make('analogue'); |
|
| 81 | - }); |
|
| 82 | - } |
|
| 79 | + $this->app->bind(Manager::class, function ($app) { |
|
| 80 | + return $app->make('analogue'); |
|
| 81 | + }); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Get the services provided by the provider. |
|
| 86 | - * |
|
| 87 | - * @return array |
|
| 88 | - */ |
|
| 89 | - public function provides() |
|
| 90 | - { |
|
| 91 | - return ['analogue']; |
|
| 92 | - } |
|
| 84 | + /** |
|
| 85 | + * Get the services provided by the provider. |
|
| 86 | + * |
|
| 87 | + * @return array |
|
| 88 | + */ |
|
| 89 | + public function provides() |
|
| 90 | + { |
|
| 91 | + return ['analogue']; |
|
| 92 | + } |
|
| 93 | 93 | } |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | protected function addWithTrashed(Query $query) |
| 75 | 75 | { |
| 76 | - $query->macro('withTrashed', function (Query $query) { |
|
| 76 | + $query->macro('withTrashed', function(Query $query) { |
|
| 77 | 77 | $this->remove($query); |
| 78 | 78 | |
| 79 | 79 | return $query; |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | protected function addOnlyTrashed(Query $query) |
| 90 | 90 | { |
| 91 | - $query->macro('onlyTrashed', function (Query $query) { |
|
| 91 | + $query->macro('onlyTrashed', function(Query $query) { |
|
| 92 | 92 | $this->remove($query); |
| 93 | 93 | |
| 94 | 94 | $query->getQuery()->whereNotNull($query->getMapper()->getEntityMap()->getQualifiedDeletedAtColumn()); |
@@ -7,107 +7,107 @@ |
||
| 7 | 7 | |
| 8 | 8 | class SoftDeletingScope implements ScopeInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * All of the extensions to be added to the builder. |
|
| 12 | - * |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 15 | - protected $extensions = [ |
|
| 16 | - 'WithTrashed', |
|
| 17 | - 'OnlyTrashed', |
|
| 18 | - ]; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function apply(Query $query) |
|
| 24 | - { |
|
| 25 | - $entityMap = $query->getMapper()->getEntityMap(); |
|
| 26 | - |
|
| 27 | - $query->whereNull($entityMap->getQualifiedDeletedAtColumn()); |
|
| 28 | - |
|
| 29 | - $this->extend($query); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * {@inheritdoc} |
|
| 34 | - */ |
|
| 35 | - public function remove(Query $query) |
|
| 36 | - { |
|
| 37 | - $column = $query->getMapper()->getEntityMap()->getQualifiedDeletedAtColumn(); |
|
| 38 | - |
|
| 39 | - $query = $query->getQuery(); |
|
| 40 | - |
|
| 41 | - foreach ((array) $query->wheres as $key => $where) { |
|
| 42 | - // If the where clause is a soft delete date constraint, we will remove it from |
|
| 43 | - // the query and reset the keys on the wheres. This allows this developer to |
|
| 44 | - // include deleted model in a relationship result set that is lazy loaded. |
|
| 45 | - if ($this->isSoftDeleteConstraint($where, $column)) { |
|
| 46 | - unset($query->wheres[$key]); |
|
| 47 | - |
|
| 48 | - $query->wheres = array_values($query->wheres); |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Extend the query builder with the needed functions. |
|
| 55 | - * |
|
| 56 | - * @param \Analogue\ORM\System\Query $query |
|
| 57 | - * |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function extend(Query $query) |
|
| 61 | - { |
|
| 62 | - foreach ($this->extensions as $extension) { |
|
| 63 | - $this->{"add{$extension}"}($query); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Add the with-trashed extension to the builder. |
|
| 69 | - * |
|
| 70 | - * @param \Analogue\ORM\System\Query $query |
|
| 71 | - * |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - protected function addWithTrashed(Query $query) |
|
| 75 | - { |
|
| 76 | - $query->macro('withTrashed', function (Query $query) { |
|
| 77 | - $this->remove($query); |
|
| 78 | - |
|
| 79 | - return $query; |
|
| 80 | - }); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Add the only-trashed extension to the builder. |
|
| 85 | - * |
|
| 86 | - * @param \Analogue\ORM\System\Query $query |
|
| 87 | - * |
|
| 88 | - * @return void |
|
| 89 | - */ |
|
| 90 | - protected function addOnlyTrashed(Query $query) |
|
| 91 | - { |
|
| 92 | - $query->macro('onlyTrashed', function (Query $query) { |
|
| 93 | - $this->remove($query); |
|
| 94 | - |
|
| 95 | - $query->getQuery()->whereNotNull($query->getMapper()->getEntityMap()->getQualifiedDeletedAtColumn()); |
|
| 96 | - |
|
| 97 | - return $query; |
|
| 98 | - }); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Determine if the given where clause is a soft delete constraint. |
|
| 103 | - * |
|
| 104 | - * @param array $where |
|
| 105 | - * @param string $column |
|
| 106 | - * |
|
| 107 | - * @return bool |
|
| 108 | - */ |
|
| 109 | - protected function isSoftDeleteConstraint(array $where, $column) |
|
| 110 | - { |
|
| 111 | - return $where['type'] == 'Null' && $where['column'] == $column; |
|
| 112 | - } |
|
| 10 | + /** |
|
| 11 | + * All of the extensions to be added to the builder. |
|
| 12 | + * |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | + protected $extensions = [ |
|
| 16 | + 'WithTrashed', |
|
| 17 | + 'OnlyTrashed', |
|
| 18 | + ]; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function apply(Query $query) |
|
| 24 | + { |
|
| 25 | + $entityMap = $query->getMapper()->getEntityMap(); |
|
| 26 | + |
|
| 27 | + $query->whereNull($entityMap->getQualifiedDeletedAtColumn()); |
|
| 28 | + |
|
| 29 | + $this->extend($query); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * {@inheritdoc} |
|
| 34 | + */ |
|
| 35 | + public function remove(Query $query) |
|
| 36 | + { |
|
| 37 | + $column = $query->getMapper()->getEntityMap()->getQualifiedDeletedAtColumn(); |
|
| 38 | + |
|
| 39 | + $query = $query->getQuery(); |
|
| 40 | + |
|
| 41 | + foreach ((array) $query->wheres as $key => $where) { |
|
| 42 | + // If the where clause is a soft delete date constraint, we will remove it from |
|
| 43 | + // the query and reset the keys on the wheres. This allows this developer to |
|
| 44 | + // include deleted model in a relationship result set that is lazy loaded. |
|
| 45 | + if ($this->isSoftDeleteConstraint($where, $column)) { |
|
| 46 | + unset($query->wheres[$key]); |
|
| 47 | + |
|
| 48 | + $query->wheres = array_values($query->wheres); |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Extend the query builder with the needed functions. |
|
| 55 | + * |
|
| 56 | + * @param \Analogue\ORM\System\Query $query |
|
| 57 | + * |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function extend(Query $query) |
|
| 61 | + { |
|
| 62 | + foreach ($this->extensions as $extension) { |
|
| 63 | + $this->{"add{$extension}"}($query); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Add the with-trashed extension to the builder. |
|
| 69 | + * |
|
| 70 | + * @param \Analogue\ORM\System\Query $query |
|
| 71 | + * |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + protected function addWithTrashed(Query $query) |
|
| 75 | + { |
|
| 76 | + $query->macro('withTrashed', function (Query $query) { |
|
| 77 | + $this->remove($query); |
|
| 78 | + |
|
| 79 | + return $query; |
|
| 80 | + }); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Add the only-trashed extension to the builder. |
|
| 85 | + * |
|
| 86 | + * @param \Analogue\ORM\System\Query $query |
|
| 87 | + * |
|
| 88 | + * @return void |
|
| 89 | + */ |
|
| 90 | + protected function addOnlyTrashed(Query $query) |
|
| 91 | + { |
|
| 92 | + $query->macro('onlyTrashed', function (Query $query) { |
|
| 93 | + $this->remove($query); |
|
| 94 | + |
|
| 95 | + $query->getQuery()->whereNotNull($query->getMapper()->getEntityMap()->getQualifiedDeletedAtColumn()); |
|
| 96 | + |
|
| 97 | + return $query; |
|
| 98 | + }); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Determine if the given where clause is a soft delete constraint. |
|
| 103 | + * |
|
| 104 | + * @param array $where |
|
| 105 | + * @param string $column |
|
| 106 | + * |
|
| 107 | + * @return bool |
|
| 108 | + */ |
|
| 109 | + protected function isSoftDeleteConstraint(array $where, $column) |
|
| 110 | + { |
|
| 111 | + return $where['type'] == 'Null' && $where['column'] == $column; |
|
| 112 | + } |
|
| 113 | 113 | } |
@@ -6,5 +6,5 @@ |
||
| 6 | 6 | |
| 7 | 7 | class EntityMapNotFoundException extends RuntimeException |
| 8 | 8 | { |
| 9 | - // |
|
| 9 | + // |
|
| 10 | 10 | } |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
| 74 | - * @param $class |
|
| 74 | + * @param string $class |
|
| 75 | 75 | */ |
| 76 | 76 | public function setClass($class) |
| 77 | 77 | { |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** |
| 82 | - * @return mixed |
|
| 82 | + * @return string |
|
| 83 | 83 | */ |
| 84 | 84 | public function getClass() |
| 85 | 85 | { |
@@ -7,97 +7,97 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class ValueMap |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var string |
|
| 12 | - */ |
|
| 13 | - protected $name; |
|
| 10 | + /** |
|
| 11 | + * @var string |
|
| 12 | + */ |
|
| 13 | + protected $name; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - protected $class; |
|
| 15 | + /** |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + protected $class; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $embeddables = []; |
|
| 20 | + /** |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $embeddables = []; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - protected $attributes = []; |
|
| 25 | + /** |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + protected $attributes = []; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - protected $properties = []; |
|
| 30 | + /** |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + protected $properties = []; |
|
| 34 | 34 | |
| 35 | - protected $arrayName = null; |
|
| 35 | + protected $arrayName = null; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @return array |
|
| 39 | - */ |
|
| 40 | - public function getAttributes() |
|
| 41 | - { |
|
| 42 | - return $this->attributes; |
|
| 43 | - } |
|
| 37 | + /** |
|
| 38 | + * @return array |
|
| 39 | + */ |
|
| 40 | + public function getAttributes() |
|
| 41 | + { |
|
| 42 | + return $this->attributes; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * [getAttributesArrayName description]. |
|
| 47 | - * |
|
| 48 | - * @return [type] [description] |
|
| 49 | - */ |
|
| 50 | - public function getAttributesArrayName() |
|
| 51 | - { |
|
| 52 | - return $this->arrayName; |
|
| 53 | - } |
|
| 45 | + /** |
|
| 46 | + * [getAttributesArrayName description]. |
|
| 47 | + * |
|
| 48 | + * @return [type] [description] |
|
| 49 | + */ |
|
| 50 | + public function getAttributesArrayName() |
|
| 51 | + { |
|
| 52 | + return $this->arrayName; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - public function usesAttributesArray() |
|
| 56 | - { |
|
| 57 | - return $this->arrayName != null; |
|
| 58 | - } |
|
| 55 | + public function usesAttributesArray() |
|
| 56 | + { |
|
| 57 | + return $this->arrayName != null; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @return array |
|
| 62 | - */ |
|
| 63 | - public function getProperties() |
|
| 64 | - { |
|
| 65 | - return $this->properties; |
|
| 66 | - } |
|
| 60 | + /** |
|
| 61 | + * @return array |
|
| 62 | + */ |
|
| 63 | + public function getProperties() |
|
| 64 | + { |
|
| 65 | + return $this->properties; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @return array |
|
| 70 | - */ |
|
| 71 | - public function getEmbeddables() |
|
| 72 | - { |
|
| 73 | - return $this->embeddables; |
|
| 74 | - } |
|
| 68 | + /** |
|
| 69 | + * @return array |
|
| 70 | + */ |
|
| 71 | + public function getEmbeddables() |
|
| 72 | + { |
|
| 73 | + return $this->embeddables; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @param $class |
|
| 78 | - */ |
|
| 79 | - public function setClass($class) |
|
| 80 | - { |
|
| 81 | - $this->class = $class; |
|
| 82 | - } |
|
| 76 | + /** |
|
| 77 | + * @param $class |
|
| 78 | + */ |
|
| 79 | + public function setClass($class) |
|
| 80 | + { |
|
| 81 | + $this->class = $class; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * @return mixed |
|
| 86 | - */ |
|
| 87 | - public function getClass() |
|
| 88 | - { |
|
| 89 | - return $this->class; |
|
| 90 | - } |
|
| 84 | + /** |
|
| 85 | + * @return mixed |
|
| 86 | + */ |
|
| 87 | + public function getClass() |
|
| 88 | + { |
|
| 89 | + return $this->class; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * @return string |
|
| 94 | - */ |
|
| 95 | - public function getName() |
|
| 96 | - { |
|
| 97 | - if (isset($this->name)) { |
|
| 98 | - return $this->name; |
|
| 99 | - } else { |
|
| 100 | - return class_basename($this); |
|
| 101 | - } |
|
| 102 | - } |
|
| 92 | + /** |
|
| 93 | + * @return string |
|
| 94 | + */ |
|
| 95 | + public function getName() |
|
| 96 | + { |
|
| 97 | + if (isset($this->name)) { |
|
| 98 | + return $this->name; |
|
| 99 | + } else { |
|
| 100 | + return class_basename($this); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | 103 | } |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | * Create a mapper for a given entity. |
| 144 | 144 | * |
| 145 | 145 | * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
| 146 | - * @param mixed $entityMap |
|
| 146 | + * @param null|EntityMap $entityMap |
|
| 147 | 147 | * |
| 148 | 148 | * @throws MappingException |
| 149 | 149 | * @throws \InvalidArgumentException |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | /** |
| 252 | 252 | * Check if the entity is already registered. |
| 253 | 253 | * |
| 254 | - * @param string|Entity $entity |
|
| 254 | + * @param string $entity |
|
| 255 | 255 | * |
| 256 | 256 | * @return bool |
| 257 | 257 | */ |
@@ -333,7 +333,7 @@ discard block |
||
| 333 | 333 | /** |
| 334 | 334 | * Register an entity. |
| 335 | 335 | * |
| 336 | - * @param string|\Analogue\ORM\Mappable $entity entity's class name |
|
| 336 | + * @param string $entity entity's class name |
|
| 337 | 337 | * @param string|EntityMap $entityMap map's class name |
| 338 | 338 | * |
| 339 | 339 | * @throws MappingException |
@@ -21,797 +21,797 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Manager |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * Manager instance. |
|
| 26 | - * |
|
| 27 | - * @var Manager |
|
| 28 | - */ |
|
| 29 | - protected static $instance; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Driver Manager. |
|
| 33 | - * |
|
| 34 | - * @var \Analogue\ORM\Drivers\Manager |
|
| 35 | - */ |
|
| 36 | - protected $drivers; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Registered entity classes and corresponding map objects. |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - protected $entityClasses = []; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Key value store of ValueObject Classes and corresponding map classes. |
|
| 47 | - * |
|
| 48 | - * @var array|ValueMap[] |
|
| 49 | - */ |
|
| 50 | - protected $valueClasses = []; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Morph map. |
|
| 54 | - */ |
|
| 55 | - protected $morphMap = []; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Loaded Mappers. |
|
| 59 | - * |
|
| 60 | - * @var array |
|
| 61 | - */ |
|
| 62 | - protected $mappers = []; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Loaded Repositories. |
|
| 66 | - * |
|
| 67 | - * @var array |
|
| 68 | - */ |
|
| 69 | - protected $repositories = []; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Event dispatcher instance. |
|
| 73 | - * |
|
| 74 | - * @var \Illuminate\Contracts\Events\Dispatcher |
|
| 75 | - */ |
|
| 76 | - protected $eventDispatcher; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Optionnal Cache implementation. |
|
| 80 | - * |
|
| 81 | - * @var CacheInterface |
|
| 82 | - */ |
|
| 83 | - protected $cache = null; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Available Analogue Events. |
|
| 87 | - * |
|
| 88 | - * @var array |
|
| 89 | - */ |
|
| 90 | - protected $events = [ |
|
| 91 | - 'initializing' => \Analogue\ORM\Events\Initializing::class, |
|
| 92 | - 'initialized' => \Analogue\ORM\Events\Initialized::class, |
|
| 93 | - 'storing' => \Analogue\ORM\Events\Storing::class, |
|
| 94 | - 'stored' => \Analogue\ORM\Events\Stored::class, |
|
| 95 | - 'creating' => \Analogue\ORM\Events\Creating::class, |
|
| 96 | - 'created' => \Analogue\ORM\Events\Created::class, |
|
| 97 | - 'updating' => \Analogue\ORM\Events\Updating::class, |
|
| 98 | - 'updated' => \Analogue\ORM\Events\Updated::class, |
|
| 99 | - 'deleting' => \Analogue\ORM\Events\Deleting::class, |
|
| 100 | - 'deleted' => \Analogue\ORM\Events\Deleted::class, |
|
| 101 | - ]; |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * If strictMode is set to true, Manager will throw |
|
| 105 | - * an exception if no entityMap class are registered |
|
| 106 | - * for a given entity class. |
|
| 107 | - * |
|
| 108 | - * @var bool |
|
| 109 | - */ |
|
| 110 | - protected $strictMode = true; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * We can add namespaces in this array where the manager |
|
| 114 | - * will look for when auto registering entityMaps. |
|
| 115 | - * |
|
| 116 | - * @var array |
|
| 117 | - */ |
|
| 118 | - protected $customMapNamespaces = []; |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Path where generated proxy classes are stored. |
|
| 122 | - * |
|
| 123 | - * @var string |
|
| 124 | - */ |
|
| 125 | - protected $proxyPath; |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @param \Analogue\ORM\Drivers\Manager $driverManager |
|
| 129 | - * @param Dispatcher $event |
|
| 130 | - */ |
|
| 131 | - public function __construct(DriverManager $driverManager, Dispatcher $event) |
|
| 132 | - { |
|
| 133 | - $this->drivers = $driverManager; |
|
| 134 | - |
|
| 135 | - $this->eventDispatcher = $event; |
|
| 136 | - |
|
| 137 | - static::$instance = $this; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Create a mapper for a given entity (static alias). |
|
| 142 | - * |
|
| 143 | - * @param \Analogue\ORM\Mappable|string $entity |
|
| 144 | - * @param null|EntityMap $entityMap |
|
| 145 | - * |
|
| 146 | - * @throws MappingException |
|
| 147 | - * @throws \InvalidArgumentException |
|
| 148 | - * |
|
| 149 | - * @return Mapper |
|
| 150 | - */ |
|
| 151 | - public static function getMapper($entity, $entityMap = null) |
|
| 152 | - { |
|
| 153 | - return static::$instance->mapper($entity, $entityMap); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Create a mapper for a given entity. |
|
| 158 | - * |
|
| 159 | - * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
|
| 160 | - * @param mixed $entityMap |
|
| 161 | - * |
|
| 162 | - * @throws MappingException |
|
| 163 | - * @throws \InvalidArgumentException |
|
| 164 | - * |
|
| 165 | - * @return Mapper |
|
| 166 | - */ |
|
| 167 | - public function mapper($entity, $entityMap = null) |
|
| 168 | - { |
|
| 169 | - if ($entity instanceof Wrapper) { |
|
| 170 | - throw new MappingException('Tried to instantiate mapper on wrapped Entity'); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - $entity = $this->resolveEntityClass($entity); |
|
| 174 | - |
|
| 175 | - $entity = $this->getInverseMorphMap($entity); |
|
| 176 | - |
|
| 177 | - // Return existing mapper instance if exists. |
|
| 178 | - if (array_key_exists($entity, $this->mappers)) { |
|
| 179 | - return $this->mappers[$entity]; |
|
| 180 | - } else { |
|
| 181 | - return $this->buildMapper($entity, $entityMap); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * This method resolve entity class from mappable instances or iterators. |
|
| 187 | - * |
|
| 188 | - * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
|
| 189 | - * |
|
| 190 | - * @throws \InvalidArgumentException |
|
| 191 | - * |
|
| 192 | - * @return string |
|
| 193 | - */ |
|
| 194 | - protected function resolveEntityClass($entity) |
|
| 195 | - { |
|
| 196 | - // We first check if the entity is traversable and we'll resolve |
|
| 197 | - // the entity based on the first item of the object. |
|
| 198 | - if ($this->isTraversable($entity)) { |
|
| 199 | - if (!count($entity)) { |
|
| 200 | - throw new \InvalidArgumentException('Length of Entity collection must be greater than 0'); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - $firstEntityItem = ($entity instanceof \Iterator) |
|
| 204 | - ? $entity->current() |
|
| 205 | - : current($entity); |
|
| 206 | - |
|
| 207 | - return $this->resolveEntityClass($firstEntityItem); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - if (is_object($entity)) { |
|
| 211 | - return get_class($entity); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - if (is_string($entity)) { |
|
| 215 | - return $entity; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - throw new \InvalidArgumentException('Invalid entity type'); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param string $key |
|
| 223 | - * |
|
| 224 | - * @return string |
|
| 225 | - */ |
|
| 226 | - public function getInverseMorphMap($key) |
|
| 227 | - { |
|
| 228 | - return array_key_exists($key, $this->morphMap) ? $this->morphMap[$key] : $key; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Build a new Mapper instance for a given Entity. |
|
| 233 | - * |
|
| 234 | - * @param string $entity |
|
| 235 | - * @param mixed $entityMap |
|
| 236 | - * |
|
| 237 | - * @throws MappingException |
|
| 238 | - * |
|
| 239 | - * @return Mapper |
|
| 240 | - */ |
|
| 241 | - protected function buildMapper($entity, $entityMap) |
|
| 242 | - { |
|
| 243 | - // If an EntityMap hasn't been manually registered by the user |
|
| 244 | - // register it at runtime. |
|
| 245 | - if (!$this->isRegisteredEntity($entity)) { |
|
| 246 | - $this->register($entity, $entityMap); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - $entityMap = $this->entityClasses[$entity]; |
|
| 250 | - |
|
| 251 | - $factory = new MapperFactory($this->drivers, $this->eventDispatcher, $this); |
|
| 252 | - |
|
| 253 | - $mapper = $factory->make($entity, $entityMap); |
|
| 254 | - |
|
| 255 | - $this->mappers[$entity] = $mapper; |
|
| 256 | - |
|
| 257 | - // At this point we can safely call the boot() method on the entityMap as |
|
| 258 | - // the mapper is now instantiated & registered within the manager. |
|
| 259 | - |
|
| 260 | - if (!$entityMap->isBooted()) { |
|
| 261 | - $entityMap->boot(); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - // If a cache is defined, use it to store the entityMap |
|
| 265 | - if ($this->cache !== null && !$this->cache->has($entityMap->getClass()) && !$this->isAnonymous($entityMap)) { |
|
| 266 | - $this->cache->set($entityMap->getClass(), serialize($entityMap), 1440); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - return $mapper; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Check if an Object is an anonymous class instance. |
|
| 274 | - * |
|
| 275 | - * @param mixed $class |
|
| 276 | - * |
|
| 277 | - * @return bool |
|
| 278 | - */ |
|
| 279 | - protected function isAnonymous($class) : bool |
|
| 280 | - { |
|
| 281 | - $instance = new ReflectionClass($class); |
|
| 282 | - |
|
| 283 | - return $instance->isAnonymous(); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Check if the entity is already registered. |
|
| 288 | - * |
|
| 289 | - * @param string|Entity $entity |
|
| 290 | - * |
|
| 291 | - * @return bool |
|
| 292 | - */ |
|
| 293 | - public function isRegisteredEntity($entity) |
|
| 294 | - { |
|
| 295 | - if (!is_string($entity)) { |
|
| 296 | - $entity = get_class($entity); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - return array_key_exists($entity, $this->entityClasses); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Return an array containing registered entities & entityMap instances. |
|
| 304 | - * |
|
| 305 | - * @return array |
|
| 306 | - */ |
|
| 307 | - public function getRegisteredEntities() |
|
| 308 | - { |
|
| 309 | - return $this->entityClasses; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Check if a value class is already registered. |
|
| 314 | - * |
|
| 315 | - * @param string|\stdClass $object |
|
| 316 | - * |
|
| 317 | - * @return bool |
|
| 318 | - */ |
|
| 319 | - public function isRegisteredValueObject($object) |
|
| 320 | - { |
|
| 321 | - if (!is_string($object)) { |
|
| 322 | - $object = get_class($object); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - return array_key_exists($object, $this->valueClasses); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Return true if an object is an array or iterator. |
|
| 330 | - * |
|
| 331 | - * @param mixed $argument |
|
| 332 | - * |
|
| 333 | - * @return bool |
|
| 334 | - */ |
|
| 335 | - public function isTraversable($argument) |
|
| 336 | - { |
|
| 337 | - return $argument instanceof \Traversable || is_array($argument); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * Set strict mode for entityMap instantiation. |
|
| 342 | - * |
|
| 343 | - * @param bool $mode |
|
| 344 | - */ |
|
| 345 | - public function setStrictMode($mode) |
|
| 346 | - { |
|
| 347 | - $this->strictMode = $mode; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Define a generated proxy class path. |
|
| 352 | - * |
|
| 353 | - * @param string $path |
|
| 354 | - */ |
|
| 355 | - public function setProxyPath(string $path) |
|
| 356 | - { |
|
| 357 | - $this->proxyPath = $path; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Return proxy path if defined. |
|
| 362 | - * |
|
| 363 | - * @return string|null |
|
| 364 | - */ |
|
| 365 | - public function getProxyPath() |
|
| 366 | - { |
|
| 367 | - return $this->proxyPath; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Register a namespace in where Analogue |
|
| 372 | - * will scan for EntityMaps & ValueMaps. |
|
| 373 | - * |
|
| 374 | - * @param string $namespace |
|
| 375 | - * |
|
| 376 | - * @return void |
|
| 377 | - */ |
|
| 378 | - public function registerMapNamespace($namespace) |
|
| 379 | - { |
|
| 380 | - // Add a trailing antislash to namespace if not present |
|
| 381 | - if (substr('testers', -1) != '\\') { |
|
| 382 | - $namespace = $namespace.'\\'; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - $this->customMapNamespaces[] = $namespace; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Register an entity. |
|
| 390 | - * |
|
| 391 | - * @param string|\Analogue\ORM\Mappable $entity entity's class name |
|
| 392 | - * @param string|EntityMap $entityMap map's class name |
|
| 393 | - * |
|
| 394 | - * @throws MappingException |
|
| 395 | - * |
|
| 396 | - * @return void |
|
| 397 | - */ |
|
| 398 | - public function register($entity, $entityMap = null) |
|
| 399 | - { |
|
| 400 | - // If an object is provider, get the class name from it |
|
| 401 | - if (!is_string($entity)) { |
|
| 402 | - $entity = get_class($entity); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - if ($this->isRegisteredEntity($entity)) { |
|
| 406 | - throw new MappingException("Entity $entity is already registered."); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - if (!class_exists($entity)) { |
|
| 410 | - throw new MappingException("Class $entity does not exists"); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - if ($entityMap === null) { |
|
| 414 | - $entityMap = $this->getEntityMapInstanceFor($entity); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - if (is_string($entityMap)) { |
|
| 418 | - $entityMap = new $entityMap(); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - if (!$entityMap instanceof EntityMap) { |
|
| 422 | - throw new MappingException(get_class($entityMap).' must be an instance of EntityMap.'); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - $entityMap->setClass($entity); |
|
| 426 | - |
|
| 427 | - $this->entityClasses[$entity] = $entityMap; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * Get the entity map instance for a custom entity. |
|
| 432 | - * |
|
| 433 | - * @param string $entity |
|
| 434 | - * |
|
| 435 | - * @return \Analogue\ORM\EntityMap |
|
| 436 | - */ |
|
| 437 | - protected function getEntityMapInstanceFor($entity) |
|
| 438 | - { |
|
| 439 | - // First, we'll try to load the entity map from cache, to |
|
| 440 | - // save from time consuming parsing of the relationships |
|
| 441 | - $entityMap = $this->getEntityMapInstanceFromCache($entity); |
|
| 442 | - |
|
| 443 | - if ($entityMap !== null) { |
|
| 444 | - return $entityMap; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - if (class_exists($entity.'Map')) { |
|
| 448 | - $map = $entity.'Map'; |
|
| 449 | - $map = new $map(); |
|
| 450 | - |
|
| 451 | - return $map; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - if ($map = $this->getMapFromNamespaces($entity)) { |
|
| 455 | - return $map; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - if ($this->strictMode) { |
|
| 459 | - throw new EntityMapNotFoundException("No Map registered for $entity"); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - $map = $this->getNewEntityMap(); |
|
| 463 | - |
|
| 464 | - return $map; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Get Entity Map instance from cache. |
|
| 469 | - * |
|
| 470 | - * @param string $entityClass |
|
| 471 | - * |
|
| 472 | - * @return EntityMap|null |
|
| 473 | - */ |
|
| 474 | - protected function getEntityMapInstanceFromCache(string $entityClass) |
|
| 475 | - { |
|
| 476 | - if ($this->cache == null) { |
|
| 477 | - return; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - if ($this->cache->has($entityClass)) { |
|
| 481 | - return unserialize($this->cache->get($entityClass)); |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * Scan through registered custom namespace |
|
| 487 | - * for an Entity/ValueMap. |
|
| 488 | - * |
|
| 489 | - * @param string $class |
|
| 490 | - * |
|
| 491 | - * @return ValueMap|EntityMap|bool |
|
| 492 | - */ |
|
| 493 | - protected function getMapFromNamespaces($class) |
|
| 494 | - { |
|
| 495 | - foreach ($this->customMapNamespaces as $namespace) { |
|
| 496 | - if ($map = $this->findMapInNamespace($class, $namespace)) { |
|
| 497 | - return $map; |
|
| 498 | - } |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - return false; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Look in a custom namespace for an Entity/ValueMap. |
|
| 506 | - * |
|
| 507 | - * @param string $class |
|
| 508 | - * @param string $namespace |
|
| 509 | - * |
|
| 510 | - * @return ValueMap|EntityMap|bool |
|
| 511 | - */ |
|
| 512 | - protected function findMapInNamespace($class, $namespace) |
|
| 513 | - { |
|
| 514 | - $parts = explode('\\', $class); |
|
| 515 | - |
|
| 516 | - $baseClass = $parts[count($parts) - 1]; |
|
| 517 | - |
|
| 518 | - $expectedClass = $namespace.$baseClass.'Map'; |
|
| 519 | - |
|
| 520 | - if (class_exists($expectedClass)) { |
|
| 521 | - return new $expectedClass(); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - return false; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * Dynamically create an entity map for a custom entity class. |
|
| 529 | - * |
|
| 530 | - * @return EntityMap |
|
| 531 | - */ |
|
| 532 | - protected function getNewEntityMap() |
|
| 533 | - { |
|
| 534 | - return new EntityMap(); |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * Return the Singleton instance of the manager. |
|
| 539 | - * |
|
| 540 | - * @return Manager |
|
| 541 | - */ |
|
| 542 | - public static function getInstance() |
|
| 543 | - { |
|
| 544 | - return static::$instance; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * Return the Driver Manager's instance. |
|
| 549 | - * |
|
| 550 | - * @return \Analogue\ORM\Drivers\Manager |
|
| 551 | - */ |
|
| 552 | - public function getDriverManager() |
|
| 553 | - { |
|
| 554 | - return $this->drivers; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Get the Repository instance for the given Entity. |
|
| 559 | - * |
|
| 560 | - * @param \Analogue\ORM\Mappable|string $entity |
|
| 561 | - * |
|
| 562 | - * @throws \InvalidArgumentException |
|
| 563 | - * @throws MappingException |
|
| 564 | - * |
|
| 565 | - * @return \Analogue\ORM\Repository |
|
| 566 | - */ |
|
| 567 | - public function repository($entity) |
|
| 568 | - { |
|
| 569 | - if (!is_string($entity)) { |
|
| 570 | - $entity = get_class($entity); |
|
| 571 | - } |
|
| 572 | - |
|
| 573 | - // First we check if the repository is not already created. |
|
| 574 | - if (array_key_exists($entity, $this->repositories)) { |
|
| 575 | - return $this->repositories[$entity]; |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - $this->repositories[$entity] = new Repository($this->mapper($entity)); |
|
| 579 | - |
|
| 580 | - return $this->repositories[$entity]; |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * Return true is the object is registered as value object. |
|
| 585 | - * |
|
| 586 | - * @param mixed $object |
|
| 587 | - * |
|
| 588 | - * @return bool |
|
| 589 | - */ |
|
| 590 | - public function isValueObject($object) |
|
| 591 | - { |
|
| 592 | - if (!is_string($object)) { |
|
| 593 | - $object = get_class($object); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - return array_key_exists($object, $this->valueClasses); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * Get the Value Map for a given Value Object Class. |
|
| 601 | - * |
|
| 602 | - * @param string $valueObject |
|
| 603 | - * |
|
| 604 | - * @throws MappingException |
|
| 605 | - * |
|
| 606 | - * @return \Analogue\ORM\ValueMap |
|
| 607 | - */ |
|
| 608 | - public function getValueMap($valueObject) |
|
| 609 | - { |
|
| 610 | - if (!is_string($valueObject)) { |
|
| 611 | - $valueObject = get_class($valueObject); |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - if (!array_key_exists($valueObject, $this->valueClasses)) { |
|
| 615 | - $this->registerValueObject($valueObject); |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - /** @var ValueMap $valueMap */ |
|
| 619 | - $valueMap = new $this->valueClasses[$valueObject](); |
|
| 620 | - |
|
| 621 | - $valueMap->setClass($valueObject); |
|
| 622 | - |
|
| 623 | - return $valueMap; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * Register a Value Object. |
|
| 628 | - * |
|
| 629 | - * @param string $valueObject |
|
| 630 | - * @param string $valueMap |
|
| 631 | - * |
|
| 632 | - * @throws MappingException |
|
| 633 | - * |
|
| 634 | - * @return void |
|
| 635 | - */ |
|
| 636 | - public function registerValueObject($valueObject, $valueMap = null) |
|
| 637 | - { |
|
| 638 | - if (!is_string($valueObject)) { |
|
| 639 | - $valueObject = get_class($valueObject); |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - if ($valueMap === null) { |
|
| 643 | - |
|
| 644 | - // First, we'll look into registered namespaces for Entity Maps, |
|
| 645 | - // if any. Then we'll fallback to the same namespace of the object |
|
| 646 | - if (!$valueMap = $this->getMapFromNamespaces($valueObject)) { |
|
| 647 | - $valueMap = $valueObject.'Map'; |
|
| 648 | - } else { |
|
| 649 | - $valueMap = get_class($valueMap); |
|
| 650 | - } |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - if (!class_exists($valueMap)) { |
|
| 654 | - throw new MappingException("$valueMap doesn't exists"); |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - $this->valueClasses[$valueObject] = $valueMap; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - /** |
|
| 661 | - * Instantiate a new Value Object instance. |
|
| 662 | - * |
|
| 663 | - * @param string $valueObject |
|
| 664 | - * |
|
| 665 | - * @return \Analogue\ORM\ValueObject |
|
| 666 | - */ |
|
| 667 | - public function getValueObjectInstance($valueObject) |
|
| 668 | - { |
|
| 669 | - $prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($valueObject), $valueObject)); |
|
| 670 | - |
|
| 671 | - return $prototype; |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * Set an application cache. |
|
| 676 | - * |
|
| 677 | - * @param \Psr\SimpleCache\CacheInterface $cache |
|
| 678 | - */ |
|
| 679 | - public function setCache(CacheInterface $cache) |
|
| 680 | - { |
|
| 681 | - $this->cache = $cache; |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - /** |
|
| 685 | - * Register Analogue Plugin. |
|
| 686 | - * |
|
| 687 | - * @param string $plugin class |
|
| 688 | - * |
|
| 689 | - * @return void |
|
| 690 | - */ |
|
| 691 | - public function registerPlugin($plugin) |
|
| 692 | - { |
|
| 693 | - /** @var AnaloguePluginInterface $plugin */ |
|
| 694 | - $plugin = new $plugin($this); |
|
| 695 | - |
|
| 696 | - $this->events = array_merge($this->events, $plugin->getCustomEvents()); |
|
| 697 | - |
|
| 698 | - $plugin->register(); |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - /** |
|
| 702 | - * Register event listeners that will be fired regardless the type |
|
| 703 | - * of the entity. |
|
| 704 | - * |
|
| 705 | - * @param string $event |
|
| 706 | - * @param \Closure $callback |
|
| 707 | - * |
|
| 708 | - * @throws \LogicException |
|
| 709 | - * |
|
| 710 | - * @return void |
|
| 711 | - */ |
|
| 712 | - public function registerGlobalEvent($event, $callback) |
|
| 713 | - { |
|
| 714 | - if (!array_key_exists($event, $this->events)) { |
|
| 715 | - throw new \LogicException("Analogue : Event $event doesn't exist"); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - $this->eventDispatcher->listen("analogue.{$event}.*", $callback); |
|
| 719 | - //$this->eventDispatcher->listen($this->events[$event], $callback); |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - /** |
|
| 723 | - * Shortcut to Mapper store. |
|
| 724 | - * |
|
| 725 | - * @param mixed $entity |
|
| 726 | - * |
|
| 727 | - * @throws MappingException |
|
| 728 | - * @throws \InvalidArgumentException |
|
| 729 | - * |
|
| 730 | - * @return mixed |
|
| 731 | - */ |
|
| 732 | - public function store($entity) |
|
| 733 | - { |
|
| 734 | - return $this->mapper($entity)->store($entity); |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - /** |
|
| 738 | - * Shortcut to Mapper delete. |
|
| 739 | - * |
|
| 740 | - * @param mixed $entity |
|
| 741 | - * |
|
| 742 | - * @throws MappingException |
|
| 743 | - * @throws \InvalidArgumentException |
|
| 744 | - * |
|
| 745 | - * @return \Traversable|array |
|
| 746 | - */ |
|
| 747 | - public function delete($entity) |
|
| 748 | - { |
|
| 749 | - return $this->mapper($entity)->delete($entity); |
|
| 750 | - } |
|
| 751 | - |
|
| 752 | - /** |
|
| 753 | - * Shortcut to Mapper query. |
|
| 754 | - * |
|
| 755 | - * @param mixed $entity |
|
| 756 | - * |
|
| 757 | - * @throws MappingException |
|
| 758 | - * @throws \InvalidArgumentException |
|
| 759 | - * |
|
| 760 | - * @return Query |
|
| 761 | - */ |
|
| 762 | - public function query($entity) |
|
| 763 | - { |
|
| 764 | - return $this->mapper($entity)->query(); |
|
| 765 | - } |
|
| 766 | - |
|
| 767 | - /** |
|
| 768 | - * Shortcut to Mapper Global Query. |
|
| 769 | - * |
|
| 770 | - * @param mixed $entity |
|
| 771 | - * |
|
| 772 | - * @throws MappingException |
|
| 773 | - * @throws \InvalidArgumentException |
|
| 774 | - * |
|
| 775 | - * @return Query |
|
| 776 | - */ |
|
| 777 | - public function globalQuery($entity) |
|
| 778 | - { |
|
| 779 | - return $this->mapper($entity)->globalQuery(); |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - /** |
|
| 783 | - * @param array $morphMap |
|
| 784 | - * |
|
| 785 | - * @return $this |
|
| 786 | - */ |
|
| 787 | - public function morphMap(array $morphMap) |
|
| 788 | - { |
|
| 789 | - $this->morphMap = $morphMap; |
|
| 790 | - |
|
| 791 | - return $this; |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - /** |
|
| 795 | - * @param string $class |
|
| 796 | - * |
|
| 797 | - * @return mixed |
|
| 798 | - */ |
|
| 799 | - public function getMorphMap($class) |
|
| 800 | - { |
|
| 801 | - $key = array_search($class, $this->morphMap, false); |
|
| 802 | - |
|
| 803 | - return $key !== false ? $key : $class; |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - /** |
|
| 807 | - * Clear all the cached entities within all mappers. |
|
| 808 | - * |
|
| 809 | - * @return void |
|
| 810 | - */ |
|
| 811 | - public function clearCache() |
|
| 812 | - { |
|
| 813 | - array_map(function ($mapper) { |
|
| 814 | - $mapper->clearCache(); |
|
| 815 | - }, $this->mappers); |
|
| 816 | - } |
|
| 24 | + /** |
|
| 25 | + * Manager instance. |
|
| 26 | + * |
|
| 27 | + * @var Manager |
|
| 28 | + */ |
|
| 29 | + protected static $instance; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Driver Manager. |
|
| 33 | + * |
|
| 34 | + * @var \Analogue\ORM\Drivers\Manager |
|
| 35 | + */ |
|
| 36 | + protected $drivers; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Registered entity classes and corresponding map objects. |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + protected $entityClasses = []; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Key value store of ValueObject Classes and corresponding map classes. |
|
| 47 | + * |
|
| 48 | + * @var array|ValueMap[] |
|
| 49 | + */ |
|
| 50 | + protected $valueClasses = []; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Morph map. |
|
| 54 | + */ |
|
| 55 | + protected $morphMap = []; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Loaded Mappers. |
|
| 59 | + * |
|
| 60 | + * @var array |
|
| 61 | + */ |
|
| 62 | + protected $mappers = []; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Loaded Repositories. |
|
| 66 | + * |
|
| 67 | + * @var array |
|
| 68 | + */ |
|
| 69 | + protected $repositories = []; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Event dispatcher instance. |
|
| 73 | + * |
|
| 74 | + * @var \Illuminate\Contracts\Events\Dispatcher |
|
| 75 | + */ |
|
| 76 | + protected $eventDispatcher; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Optionnal Cache implementation. |
|
| 80 | + * |
|
| 81 | + * @var CacheInterface |
|
| 82 | + */ |
|
| 83 | + protected $cache = null; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Available Analogue Events. |
|
| 87 | + * |
|
| 88 | + * @var array |
|
| 89 | + */ |
|
| 90 | + protected $events = [ |
|
| 91 | + 'initializing' => \Analogue\ORM\Events\Initializing::class, |
|
| 92 | + 'initialized' => \Analogue\ORM\Events\Initialized::class, |
|
| 93 | + 'storing' => \Analogue\ORM\Events\Storing::class, |
|
| 94 | + 'stored' => \Analogue\ORM\Events\Stored::class, |
|
| 95 | + 'creating' => \Analogue\ORM\Events\Creating::class, |
|
| 96 | + 'created' => \Analogue\ORM\Events\Created::class, |
|
| 97 | + 'updating' => \Analogue\ORM\Events\Updating::class, |
|
| 98 | + 'updated' => \Analogue\ORM\Events\Updated::class, |
|
| 99 | + 'deleting' => \Analogue\ORM\Events\Deleting::class, |
|
| 100 | + 'deleted' => \Analogue\ORM\Events\Deleted::class, |
|
| 101 | + ]; |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * If strictMode is set to true, Manager will throw |
|
| 105 | + * an exception if no entityMap class are registered |
|
| 106 | + * for a given entity class. |
|
| 107 | + * |
|
| 108 | + * @var bool |
|
| 109 | + */ |
|
| 110 | + protected $strictMode = true; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * We can add namespaces in this array where the manager |
|
| 114 | + * will look for when auto registering entityMaps. |
|
| 115 | + * |
|
| 116 | + * @var array |
|
| 117 | + */ |
|
| 118 | + protected $customMapNamespaces = []; |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Path where generated proxy classes are stored. |
|
| 122 | + * |
|
| 123 | + * @var string |
|
| 124 | + */ |
|
| 125 | + protected $proxyPath; |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @param \Analogue\ORM\Drivers\Manager $driverManager |
|
| 129 | + * @param Dispatcher $event |
|
| 130 | + */ |
|
| 131 | + public function __construct(DriverManager $driverManager, Dispatcher $event) |
|
| 132 | + { |
|
| 133 | + $this->drivers = $driverManager; |
|
| 134 | + |
|
| 135 | + $this->eventDispatcher = $event; |
|
| 136 | + |
|
| 137 | + static::$instance = $this; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Create a mapper for a given entity (static alias). |
|
| 142 | + * |
|
| 143 | + * @param \Analogue\ORM\Mappable|string $entity |
|
| 144 | + * @param null|EntityMap $entityMap |
|
| 145 | + * |
|
| 146 | + * @throws MappingException |
|
| 147 | + * @throws \InvalidArgumentException |
|
| 148 | + * |
|
| 149 | + * @return Mapper |
|
| 150 | + */ |
|
| 151 | + public static function getMapper($entity, $entityMap = null) |
|
| 152 | + { |
|
| 153 | + return static::$instance->mapper($entity, $entityMap); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Create a mapper for a given entity. |
|
| 158 | + * |
|
| 159 | + * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
|
| 160 | + * @param mixed $entityMap |
|
| 161 | + * |
|
| 162 | + * @throws MappingException |
|
| 163 | + * @throws \InvalidArgumentException |
|
| 164 | + * |
|
| 165 | + * @return Mapper |
|
| 166 | + */ |
|
| 167 | + public function mapper($entity, $entityMap = null) |
|
| 168 | + { |
|
| 169 | + if ($entity instanceof Wrapper) { |
|
| 170 | + throw new MappingException('Tried to instantiate mapper on wrapped Entity'); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + $entity = $this->resolveEntityClass($entity); |
|
| 174 | + |
|
| 175 | + $entity = $this->getInverseMorphMap($entity); |
|
| 176 | + |
|
| 177 | + // Return existing mapper instance if exists. |
|
| 178 | + if (array_key_exists($entity, $this->mappers)) { |
|
| 179 | + return $this->mappers[$entity]; |
|
| 180 | + } else { |
|
| 181 | + return $this->buildMapper($entity, $entityMap); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * This method resolve entity class from mappable instances or iterators. |
|
| 187 | + * |
|
| 188 | + * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
|
| 189 | + * |
|
| 190 | + * @throws \InvalidArgumentException |
|
| 191 | + * |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 194 | + protected function resolveEntityClass($entity) |
|
| 195 | + { |
|
| 196 | + // We first check if the entity is traversable and we'll resolve |
|
| 197 | + // the entity based on the first item of the object. |
|
| 198 | + if ($this->isTraversable($entity)) { |
|
| 199 | + if (!count($entity)) { |
|
| 200 | + throw new \InvalidArgumentException('Length of Entity collection must be greater than 0'); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + $firstEntityItem = ($entity instanceof \Iterator) |
|
| 204 | + ? $entity->current() |
|
| 205 | + : current($entity); |
|
| 206 | + |
|
| 207 | + return $this->resolveEntityClass($firstEntityItem); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + if (is_object($entity)) { |
|
| 211 | + return get_class($entity); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + if (is_string($entity)) { |
|
| 215 | + return $entity; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + throw new \InvalidArgumentException('Invalid entity type'); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param string $key |
|
| 223 | + * |
|
| 224 | + * @return string |
|
| 225 | + */ |
|
| 226 | + public function getInverseMorphMap($key) |
|
| 227 | + { |
|
| 228 | + return array_key_exists($key, $this->morphMap) ? $this->morphMap[$key] : $key; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Build a new Mapper instance for a given Entity. |
|
| 233 | + * |
|
| 234 | + * @param string $entity |
|
| 235 | + * @param mixed $entityMap |
|
| 236 | + * |
|
| 237 | + * @throws MappingException |
|
| 238 | + * |
|
| 239 | + * @return Mapper |
|
| 240 | + */ |
|
| 241 | + protected function buildMapper($entity, $entityMap) |
|
| 242 | + { |
|
| 243 | + // If an EntityMap hasn't been manually registered by the user |
|
| 244 | + // register it at runtime. |
|
| 245 | + if (!$this->isRegisteredEntity($entity)) { |
|
| 246 | + $this->register($entity, $entityMap); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + $entityMap = $this->entityClasses[$entity]; |
|
| 250 | + |
|
| 251 | + $factory = new MapperFactory($this->drivers, $this->eventDispatcher, $this); |
|
| 252 | + |
|
| 253 | + $mapper = $factory->make($entity, $entityMap); |
|
| 254 | + |
|
| 255 | + $this->mappers[$entity] = $mapper; |
|
| 256 | + |
|
| 257 | + // At this point we can safely call the boot() method on the entityMap as |
|
| 258 | + // the mapper is now instantiated & registered within the manager. |
|
| 259 | + |
|
| 260 | + if (!$entityMap->isBooted()) { |
|
| 261 | + $entityMap->boot(); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + // If a cache is defined, use it to store the entityMap |
|
| 265 | + if ($this->cache !== null && !$this->cache->has($entityMap->getClass()) && !$this->isAnonymous($entityMap)) { |
|
| 266 | + $this->cache->set($entityMap->getClass(), serialize($entityMap), 1440); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + return $mapper; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Check if an Object is an anonymous class instance. |
|
| 274 | + * |
|
| 275 | + * @param mixed $class |
|
| 276 | + * |
|
| 277 | + * @return bool |
|
| 278 | + */ |
|
| 279 | + protected function isAnonymous($class) : bool |
|
| 280 | + { |
|
| 281 | + $instance = new ReflectionClass($class); |
|
| 282 | + |
|
| 283 | + return $instance->isAnonymous(); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Check if the entity is already registered. |
|
| 288 | + * |
|
| 289 | + * @param string|Entity $entity |
|
| 290 | + * |
|
| 291 | + * @return bool |
|
| 292 | + */ |
|
| 293 | + public function isRegisteredEntity($entity) |
|
| 294 | + { |
|
| 295 | + if (!is_string($entity)) { |
|
| 296 | + $entity = get_class($entity); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + return array_key_exists($entity, $this->entityClasses); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Return an array containing registered entities & entityMap instances. |
|
| 304 | + * |
|
| 305 | + * @return array |
|
| 306 | + */ |
|
| 307 | + public function getRegisteredEntities() |
|
| 308 | + { |
|
| 309 | + return $this->entityClasses; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Check if a value class is already registered. |
|
| 314 | + * |
|
| 315 | + * @param string|\stdClass $object |
|
| 316 | + * |
|
| 317 | + * @return bool |
|
| 318 | + */ |
|
| 319 | + public function isRegisteredValueObject($object) |
|
| 320 | + { |
|
| 321 | + if (!is_string($object)) { |
|
| 322 | + $object = get_class($object); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + return array_key_exists($object, $this->valueClasses); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Return true if an object is an array or iterator. |
|
| 330 | + * |
|
| 331 | + * @param mixed $argument |
|
| 332 | + * |
|
| 333 | + * @return bool |
|
| 334 | + */ |
|
| 335 | + public function isTraversable($argument) |
|
| 336 | + { |
|
| 337 | + return $argument instanceof \Traversable || is_array($argument); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * Set strict mode for entityMap instantiation. |
|
| 342 | + * |
|
| 343 | + * @param bool $mode |
|
| 344 | + */ |
|
| 345 | + public function setStrictMode($mode) |
|
| 346 | + { |
|
| 347 | + $this->strictMode = $mode; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Define a generated proxy class path. |
|
| 352 | + * |
|
| 353 | + * @param string $path |
|
| 354 | + */ |
|
| 355 | + public function setProxyPath(string $path) |
|
| 356 | + { |
|
| 357 | + $this->proxyPath = $path; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Return proxy path if defined. |
|
| 362 | + * |
|
| 363 | + * @return string|null |
|
| 364 | + */ |
|
| 365 | + public function getProxyPath() |
|
| 366 | + { |
|
| 367 | + return $this->proxyPath; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Register a namespace in where Analogue |
|
| 372 | + * will scan for EntityMaps & ValueMaps. |
|
| 373 | + * |
|
| 374 | + * @param string $namespace |
|
| 375 | + * |
|
| 376 | + * @return void |
|
| 377 | + */ |
|
| 378 | + public function registerMapNamespace($namespace) |
|
| 379 | + { |
|
| 380 | + // Add a trailing antislash to namespace if not present |
|
| 381 | + if (substr('testers', -1) != '\\') { |
|
| 382 | + $namespace = $namespace.'\\'; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + $this->customMapNamespaces[] = $namespace; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Register an entity. |
|
| 390 | + * |
|
| 391 | + * @param string|\Analogue\ORM\Mappable $entity entity's class name |
|
| 392 | + * @param string|EntityMap $entityMap map's class name |
|
| 393 | + * |
|
| 394 | + * @throws MappingException |
|
| 395 | + * |
|
| 396 | + * @return void |
|
| 397 | + */ |
|
| 398 | + public function register($entity, $entityMap = null) |
|
| 399 | + { |
|
| 400 | + // If an object is provider, get the class name from it |
|
| 401 | + if (!is_string($entity)) { |
|
| 402 | + $entity = get_class($entity); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + if ($this->isRegisteredEntity($entity)) { |
|
| 406 | + throw new MappingException("Entity $entity is already registered."); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + if (!class_exists($entity)) { |
|
| 410 | + throw new MappingException("Class $entity does not exists"); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + if ($entityMap === null) { |
|
| 414 | + $entityMap = $this->getEntityMapInstanceFor($entity); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + if (is_string($entityMap)) { |
|
| 418 | + $entityMap = new $entityMap(); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + if (!$entityMap instanceof EntityMap) { |
|
| 422 | + throw new MappingException(get_class($entityMap).' must be an instance of EntityMap.'); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + $entityMap->setClass($entity); |
|
| 426 | + |
|
| 427 | + $this->entityClasses[$entity] = $entityMap; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * Get the entity map instance for a custom entity. |
|
| 432 | + * |
|
| 433 | + * @param string $entity |
|
| 434 | + * |
|
| 435 | + * @return \Analogue\ORM\EntityMap |
|
| 436 | + */ |
|
| 437 | + protected function getEntityMapInstanceFor($entity) |
|
| 438 | + { |
|
| 439 | + // First, we'll try to load the entity map from cache, to |
|
| 440 | + // save from time consuming parsing of the relationships |
|
| 441 | + $entityMap = $this->getEntityMapInstanceFromCache($entity); |
|
| 442 | + |
|
| 443 | + if ($entityMap !== null) { |
|
| 444 | + return $entityMap; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + if (class_exists($entity.'Map')) { |
|
| 448 | + $map = $entity.'Map'; |
|
| 449 | + $map = new $map(); |
|
| 450 | + |
|
| 451 | + return $map; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + if ($map = $this->getMapFromNamespaces($entity)) { |
|
| 455 | + return $map; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + if ($this->strictMode) { |
|
| 459 | + throw new EntityMapNotFoundException("No Map registered for $entity"); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + $map = $this->getNewEntityMap(); |
|
| 463 | + |
|
| 464 | + return $map; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Get Entity Map instance from cache. |
|
| 469 | + * |
|
| 470 | + * @param string $entityClass |
|
| 471 | + * |
|
| 472 | + * @return EntityMap|null |
|
| 473 | + */ |
|
| 474 | + protected function getEntityMapInstanceFromCache(string $entityClass) |
|
| 475 | + { |
|
| 476 | + if ($this->cache == null) { |
|
| 477 | + return; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + if ($this->cache->has($entityClass)) { |
|
| 481 | + return unserialize($this->cache->get($entityClass)); |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * Scan through registered custom namespace |
|
| 487 | + * for an Entity/ValueMap. |
|
| 488 | + * |
|
| 489 | + * @param string $class |
|
| 490 | + * |
|
| 491 | + * @return ValueMap|EntityMap|bool |
|
| 492 | + */ |
|
| 493 | + protected function getMapFromNamespaces($class) |
|
| 494 | + { |
|
| 495 | + foreach ($this->customMapNamespaces as $namespace) { |
|
| 496 | + if ($map = $this->findMapInNamespace($class, $namespace)) { |
|
| 497 | + return $map; |
|
| 498 | + } |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + return false; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Look in a custom namespace for an Entity/ValueMap. |
|
| 506 | + * |
|
| 507 | + * @param string $class |
|
| 508 | + * @param string $namespace |
|
| 509 | + * |
|
| 510 | + * @return ValueMap|EntityMap|bool |
|
| 511 | + */ |
|
| 512 | + protected function findMapInNamespace($class, $namespace) |
|
| 513 | + { |
|
| 514 | + $parts = explode('\\', $class); |
|
| 515 | + |
|
| 516 | + $baseClass = $parts[count($parts) - 1]; |
|
| 517 | + |
|
| 518 | + $expectedClass = $namespace.$baseClass.'Map'; |
|
| 519 | + |
|
| 520 | + if (class_exists($expectedClass)) { |
|
| 521 | + return new $expectedClass(); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + return false; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * Dynamically create an entity map for a custom entity class. |
|
| 529 | + * |
|
| 530 | + * @return EntityMap |
|
| 531 | + */ |
|
| 532 | + protected function getNewEntityMap() |
|
| 533 | + { |
|
| 534 | + return new EntityMap(); |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * Return the Singleton instance of the manager. |
|
| 539 | + * |
|
| 540 | + * @return Manager |
|
| 541 | + */ |
|
| 542 | + public static function getInstance() |
|
| 543 | + { |
|
| 544 | + return static::$instance; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * Return the Driver Manager's instance. |
|
| 549 | + * |
|
| 550 | + * @return \Analogue\ORM\Drivers\Manager |
|
| 551 | + */ |
|
| 552 | + public function getDriverManager() |
|
| 553 | + { |
|
| 554 | + return $this->drivers; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Get the Repository instance for the given Entity. |
|
| 559 | + * |
|
| 560 | + * @param \Analogue\ORM\Mappable|string $entity |
|
| 561 | + * |
|
| 562 | + * @throws \InvalidArgumentException |
|
| 563 | + * @throws MappingException |
|
| 564 | + * |
|
| 565 | + * @return \Analogue\ORM\Repository |
|
| 566 | + */ |
|
| 567 | + public function repository($entity) |
|
| 568 | + { |
|
| 569 | + if (!is_string($entity)) { |
|
| 570 | + $entity = get_class($entity); |
|
| 571 | + } |
|
| 572 | + |
|
| 573 | + // First we check if the repository is not already created. |
|
| 574 | + if (array_key_exists($entity, $this->repositories)) { |
|
| 575 | + return $this->repositories[$entity]; |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + $this->repositories[$entity] = new Repository($this->mapper($entity)); |
|
| 579 | + |
|
| 580 | + return $this->repositories[$entity]; |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * Return true is the object is registered as value object. |
|
| 585 | + * |
|
| 586 | + * @param mixed $object |
|
| 587 | + * |
|
| 588 | + * @return bool |
|
| 589 | + */ |
|
| 590 | + public function isValueObject($object) |
|
| 591 | + { |
|
| 592 | + if (!is_string($object)) { |
|
| 593 | + $object = get_class($object); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + return array_key_exists($object, $this->valueClasses); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * Get the Value Map for a given Value Object Class. |
|
| 601 | + * |
|
| 602 | + * @param string $valueObject |
|
| 603 | + * |
|
| 604 | + * @throws MappingException |
|
| 605 | + * |
|
| 606 | + * @return \Analogue\ORM\ValueMap |
|
| 607 | + */ |
|
| 608 | + public function getValueMap($valueObject) |
|
| 609 | + { |
|
| 610 | + if (!is_string($valueObject)) { |
|
| 611 | + $valueObject = get_class($valueObject); |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + if (!array_key_exists($valueObject, $this->valueClasses)) { |
|
| 615 | + $this->registerValueObject($valueObject); |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + /** @var ValueMap $valueMap */ |
|
| 619 | + $valueMap = new $this->valueClasses[$valueObject](); |
|
| 620 | + |
|
| 621 | + $valueMap->setClass($valueObject); |
|
| 622 | + |
|
| 623 | + return $valueMap; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * Register a Value Object. |
|
| 628 | + * |
|
| 629 | + * @param string $valueObject |
|
| 630 | + * @param string $valueMap |
|
| 631 | + * |
|
| 632 | + * @throws MappingException |
|
| 633 | + * |
|
| 634 | + * @return void |
|
| 635 | + */ |
|
| 636 | + public function registerValueObject($valueObject, $valueMap = null) |
|
| 637 | + { |
|
| 638 | + if (!is_string($valueObject)) { |
|
| 639 | + $valueObject = get_class($valueObject); |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + if ($valueMap === null) { |
|
| 643 | + |
|
| 644 | + // First, we'll look into registered namespaces for Entity Maps, |
|
| 645 | + // if any. Then we'll fallback to the same namespace of the object |
|
| 646 | + if (!$valueMap = $this->getMapFromNamespaces($valueObject)) { |
|
| 647 | + $valueMap = $valueObject.'Map'; |
|
| 648 | + } else { |
|
| 649 | + $valueMap = get_class($valueMap); |
|
| 650 | + } |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + if (!class_exists($valueMap)) { |
|
| 654 | + throw new MappingException("$valueMap doesn't exists"); |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + $this->valueClasses[$valueObject] = $valueMap; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + /** |
|
| 661 | + * Instantiate a new Value Object instance. |
|
| 662 | + * |
|
| 663 | + * @param string $valueObject |
|
| 664 | + * |
|
| 665 | + * @return \Analogue\ORM\ValueObject |
|
| 666 | + */ |
|
| 667 | + public function getValueObjectInstance($valueObject) |
|
| 668 | + { |
|
| 669 | + $prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($valueObject), $valueObject)); |
|
| 670 | + |
|
| 671 | + return $prototype; |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * Set an application cache. |
|
| 676 | + * |
|
| 677 | + * @param \Psr\SimpleCache\CacheInterface $cache |
|
| 678 | + */ |
|
| 679 | + public function setCache(CacheInterface $cache) |
|
| 680 | + { |
|
| 681 | + $this->cache = $cache; |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + /** |
|
| 685 | + * Register Analogue Plugin. |
|
| 686 | + * |
|
| 687 | + * @param string $plugin class |
|
| 688 | + * |
|
| 689 | + * @return void |
|
| 690 | + */ |
|
| 691 | + public function registerPlugin($plugin) |
|
| 692 | + { |
|
| 693 | + /** @var AnaloguePluginInterface $plugin */ |
|
| 694 | + $plugin = new $plugin($this); |
|
| 695 | + |
|
| 696 | + $this->events = array_merge($this->events, $plugin->getCustomEvents()); |
|
| 697 | + |
|
| 698 | + $plugin->register(); |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + /** |
|
| 702 | + * Register event listeners that will be fired regardless the type |
|
| 703 | + * of the entity. |
|
| 704 | + * |
|
| 705 | + * @param string $event |
|
| 706 | + * @param \Closure $callback |
|
| 707 | + * |
|
| 708 | + * @throws \LogicException |
|
| 709 | + * |
|
| 710 | + * @return void |
|
| 711 | + */ |
|
| 712 | + public function registerGlobalEvent($event, $callback) |
|
| 713 | + { |
|
| 714 | + if (!array_key_exists($event, $this->events)) { |
|
| 715 | + throw new \LogicException("Analogue : Event $event doesn't exist"); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + $this->eventDispatcher->listen("analogue.{$event}.*", $callback); |
|
| 719 | + //$this->eventDispatcher->listen($this->events[$event], $callback); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + /** |
|
| 723 | + * Shortcut to Mapper store. |
|
| 724 | + * |
|
| 725 | + * @param mixed $entity |
|
| 726 | + * |
|
| 727 | + * @throws MappingException |
|
| 728 | + * @throws \InvalidArgumentException |
|
| 729 | + * |
|
| 730 | + * @return mixed |
|
| 731 | + */ |
|
| 732 | + public function store($entity) |
|
| 733 | + { |
|
| 734 | + return $this->mapper($entity)->store($entity); |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + /** |
|
| 738 | + * Shortcut to Mapper delete. |
|
| 739 | + * |
|
| 740 | + * @param mixed $entity |
|
| 741 | + * |
|
| 742 | + * @throws MappingException |
|
| 743 | + * @throws \InvalidArgumentException |
|
| 744 | + * |
|
| 745 | + * @return \Traversable|array |
|
| 746 | + */ |
|
| 747 | + public function delete($entity) |
|
| 748 | + { |
|
| 749 | + return $this->mapper($entity)->delete($entity); |
|
| 750 | + } |
|
| 751 | + |
|
| 752 | + /** |
|
| 753 | + * Shortcut to Mapper query. |
|
| 754 | + * |
|
| 755 | + * @param mixed $entity |
|
| 756 | + * |
|
| 757 | + * @throws MappingException |
|
| 758 | + * @throws \InvalidArgumentException |
|
| 759 | + * |
|
| 760 | + * @return Query |
|
| 761 | + */ |
|
| 762 | + public function query($entity) |
|
| 763 | + { |
|
| 764 | + return $this->mapper($entity)->query(); |
|
| 765 | + } |
|
| 766 | + |
|
| 767 | + /** |
|
| 768 | + * Shortcut to Mapper Global Query. |
|
| 769 | + * |
|
| 770 | + * @param mixed $entity |
|
| 771 | + * |
|
| 772 | + * @throws MappingException |
|
| 773 | + * @throws \InvalidArgumentException |
|
| 774 | + * |
|
| 775 | + * @return Query |
|
| 776 | + */ |
|
| 777 | + public function globalQuery($entity) |
|
| 778 | + { |
|
| 779 | + return $this->mapper($entity)->globalQuery(); |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + /** |
|
| 783 | + * @param array $morphMap |
|
| 784 | + * |
|
| 785 | + * @return $this |
|
| 786 | + */ |
|
| 787 | + public function morphMap(array $morphMap) |
|
| 788 | + { |
|
| 789 | + $this->morphMap = $morphMap; |
|
| 790 | + |
|
| 791 | + return $this; |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + /** |
|
| 795 | + * @param string $class |
|
| 796 | + * |
|
| 797 | + * @return mixed |
|
| 798 | + */ |
|
| 799 | + public function getMorphMap($class) |
|
| 800 | + { |
|
| 801 | + $key = array_search($class, $this->morphMap, false); |
|
| 802 | + |
|
| 803 | + return $key !== false ? $key : $class; |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + /** |
|
| 807 | + * Clear all the cached entities within all mappers. |
|
| 808 | + * |
|
| 809 | + * @return void |
|
| 810 | + */ |
|
| 811 | + public function clearCache() |
|
| 812 | + { |
|
| 813 | + array_map(function ($mapper) { |
|
| 814 | + $mapper->clearCache(); |
|
| 815 | + }, $this->mappers); |
|
| 816 | + } |
|
| 817 | 817 | } |
@@ -810,7 +810,7 @@ |
||
| 810 | 810 | */ |
| 811 | 811 | public function clearCache() |
| 812 | 812 | { |
| 813 | - array_map(function ($mapper) { |
|
| 813 | + array_map(function($mapper) { |
|
| 814 | 814 | $mapper->clearCache(); |
| 815 | 815 | }, $this->mappers); |
| 816 | 816 | } |
@@ -7,28 +7,28 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | trait MagicSetters |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Dynamically set attributes on the entity. |
|
| 12 | - * |
|
| 13 | - * @param string $key |
|
| 14 | - * @param mixed $value |
|
| 15 | - * |
|
| 16 | - * @return void |
|
| 17 | - */ |
|
| 18 | - public function __set($key, $value) |
|
| 19 | - { |
|
| 20 | - $this->attributes[$key] = $value; |
|
| 21 | - } |
|
| 10 | + /** |
|
| 11 | + * Dynamically set attributes on the entity. |
|
| 12 | + * |
|
| 13 | + * @param string $key |
|
| 14 | + * @param mixed $value |
|
| 15 | + * |
|
| 16 | + * @return void |
|
| 17 | + */ |
|
| 18 | + public function __set($key, $value) |
|
| 19 | + { |
|
| 20 | + $this->attributes[$key] = $value; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Unset an attribute on the entity. |
|
| 25 | - * |
|
| 26 | - * @param string $key |
|
| 27 | - * |
|
| 28 | - * @return void |
|
| 29 | - */ |
|
| 30 | - public function __unset($key) |
|
| 31 | - { |
|
| 32 | - unset($this->attributes[$key]); |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * Unset an attribute on the entity. |
|
| 25 | + * |
|
| 26 | + * @param string $key |
|
| 27 | + * |
|
| 28 | + * @return void |
|
| 29 | + */ |
|
| 30 | + public function __unset($key) |
|
| 31 | + { |
|
| 32 | + unset($this->attributes[$key]); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -4,54 +4,54 @@ |
||
| 4 | 4 | |
| 5 | 5 | trait MagicGetters |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Contains the entity's attributes. |
|
| 9 | - * |
|
| 10 | - * @var array |
|
| 11 | - */ |
|
| 12 | - protected $attributes = []; |
|
| 7 | + /** |
|
| 8 | + * Contains the entity's attributes. |
|
| 9 | + * |
|
| 10 | + * @var array |
|
| 11 | + */ |
|
| 12 | + protected $attributes = []; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Dynamically retrieve attributes on the entity. |
|
| 16 | - * |
|
| 17 | - * @param string $key |
|
| 18 | - * |
|
| 19 | - * @return mixed |
|
| 20 | - */ |
|
| 21 | - public function __get($key) |
|
| 22 | - { |
|
| 23 | - // When using mixed mapping, we will check |
|
| 24 | - // for a class property corresponding to |
|
| 25 | - // the attribute's key first. |
|
| 26 | - // |
|
| 27 | - // Note : this may raise issues as we may grant |
|
| 28 | - // access to unwanted properties, like class dependencies. |
|
| 29 | - // |
|
| 30 | - // -> Solution would be to access the entityMap's $attributes, but we |
|
| 31 | - // have to do this in a very efficient way. |
|
| 32 | - // |
|
| 33 | - // Manager::getEntityMap(get_class($this))->hasProperty() |
|
| 34 | - // |
|
| 35 | - // We could do the casting to array / json the same way, and it would |
|
| 14 | + /** |
|
| 15 | + * Dynamically retrieve attributes on the entity. |
|
| 16 | + * |
|
| 17 | + * @param string $key |
|
| 18 | + * |
|
| 19 | + * @return mixed |
|
| 20 | + */ |
|
| 21 | + public function __get($key) |
|
| 22 | + { |
|
| 23 | + // When using mixed mapping, we will check |
|
| 24 | + // for a class property corresponding to |
|
| 25 | + // the attribute's key first. |
|
| 26 | + // |
|
| 27 | + // Note : this may raise issues as we may grant |
|
| 28 | + // access to unwanted properties, like class dependencies. |
|
| 29 | + // |
|
| 30 | + // -> Solution would be to access the entityMap's $attributes, but we |
|
| 31 | + // have to do this in a very efficient way. |
|
| 32 | + // |
|
| 33 | + // Manager::getEntityMap(get_class($this))->hasProperty() |
|
| 34 | + // |
|
| 35 | + // We could do the casting to array / json the same way, and it would |
|
| 36 | 36 | |
| 37 | - if (property_exists($this, $key)) { |
|
| 38 | - return $this->$key; |
|
| 39 | - } |
|
| 37 | + if (property_exists($this, $key)) { |
|
| 38 | + return $this->$key; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - if (array_key_exists($key, $this->attributes)) { |
|
| 42 | - return $this->attributes[$key]; |
|
| 43 | - } |
|
| 44 | - } |
|
| 41 | + if (array_key_exists($key, $this->attributes)) { |
|
| 42 | + return $this->attributes[$key]; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Determine if an attribute exists on the entity. |
|
| 48 | - * |
|
| 49 | - * @param string $key |
|
| 50 | - * |
|
| 51 | - * @return bool |
|
| 52 | - */ |
|
| 53 | - public function __isset($key) |
|
| 54 | - { |
|
| 55 | - return array_key_exists($key, $this->attributes) || property_exists($this, $key); |
|
| 56 | - } |
|
| 46 | + /** |
|
| 47 | + * Determine if an attribute exists on the entity. |
|
| 48 | + * |
|
| 49 | + * @param string $key |
|
| 50 | + * |
|
| 51 | + * @return bool |
|
| 52 | + */ |
|
| 53 | + public function __isset($key) |
|
| 54 | + { |
|
| 55 | + return array_key_exists($key, $this->attributes) || property_exists($this, $key); |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -6,36 +6,36 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Restore extends Command |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @throws \InvalidArgumentException |
|
| 11 | - * |
|
| 12 | - * @return false|mixed |
|
| 13 | - */ |
|
| 14 | - public function execute() |
|
| 15 | - { |
|
| 16 | - $aggregate = $this->aggregate; |
|
| 17 | - $entity = $aggregate->getEntityObject(); |
|
| 18 | - $mapper = $aggregate->getMapper(); |
|
| 19 | - $entityMap = $mapper->getEntityMap(); |
|
| 9 | + /** |
|
| 10 | + * @throws \InvalidArgumentException |
|
| 11 | + * |
|
| 12 | + * @return false|mixed |
|
| 13 | + */ |
|
| 14 | + public function execute() |
|
| 15 | + { |
|
| 16 | + $aggregate = $this->aggregate; |
|
| 17 | + $entity = $aggregate->getEntityObject(); |
|
| 18 | + $mapper = $aggregate->getMapper(); |
|
| 19 | + $entityMap = $mapper->getEntityMap(); |
|
| 20 | 20 | |
| 21 | - if ($mapper->fireEvent('restoring', $entity) === false) { |
|
| 22 | - return false; |
|
| 23 | - } |
|
| 21 | + if ($mapper->fireEvent('restoring', $entity) === false) { |
|
| 22 | + return false; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - $keyName = $entityMap->getKeyName(); |
|
| 25 | + $keyName = $entityMap->getKeyName(); |
|
| 26 | 26 | |
| 27 | - $query = $this->query->where($keyName, '=', $aggregate->getEntityAttribute($keyName)); |
|
| 27 | + $query = $this->query->where($keyName, '=', $aggregate->getEntityAttribute($keyName)); |
|
| 28 | 28 | |
| 29 | - $deletedAtColumn = $entityMap->getQualifiedDeletedAtColumn(); |
|
| 29 | + $deletedAtColumn = $entityMap->getQualifiedDeletedAtColumn(); |
|
| 30 | 30 | |
| 31 | - $query->update([$deletedAtColumn => null]); |
|
| 31 | + $query->update([$deletedAtColumn => null]); |
|
| 32 | 32 | |
| 33 | - $aggregate->setEntityAttribute($deletedAtColumn, null); |
|
| 33 | + $aggregate->setEntityAttribute($deletedAtColumn, null); |
|
| 34 | 34 | |
| 35 | - $mapper->fireEvent('restored', $entity, false); |
|
| 35 | + $mapper->fireEvent('restored', $entity, false); |
|
| 36 | 36 | |
| 37 | - $mapper->getEntityCache()->refresh($aggregate); |
|
| 37 | + $mapper->getEntityCache()->refresh($aggregate); |
|
| 38 | 38 | |
| 39 | - return $entity; |
|
| 40 | - } |
|
| 39 | + return $entity; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -6,36 +6,36 @@ |
||
| 6 | 6 | |
| 7 | 7 | class EntityNotFoundException extends RuntimeException |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Name of the affected Entity Map. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $entity; |
|
| 9 | + /** |
|
| 10 | + * Name of the affected Entity Map. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $entity; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Set the affected Entity Map. |
|
| 18 | - * |
|
| 19 | - * @param string $entity |
|
| 20 | - * |
|
| 21 | - * @return $this |
|
| 22 | - */ |
|
| 23 | - public function setEntity($entity) |
|
| 24 | - { |
|
| 25 | - $this->entity = $entity; |
|
| 16 | + /** |
|
| 17 | + * Set the affected Entity Map. |
|
| 18 | + * |
|
| 19 | + * @param string $entity |
|
| 20 | + * |
|
| 21 | + * @return $this |
|
| 22 | + */ |
|
| 23 | + public function setEntity($entity) |
|
| 24 | + { |
|
| 25 | + $this->entity = $entity; |
|
| 26 | 26 | |
| 27 | - $this->message = "No query results for entity [{$entity}]."; |
|
| 27 | + $this->message = "No query results for entity [{$entity}]."; |
|
| 28 | 28 | |
| 29 | - return $this; |
|
| 30 | - } |
|
| 29 | + return $this; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Get the affected Entity. |
|
| 34 | - * |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - public function getEntity() |
|
| 38 | - { |
|
| 39 | - return $this->entity; |
|
| 40 | - } |
|
| 32 | + /** |
|
| 33 | + * Get the affected Entity. |
|
| 34 | + * |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + public function getEntity() |
|
| 38 | + { |
|
| 39 | + return $this->entity; |
|
| 40 | + } |
|
| 41 | 41 | } |