/** * @return void */ public function registerEvents() { foreach ($this->eventsNames as $eventName) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => $eventName, "actionAnonymousFunction" => function ($arguments) { $arguments["entity"]->setValueForPropertyWithName(new \DateTime(), $arguments["propertyName"]); }, "actionArguments" => ["propertyName" => $this->propertyInformation->name]])); } }
/** * @return void */ public function registerEvents() { if (!$this->registerUuidGenerator) { return; } \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "beforeInsert", "actionAnonymousFunction" => function ($arguments) { $arguments["annotation"]->generateUuid($arguments); }, "actionArguments" => ["propertyName" => $this->propertyInformation->name, "annotation" => $this]])); }
/** * @param array $values * @return void * @throws \obo\Exceptions\BadDataTypeException */ public function process(array $values) { parent::process($values); if (count($values) > 1) { $dataTypeName = \array_shift($values); $dataTypeOptions = $values; } else { $dataTypeName = $values[0]; $dataTypeOptions = []; } $this->propertyInformation->dataType = \obo\Services::serviceWithName(\obo\obo::ENTITIES_EXPLORER)->createDataType($dataTypeName, $this->propertyInformation, $dataTypeOptions); }
/** * @return void */ public function registerEvents() { foreach ($this->cascadeOptions as $cascadeOption) { if ($cascadeOption == "save") { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "afterSave", "actionAnonymousFunction" => function ($arguments) { if ($arguments["entity"]->valueForPropertyWithName($arguments["propertyName"], false, false) instanceof \obo\Relationships\EntitiesCollection) { $arguments["entity"]->valueForPropertyWithName($arguments["propertyName"], false, false)->save(); } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name]])); } elseif ($cascadeOption == "delete") { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "beforeDelete", "actionAnonymousFunction" => function ($arguments) { if ($arguments["entity"]->valueForPropertyWithName($arguments["propertyName"]) instanceof \obo\Relationships\EntitiesCollection) { $arguments["entity"]->valueForPropertyWithName($arguments["propertyName"])->delete($arguments["removeEntity"]); } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name, "removeEntity" => (bool) $this->connectViaProperty]])); } } \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "beforeRead" . \ucfirst($this->propertyInformation->name), "actionAnonymousFunction" => function ($arguments) { if ($arguments["entityAsPrimaryPropertyValue"]) { return; } $propertyInformation = $arguments["entity"]->informationForPropertyWithName($arguments["propertyName"]); $currentPropertyValue = $arguments["entity"]->valueForPropertyWithName($arguments["propertyName"]); if (!$currentPropertyValue instanceof $propertyInformation->relationship->entityClassNameToBeConnected and !$currentPropertyValue instanceof \obo\Relationships\EntitiesCollection) { $arguments["entity"]->setValueForPropertyWithName($propertyInformation->relationship->relationshipForOwnerAndPropertyValue($arguments["entity"], $currentPropertyValue), $arguments["propertyName"], false); } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name]])); }
/** * @return void */ public function registerEvents() { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "beforeRead" . \ucfirst($this->propertyInformation->name), "actionAnonymousFunction" => function ($arguments) { $arguments["annotation"]->fromArray($arguments); }, "actionArguments" => ["propertyName" => $this->propertyInformation->name, "annotation" => $this]])); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "afterChange" . \ucfirst($this->propertyInformation->name), "actionAnonymousFunction" => function ($arguments) { $arguments["annotation"]->toArray($arguments); }, "actionArguments" => ["propertyName" => $this->propertyInformation->name, "annotation" => $this]])); }
/** * @param string $eventName * @param callable $callback */ public final function on($eventName, callable $callback) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onObject" => $this, "name" => $eventName, "actionAnonymousFunction" => $callback])); }
/** * @return void */ public static function run() { self::checkConfiguration(); \obo\Services::registerServiceWithName(new \obo\Services\Events\EventManager(), self::EVENT_MANAGER); \obo\Services::registerServiceWithName(new \obo\Services\EntitiesInformation\Explorer(), self::ENTITIES_EXPLORER); \obo\Annotation\CoreAnnotations::register(\obo\Services::serviceWithName(self::ENTITIES_EXPLORER)); \obo\DataType\CoreDataTypes::register(\obo\Services::serviceWithName(self::ENTITIES_EXPLORER)); \obo\Services::registerServiceWithName(new \obo\Services\EntitiesInformation\Information(self::$modelsDirs, \obo\Services::serviceWithName(self::ENTITIES_EXPLORER), \obo\Services::serviceWithName(self::CACHE)), self::ENTITIES_INFORMATION); \obo\Services::registerServiceWithName(new \obo\Services\IdentityMapper\IdentityMapper(), self::IDENTITY_MAPPER); }
public function add(\obo\Entity $entity) { if ($this->connectViaPropertyWithName !== "") { $entity->setValueForPropertyWithName($this->owner, $this->connectViaPropertyWithName); if ($this->ownerNameInProperty !== "") { $entity->setValueForPropertyWithName($this->owner->entityInformation()->name, $this->ownerNameInProperty); } if ($entity->isBasedInRepository() and $this->owner->isBasedInRepository()) { $entity->save(); } if (!$this->owner->isBasedInRepository()) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onObject" => $this->owner, "name" => "afterInsert", "actionAnonymousFunction" => function () use($entity) { if ($entity->isBasedInRepository()) { $entity->save(); } }, "actionArguments" => []])); } } elseif ($this->connectViaRepositoryWithName !== "") { if ($entity->isBasedInRepository() and $this->owner->isBasedInRepository()) { $this->createRelationshipInRepositoryForEntity($entity); } else { if (!$entity->isBasedInRepository()) { $owner = $this->owner; \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onObject" => $entity, "name" => "afterInsert", "actionAnonymousFunction" => function () use($owner, $entity) { if ($owner->isBasedInRepository()) { $this->createRelationshipInRepositoryForEntity($entity); } }, "actionArguments" => []])); } if (!$this->owner->isBasedInRepository()) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onObject" => $this->owner, "name" => "afterInsert", "actionAnonymousFunction" => function () use($entity) { if ($entity->isBasedInRepository()) { $this->createRelationshipInRepositoryForEntity($entity); } }, "actionArguments" => []])); } } } else { throw new \obo\Exceptions\Exception("This relationship is not well configured"); } }
/** * @return void */ public function clear() { foreach ($entities =& parent::variables() as $key => $entity) { unset($entities[$key]); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity($this->relationShip->ownerPropertyName . "Disconnected", $this->owner, ["collection" => $this, "owner" => $this->owner, "columnName" => $this->relationShip->ownerPropertyName, "disconnectedEntity" => $entity]); } }
/** * @return array */ public function registerEvents() { foreach ($this->cascadeOptions as $cascadeOption) { if ($cascadeOption == "save") { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => $this->connectViaProperty ? "afterSave" : "beforeSave", "actionAnonymousFunction" => function ($arguments) { $connectedEntity = $arguments["entity"]->valueForPropertyWithName($arguments["propertyName"], false, true, false); if ($connectedEntity instanceof \obo\Entity && !$connectedEntity->isDeleted()) { $connectedEntity->save(); } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name]])); } elseif ($cascadeOption == "delete") { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "beforeDelete", "actionAnonymousFunction" => function ($arguments) { if ($arguments["entity"]->valueForPropertyWithName($arguments["propertyName"], false, true, false) !== null) { if (($connectedEntity = $arguments["entity"]->valueForPropertyWithName($arguments["propertyName"])) instanceof \obo\Entity) { $connectedEntity->delete($arguments["removeEntity"]); } } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name, "removeEntity" => true]])); } } \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "beforeRead" . \ucfirst($this->propertyInformation->name), "actionAnonymousFunction" => function ($arguments) { if ($arguments["entityAsPrimaryPropertyValue"] and $this->connectViaProperty === null) { return; } $propertyInformation = $arguments["entity"]->informationForPropertyWithName($arguments["propertyName"]); $currentPropertyValue = $arguments["entity"]->valueForPropertyWithName($arguments["propertyName"]); if (!$currentPropertyValue instanceof \obo\Entity) { if ($this->connectViaProperty === null) { $entityToBeConnected = $propertyInformation->relationship->relationshipForOwnerAndPropertyValue($arguments["entity"], $currentPropertyValue, $arguments["autoCreate"]); } else { $foreignKey = [$this->connectViaProperty]; if ($this->ownerNameInProperty !== null) { $foreignKey[] = $this->ownerNameInProperty; } $entityToBeConnected = $propertyInformation->relationship->entityForOwnerForeignKey($arguments["entity"], $foreignKey, $arguments["autoCreate"]); } if ($entityToBeConnected === null) { $arguments["entity"]->setValueForPropertyWithName(null, $arguments["propertyName"]); } else { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeConnectToOwner", $entityToBeConnected, ["owner" => $arguments["entity"], "columnName" => $propertyInformation->columnName]); $arguments["entity"]->setValueForPropertyWithName($entityToBeConnected, $arguments["propertyName"], !$entityToBeConnected->isBasedInRepository()); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterConnectToOwner", $entityToBeConnected, ["owner" => $arguments["entity"], "columnName" => $propertyInformation->columnName]); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity($this->propertyInformation->name . "Connected", $arguments["entity"], ["columnName" => $propertyInformation->columnName, "connectedEntity" => $entityToBeConnected]); } } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name]])); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "beforeChange" . \ucfirst($this->propertyInformation->name), "actionAnonymousFunction" => function ($arguments) { $propertyInformation = $arguments["entity"]->informationForPropertyWithName($arguments["propertyName"]); if ($arguments["propertyValue"]["new"] instanceof \obo\Entity) { if ($arguments["propertyValue"]["old"] instanceof \obo\Entity) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeDisconnectFromOwner", $arguments["propertyValue"]["old"], ["owner" => $arguments["entity"], "columnName" => $propertyInformation->columnName]); } \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeConnectToOwner", $arguments["propertyValue"]["new"], ["owner" => $arguments["entity"], "columnName" => $propertyInformation->columnName]); } else { if ($arguments["propertyValue"]["old"] instanceof \obo\Entity) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeDisconnectFromOwner", $arguments["propertyValue"]["old"], ["owner" => $arguments["entity"], "columnName" => $propertyInformation->columnName]); } } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name]])); if ($this->targetEntityInProperty) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "beforeWrite" . \ucfirst($this->propertyInformation->name), "actionAnonymousFunction" => function ($arguments) { if (\is_string($arguments["propertyValue"]["new"]) and \count($parts = explode(":", $arguments["propertyValue"]["new"])) === 2) { if (!\class_exists($parts[1])) { throw new \obo\Exceptions\Exception("Can not set property with dynamic relationship 'one', class '{$parts[1]}' does not exist"); } $arguments["propertyValue"]["new"] = $parts[0]; $propertyInformation = $arguments["entity"]->informationForPropertyWithName($this->propertyInformation->name); $arguments["entity"]->setValueForPropertyWithName(ltrim($parts[1], "\\"), $propertyInformation->relationship->entityClassNameToBeConnectedInPropertyWithName); } }])); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "afterChange" . \ucfirst($this->propertyInformation->name), "actionAnonymousFunction" => function ($arguments) { $propertyInformation = $arguments["entity"]->informationForPropertyWithName($arguments["propertyName"]); if ($arguments["propertyValue"]["new"] instanceof \obo\Entity) { $arguments["entity"]->setValueForPropertyWithName($arguments["propertyValue"]["new"]->className(), $propertyInformation->relationship->entityClassNameToBeConnectedInPropertyWithName); } elseif ($arguments["propertyValue"]["new"] === null) { $arguments["entity"]->setValueForPropertyWithName(null, $propertyInformation->relationship->entityClassNameToBeConnectedInPropertyWithName); } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name]])); } \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "afterChange" . \ucfirst($this->propertyInformation->name), "actionAnonymousFunction" => function ($arguments) { $propertyInformation = $arguments["entity"]->informationForPropertyWithName($arguments["propertyName"]); if ($arguments["propertyValue"]["new"] instanceof \obo\Entity) { if ($arguments["propertyValue"]["old"] instanceof \obo\Entity) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterDisconnectFromOwner", $arguments["propertyValue"]["old"], ["owner" => $arguments["entity"], "columnName" => $propertyInformation->columnName]); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity($this->propertyInformation->name . "Disconnected", $arguments["entity"], ["columnName" => $propertyInformation->columnName, "disconnectedEntity" => $arguments["propertyValue"]["old"]]); } \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterConnectToOwner", $arguments["propertyValue"]["new"], ["owner" => $arguments["entity"], "columnName" => $propertyInformation->columnName]); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity($this->propertyInformation->name . "Connected", $arguments["entity"], ["columnName" => $propertyInformation->columnName, "connectedEntity" => $arguments["propertyValue"]["new"]]); } else { if ($arguments["propertyValue"]["old"] instanceof \obo\Entity) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterDisconnectFromOwner", $arguments["propertyValue"]["old"], ["owner" => $arguments["entity"], "columnName" => $propertyInformation->columnName]); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity($this->propertyInformation->name . "Disconnected", $arguments["entity"], ["columnName" => $propertyInformation->columnName, "disconnectedEntity" => $arguments["propertyValue"]["old"]]); } } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name]])); if ($this->connectViaProperty !== null) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "afterConnectToOwner", "actionAnonymousFunction" => function ($arguments) { $propertyInformation = $arguments["entity"]->informationForPropertyWithName($arguments["propertyName"]); if ($arguments["owner"] instanceof $propertyInformation->relationship->entityClassNameToBeConnected && $arguments["columnName"] === $propertyInformation->relationship->connectViaProperty) { $arguments["entity"]->setValueForPropertyWithName($arguments["owner"], $propertyInformation->relationship->ownerPropertyName); } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name]])); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => "afterDisconnectFromOwner", "actionAnonymousFunction" => function ($arguments) { $propertyInformation = $arguments["entity"]->informationForPropertyWithName($arguments["propertyName"]); if ($arguments["owner"] instanceof $propertyInformation->relationship->entityClassNameToBeConnected && $arguments["columnName"] === $propertyInformation->relationship->connectViaProperty) { $arguments["entity"]->setValueForPropertyWithName(null, $propertyInformation->relationship->ownerPropertyName); } }, "actionArguments" => ["propertyName" => $this->propertyInformation->name]])); } }
/** * @param \obo\Entity $entity * @param bool $triggerEvents * @return void * @throws \obo\Exceptions\EntityIsNotInitializedException */ public static function deleteEntity(\obo\Entity $entity, $triggerEvents = true) { if (!$entity->isInitialized()) { throw new \obo\Exceptions\EntityIsNotInitializedException("Cannot delete entity which is not initialized"); } $entity->setDeletingInProgress(); if ($triggerEvents) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("beforeDelete", $entity); } if (($propertyNameForSoftDelete = $entity->entityInformation()->propertyNameForSoftDelete) === "") { $entity->dataStorage()->removeEntity($entity); } else { $entity->discardNonPersistedChanges(); $entity->setValueForPropertyWithName(true, $propertyNameForSoftDelete); self::saveEntity($entity, true, false); } $entity->setDeleted(true); if ($triggerEvents) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->notifyEventForEntity("afterDelete", $entity); } $entity->setDeletingInProgress(false); }
/** * @param \obo\Carriers\EntityInformationCarrier $entityInformation */ private function registerRunTimeEventsForEntity(\obo\Carriers\EntityInformationCarrier $entityInformation) { foreach ($entityInformation->annotations as $annotation) { $annotation->registerEvents(); } foreach ($entityInformation->propertiesInformation as $propertyInformation) { foreach ($propertyInformation->annotations as $annotation) { $annotation->registerEvents(); } if ($propertyInformation->dataType instanceof \obo\DataType\Base\DataType) { $propertyInformation->dataType->registerEvents(); } } \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $entityInformation->className, "name" => "beforeChange" . \ucfirst($entityInformation->primaryPropertyName), "actionAnonymousFunction" => function ($arguments) { if ($arguments["entity"]->isInitialized()) { $backTrace = \debug_backtrace(); if ($backTrace[4]["function"] !== "insertEntity") { throw new \obo\Exceptions\PropertyAccessException("Primary entity property can't be changed, has been marked as initialized"); } } }])); \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $entityInformation->className, "name" => "beforeChange", "actionAnonymousFunction" => function ($arguments) { if ($arguments["entity"]->isDeleted() and !$arguments["entity"]->isDeletingInProgress()) { throw new \obo\Exceptions\PropertyAccessException("Property '{$arguments["propertyName"]}' can't be changed, entity '{$arguments["entity"]->entityInformation()->className}' is deleted"); } }])); }
/** * @return void */ public function registerEvents() { foreach ($this->eventsNames as $eventName) { \obo\Services::serviceWithName(\obo\obo::EVENT_MANAGER)->registerEvent(new \obo\Services\Events\Event(["onClassWithName" => $this->entityInformation->className, "name" => $eventName, "actionMessage" => $this->methodName])); } }