{"id":26905143,"url":"https://github.com/jimdo/jimphle-messaging","last_synced_at":"2025-04-01T10:56:13.784Z","repository":{"id":14180196,"uuid":"16886404","full_name":"Jimdo/jimphle-messaging","owner":"Jimdo","description":"Jimdo PHP library extraction of messaging component","archived":false,"fork":false,"pushed_at":"2022-03-29T06:33:46.000Z","size":104,"stargazers_count":5,"open_issues_count":1,"forks_count":2,"subscribers_count":115,"default_branch":"master","last_synced_at":"2025-01-18T22:41:22.013Z","etag":null,"topics":["owner-team-creator"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jimdo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-02-16T14:35:01.000Z","updated_at":"2022-03-29T06:33:48.000Z","dependencies_parsed_at":"2022-07-10T05:00:38.714Z","dependency_job_id":null,"html_url":"https://github.com/Jimdo/jimphle-messaging","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jimdo%2Fjimphle-messaging","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jimdo%2Fjimphle-messaging/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jimdo%2Fjimphle-messaging/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jimdo%2Fjimphle-messaging/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jimdo","download_url":"https://codeload.github.com/Jimdo/jimphle-messaging/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246628272,"owners_count":20808106,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["owner-team-creator"],"created_at":"2025-04-01T10:56:13.198Z","updated_at":"2025-04-01T10:56:13.774Z","avatar_url":"https://github.com/Jimdo.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Jimphle-messaging\n\nJimdo PHP library extraction of messaging component.\n\nThe Messaging component is base on message-handler which implement the MessageHandler Interface.\n\n## Handling commands and events\n\nLet's get started with sending a command say-hello:\n```php\nuse Jimphle\\Example\\MessageHandler\\SayHelloHandler;\n\nuse Jimphle\\Messaging\\MessageHandler\\HandleMessage;\nuse Jimphle\\Messaging\\MessageHandlerProvider;\nuse Jimphle\\Messaging\\Command;\n\n$messageHandlerDefinitions = array(\n    'say-hello' =\u003e new SayHelloHandler(),\n);\n$messagingContext = new HandleMessage(\n    new MessageHandlerProvider(\n        new ArrayObject(\n            $messageHandlerDefinitions\n        )\n    )\n);\n\n$response = $messagingContext-\u003ehandle(\n    Command::generate(\n        'say-hello',\n        array(\n            'name' =\u003e 'World'\n        )\n    )\n);\necho $response-\u003eanswer;\n\n# =\u003e Hello World!\n```\nOk what happened here? We passed the Command say-hello and it's payload to the messagingContext.\nThe messagingContext did a lookup for A MessageHandler registered to say-hello.\n\nSomething more interesting. If you had a look at the SayHelloHandler you would have seen that the handler itself\ngenerated a new Event which is returned with the MessageResponse \"to-be-processed-directly\" by the calling context.\nTo make our messagingContext to be able to handle this kind of messages we have to add another kind of handler.\n```php\nuse Jimphle\\Example\\MessageHandler\\BeObsceneHandler;\nuse Jimphle\\Example\\MessageHandler\\SayHelloHandler;\n\nuse Jimphle\\Messaging\\MessageHandler\\HandleMessagesToProcessDirectly;\nuse Jimphle\\Messaging\\MessageHandler\\HandleMessage;\nuse Jimphle\\Messaging\\MessageHandlerProvider;\nuse Jimphle\\Messaging\\Command;\n\n$messageHandlerDefinitions = array(\n    'say-hello' =\u003e new SayHelloHandler(),\n    'said-hello' =\u003e array(\n        new BeObsceneHandler()\n    )\n);\n$messagingContext = new HandleMessagesToProcessDirectly(\n    new HandleMessage(\n        new MessageHandlerProvider(\n            new ArrayObject(\n                $messageHandlerDefinitions\n            )\n        )\n    )\n);\n\n$response = $messagingContext-\u003ehandle(\n    Command::generate(\n        'say-hello',\n        array(\n            'name' =\u003e 'World'\n        )\n    )\n);\necho $response-\u003eanswer . \"\\n\";\n\n# =\u003e GTFO Joscha!\n# =\u003e Hello World!\n```\nAs you can see Our said-hello Event is handled by the messagingContext now.\nWhich means the messagingContext handles the SayHelloHandler iterates over the returned \"to-be-processed-directly\"\nevents and tries to handle them as well.\nHow you may have noticed the order of the printed messages is reversed to the order the printing handlers are called.\nThis happens because the response of the first called message-handler is the one which is returned here.\n\n## Message-filter and message-handler annotations\n\nIf you add the ApplyFilter MessageHandler to the messagingContext you add the possibility to apply filter to a message\nbefore passing it to the next message-handler.\n```php\n$messageFilterDefinitions = array(new SomeMessageFilter());\n$messagingContext = new HandleMessagesToProcessDirectly(\n    new ApplyFilter(\n        $messageFilterDefinitions,\n        new HandleMessage(\n            new MessageHandlerProvider(\n                new ArrayObject(\n                    $messageHandlerDefinitions\n                )\n            )\n        )\n    )\n);\n```\n\nHowever there are two predefined filter we can use here:\n * The Validation filter, based on Symfony-Validation-Component\n * The Authorization filter explained here another day\n\n## Handling messages in a PDO transaction\n\nTo handle the messages in a PDO transaction we can add the TransactionalMessageHandler to the messagingContext.\n```php\n$messagingContext = new HandleMessagesToProcessDirectly(\n    new TransactionalMessageHandler(\n        new PDO('some-dsn'),\n        new ApplyFilter(\n            $messageFilterDefinitions,\n            new HandleMessage(\n                new MessageHandlerProvider(\n                    new ArrayObject(\n                        $messageHandlerDefinitions\n                    )\n                )\n            )\n        )\n    )\n);\n```\nTo execute a message handler in a pdo transaction you just have to add an annotation.\n```php\nuse Jimphle\\Messaging\\Plugin\\Pdo\\TransactionalAnnotation as withPdoTransaction;\n\n/**\n * @withPdoTransaction\n */\nclass SayHelloHandler extends AbstractMessageHandler\n{\n    public function handle(Message $message)\n    {\n        return $this-\u003eresponse(\n            array('answer' =\u003e sprintf(\"Hello %s!\", $message-\u003ename))\n        )\n            -\u003eaddMessageToProcessDirectly(\n                $this-\u003eevent('said-hello', array('name' =\u003e 'Joscha'))\n            );\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimdo%2Fjimphle-messaging","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjimdo%2Fjimphle-messaging","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimdo%2Fjimphle-messaging/lists"}