{"id":13792274,"url":"https://github.com/dddshelf/ddd","last_synced_at":"2026-01-11T16:52:32.216Z","repository":{"id":20954428,"uuid":"24243031","full_name":"dddshelf/ddd","owner":"dddshelf","description":"Domain Driven Design PHP helper classes","archived":false,"fork":false,"pushed_at":"2020-10-07T08:12:22.000Z","size":51,"stargazers_count":661,"open_issues_count":8,"forks_count":107,"subscribers_count":60,"default_branch":"master","last_synced_at":"2024-10-29T23:19:22.288Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://leanpub.com/ddd-in-php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dddshelf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-09-19T19:41:05.000Z","updated_at":"2024-10-22T09:01:05.000Z","dependencies_parsed_at":"2022-07-18T22:01:19.158Z","dependency_job_id":null,"html_url":"https://github.com/dddshelf/ddd","commit_stats":null,"previous_names":["dddinphp/ddd"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dddshelf%2Fddd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dddshelf%2Fddd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dddshelf%2Fddd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dddshelf%2Fddd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dddshelf","download_url":"https://codeload.github.com/dddshelf/ddd/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253754967,"owners_count":21958934,"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":[],"created_at":"2024-08-03T22:01:10.498Z","updated_at":"2026-01-11T16:52:32.207Z","avatar_url":"https://github.com/dddshelf.png","language":"PHP","readme":"carlosbuenosvinos/ddd\n=====================\n\n[![Build Status](https://secure.travis-ci.org/dddinphp/ddd.svg?branch=master)](http://travis-ci.org/dddinphp/ddd)\n\nThis library will help you with typical DDD scenarios, for now:\n* Application Services Interface\n* Transactional Application Services with Doctrine and ADODb\n* Data Transformers Interface\n* No Transformer Data Transformers\n* Domain Event Interface\n* Event Store Interface\n* Event Store Doctrine Implementation\n* Domain Event Publishing Service\n* Messaging Producer Interface\n* Messaging Producer RabbitMQ Implementation\n\n# Sample Projects\n\nThere are some projects developed using carlosbuenosvinos/ddd library. Check some of them to see how to use it:\n* [Last Wishes](https://github.com/dddinphp/last-wishes): Actions to run, such as tweet, send emails, etc. in case anything happen to you.\n\n# Application Services\n\n## Application Service Interface\n\nConsider an Application Service that registers a new user in your application. \n\n    $signInUserService = new SignInUserService(\n        $em-\u003egetRepository('MyBC\\Domain\\Model\\User\\User')\n    );\n    \n    $response = $signInUserService-\u003eexecute(\n        new SignInUserRequest(\n            'carlos.buenosvinos@gmail.com',\n            'thisisnotasecretpassword'\n        )\n    );\n\n    $newUserCreated = $response-\u003egetUser();\n    //...\n\nWe need to pass in the constructor all the dependencies. In this case, the User repository. As DDD explains, the Doctrine repository is implementing a generic interface for User repositories.\n\n    \u003c?php\n    \n    namespace MyBC\\Application\\Service\\User;\n    \n    use MyBC\\Domain\\Model\\User\\User;\n    use MyBC\\Domain\\Model\\User\\UserAlreadyExistsException;\n    use MyBC\\Domain\\Model\\User\\UserRepository;\n    \n    use Ddd\\Application\\Service\\ApplicationService;\n    \n    /**\n     * Class SignInUserService\n     * @package MyBC\\Application\\Service\\User\n     */\n    class SignInUserService implements ApplicationService\n    {\n        /**\n         * @var UserRepository\n         */\n        private $userRepository;\n    \n        /**\n         * @param UserRepository $userRepository\n         */\n        public function __construct(UserRepository $userRepository)\n        {\n            $this-\u003euserRepository = $userRepository;\n        }\n    \n        /**\n         * @param SignInUserRequest $request\n         * @return SignInUserResponse\n         * @throws UserAlreadyExistsException\n         */\n        public function execute($request = null)\n        {\n            $email = $request-\u003eemail();\n            $password = $request-\u003epassword();\n    \n            $user = $this-\u003euserRepository-\u003euserOfEmail($email);\n            if (null !== $user) {\n                throw new UserAlreadyExistsException();\n            }\n    \n            $user = new User(\n                $this-\u003euserRepository-\u003enextIdentity(),\n                $email,\n                $password\n            );\n    \n            $this-\u003euserRepository-\u003epersist($user);\n    \n            return new SignInUserResponse($user);\n        }\n    }\n\nI suggest to make your Application Services implement the following interface following the command pattern.\n\n    /**\n     * Interface ApplicationService\n     * @package Ddd\\Application\\Service\n     */\n    interface ApplicationService\n    {\n        /**\n         * @param $request\n         * @return mixed\n         */\n        public function execute($request = null);\n    }\n\n## Transactions\n\nApplication Services should manage transactions when dealing with database persistence strategies. In order to manage it cleanly, I provide an Application Service decorator that wraps an Application Service an executes it inside a transactional boundary.\n\nThe decorator is the ```Ddd\\Application\\Service\\TransactionalApplicationService``` class. In order to create one, you need the non transactional Application Service and a Transactional Session. We provide different types of Transactional Sessions. See how to do it with Doctrine.\n\n### Doctrine Transactional Application Services\n\nFor the Doctrine Transactional Session, pass the EntityManager instance.\n\n    /** @var EntityManager $em */\n    $txSignInUserService = new TransactionalApplicationService(\n        new SignInUserService(\n            $em-\u003egetRepository('MyBC\\Domain\\Model\\User\\User')\n        ),\n        new DoctrineSession($em)\n    );\n    \n    $response = $txSignInUserService-\u003eexecute(\n        new SignInUserRequest(\n            'carlos.buenosvinos@gmail.com',\n            'thisisnotasecretpassword'\n        )\n    );\n    \n    $newUserCreated = $response-\u003egetUser();\n    //...\n\nAs you can see, the use case creation and execution is the same as the non transactional, the only difference is the decoration with the Transactional Application Service.\n\nAs a collateral benefit, the Doctrine Session manages internally the ```flush``` method, so you don't need to add a ```flush``` in your Domain neither your infrastructure.\n\n## Asynchronous AMQP listeners\n\nThis library is capable to support asynchronous messaging in order to make Bounded Context capable to listen to other Bounded Context's events in an efficient way. The base for this is the both the **[amqp](https://pecl.php.net/package/amqp)** and the **[react's event loop](https://github.com/reactphp/event-loop)**. In addition, to support more efficient event loopings, we recommend the installation of one of this extensions\n\n* **[libevent](http://php.net/manual/en/book.libevent.php)**\n* **[libev](http://php.net/manual/en/intro.ev.php)**\n* **[event](http://php.net/manual/en/book.event.php)**\n\nThe usage of any of this extensions is handled by ReactPHP's *event-loop* in a totally transparent way.\n\n### Example\n\nSupose we need to listen to the ```Acme\\Billing\\DomainModel\\Order\\OrderWasCreated``` event triggered via messaging from  another bounded context. The following, is an example of a AMQP exchange listener that listents to the ```Acme\\Billing\\DomainModel\\Order\\OrderWasCreated``` event.\n\n```php\n\u003c?php\n\nnamespace Acme\\Inventory\\Infrastructure\\Messaging\\Amqp;\n\nuse stdClass;\nuse AMQPQueue;\nuse JMS\\Serializer\\Serializer;\nuse League\\Tactician\\CommandBus;\nuse React\\EventLoop\\LoopInterface;\nuse Ddd\\Infrastructure\\Application\\Notification\\AmqpExchangeListener;\n\nclass OrderWasCreatedListener extends AmqpExchangeListener\n{\n    private $commandBus;\n    \n    public function __construct(AMQPQueue $queue, LoopInterface $loop, Serializer $serializer, CommandBus $commandBus)\n    {\n        $this-\u003ecommandBus = $commandBus;\n        \n        parent::construct($queue, $loop, $serializer);\n    }\n    \n    /**\n     * This method will be responsible to decide whether this listener listens to an specific\n     * event type or not, given an event type name\n     *\n     * @param string $typeName\n     *\n     * @return bool\n     */\n    protected function listensTo($typeName)\n    {\n        return 'Acme\\Billing\\DomainModel\\Order\\OrderWasCreated' === $typeName;\n    }\n\n    /**\n     * The action to perform\n     *\n     * @param stdClass $event\n     *\n     * @return void\n     */\n    protected function handle($event)\n    {\n        $this-\u003ecommandBus-\u003ehandle(new CreateOrder(\n            $event-\u003eorder_id,\n            // ...\n        ));\n    }\n}\n```\n\nAnd this is a possible command to create AMQP workers\n\n```php\n\u003c?php\n\nnamespace AppBundle\\Command;\n\nuse AMQPConnection;\nuse AMQPChannel;\nuse React\\EventLoop\\Factory;\nuse Symfony\\Component\\Console\\Command\\Command;\nuse Symfony\\Component\\Console\\Input\\InputArgument;\nuse Symfony\\Component\\Console\\Input\\InputInterface;\nuse Symfony\\Component\\Console\\Input\\InputOption;\nuse Symfony\\Component\\Console\\Output\\OutputInterface;\nuse JMS\\Serializer\\Serializer;\nuse League\\Tactician\\CommandBus;\n\nclass OrderWasCreatedWorkerCommand extends Command\n{\n    private $serializer;\n    private $commandBus;\n    \n    public function __construct(Serializer $serializer, CommandBus $commandBus)\n    {\n        $this-\u003eserializer = $serializer;\n        $this-\u003ecommandBus = $commandBus;\n        \n        parent::__construct();\n    }\n    \n    public function execute(InputInterface $input, OutputInterface $output)\n    {\n        $connection = new AMQPConnection([\n            'host' =\u003e 'example.host',\n            'vhost' =\u003e '/',\n            'port' =\u003e 5763,\n            'login' =\u003e 'user',\n            'password' =\u003e 'password'\n        ]);\n        $connection-\u003econnect();\n        \n        $queue = new AMQPQueue(new AMQPChannel($connection));\n        $queue-\u003esetName('events');\n        $queue-\u003esetFlags(AMQP_NOPARAM);\n        $queue-\u003edeclareQueue();\n        \n        $loop = Factory::create();\n        \n        $listener = new OrderWasCreatedListener(\n            $queue,\n            $loop,\n            $serializer,\n            $this-\u003ecommandBus\n        );\n        \n        $loop-\u003erun();\n    }\n}\n```\n\n## AMQP Message producer\n\nThe intention of the AMQP message producer is to be composed in some other class. The following is an example of the usage of the AMQP message producer.\n\n```php\n\u003c?php\n\nuse Doctrine\\ORM\\Tools\\Setup;\nuse Doctrine\\ORM\\EntityManager;\nuse Ddd\\Infrastructure\\Application\\Notification\\AmqpMessageProducer;\nuse Ddd\\Application\\Notification\\NotificationService;\n\n$connection = new AMQPConnection([\n    'host' =\u003e 'example.host',\n    'vhost' =\u003e '/',\n    'port' =\u003e 5763,\n    'login' =\u003e 'user',\n    'password' =\u003e 'password'\n]);\n$connection-\u003econnect();\n\n$exchange = new AMQPExchange(new AMQPChannel($connection));\n$exchange-\u003esetName('events');\n$exchange-\u003edeclare();\n\n$config = Setup::createYAMLMetadataConfiguration([__DIR__.\"/src/Infrastructure/Application/Persistence/Doctrine/Config\"], false);\n$entityManager = EntityManager::create(['driver' =\u003e 'pdo_sqlite', 'path' =\u003e __DIR__ . '/db.sqlite'], $config);\n\n$eventStore = $entityManager-\u003egetRepository('Ddd\\Domain\\Event\\StoredEvent');\n$publishedMessageTracker = $entityManager-\u003egetRepository('Ddd\\Domain\\Event\\PublishedMessage');\n$messageProducer = new AmqpMessageProducer($exchange);\n\n$notificationService = new NotificationService(\n    $eventStore,\n    $publishedMessageTracker,\n    $messageProducer\n);\n\n$notificationService-\u003epublish(/** ... **/);\n```\n","funding_links":[],"categories":["基础框架"],"sub_categories":["构建/部署"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdddshelf%2Fddd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdddshelf%2Fddd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdddshelf%2Fddd/lists"}