| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license http://www.gnu.org/licenses/gpl.html GNU General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use midgard\portable\storage\connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use midgard\portable\storage\subscriber; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use midgard\portable\api\error\exception; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use midgard\portable\storage\objectmanager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use midgard\portable\storage\interfaces\metadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use midgard\portable\api\dbobject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class midgard_object_class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 10 |  |     private static function resolve_classname(string $guid, bool $include_deleted = false) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 10 |  |         $qb = connection::get_em()->createQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 10 |  |         $qb->from(connection::get_fqcn('midgard_repligard'), 'r') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 10 |  |             ->select('r.typename, r.object_action') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 10 |  |             ->where('r.guid = ?1') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 10 |  |             ->setParameter(1, $guid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 10 |  |             $result = $qb->getQuery()->getSingleResult(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 3 |  |         } catch (\Doctrine\ORM\NoResultException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 3 |  |             throw exception::not_exists(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 10 |  |         if ($result["object_action"] == subscriber::ACTION_PURGE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  |             throw exception::object_purged(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 9 |  |         if (!$include_deleted && $result["object_action"] == subscriber::ACTION_DELETE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |             throw exception::object_deleted(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 8 |  |         if ($include_deleted && !self::has_metadata($result["typename"])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |             throw exception::invalid_property_value(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 7 |  |         return $result["typename"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 8 |  |     public static function factory(?string $classname, $id = null) : ?dbobject | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 8 |  |         if ($classname === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 8 |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $classname = connection::get_fqcn($classname); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 5 |  |         return new $classname($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 5 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public static function undelete(string $guid) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 5 |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 5 |  |             $classname = self::resolve_classname($guid, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } catch (exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 4 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $classname = connection::get_fqcn($classname); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |         $qb = new \midgard_query_builder($classname); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |         $qb->include_deleted(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |         $qb->add_constraint('guid', '=', $guid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $results = $qb->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  |         if (empty($results)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             exception::not_exists(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  |         $entity = $results[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 3 |  |         if (!$entity->metadata_deleted) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             exception::internal(new \Exception("Object is not deleted.")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |             $om = new objectmanager(connection::get_em()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             $om->undelete($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             exception::internal($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |         midgard_connection::get_instance()->set_error(MGD_ERR_OK); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public static function get_object_by_guid(string $guid) : dbobject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |         if (!mgd_is_guid($guid)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  |             throw exception::not_exists(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 7 |  |         $type = self::resolve_classname($guid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return self::factory($type, $guid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public static function get_property_up(string $classname) : ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 6 |  |         $cm = connection::get_em()->getClassMetadata($classname); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 4 |  |         return $cm->midgard['upfield']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |     public static function get_property_parent(string $classname) : ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |         $cm = connection::get_em()->getClassMetadata($classname); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |         return $cm->midgard['parentfield']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |     public static function has_metadata($classname) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |         if (is_string($classname)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |             return in_array(metadata::class, class_implements($classname)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         if (is_object($classname)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 7 |  |             return $classname instanceof metadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 7 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 7 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  | 
            
                        
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: