{"id":23036869,"url":"https://github.com/degraciamathieu/manager","last_synced_at":"2025-04-09T16:17:57.519Z","repository":{"id":48505882,"uuid":"243811651","full_name":"DeGraciaMathieu/Manager","owner":"DeGraciaMathieu","description":"Implementation of the Manager pattern existing in Laravel framework","archived":false,"fork":false,"pushed_at":"2023-12-27T07:43:20.000Z","size":100,"stargazers_count":90,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T16:17:51.804Z","etag":null,"topics":["design-patterns","laravel","manager","package","php"],"latest_commit_sha":null,"homepage":"","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/DeGraciaMathieu.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-28T16:54:28.000Z","updated_at":"2024-12-20T15:30:50.000Z","dependencies_parsed_at":"2023-12-27T08:32:40.056Z","dependency_job_id":"ae79711c-58ca-4b6a-8ab9-2934c29446fd","html_url":"https://github.com/DeGraciaMathieu/Manager","commit_stats":{"total_commits":45,"total_committers":4,"mean_commits":11.25,"dds":0.3111111111111111,"last_synced_commit":"29211219b5974acfe4775b952898466e6681a59d"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeGraciaMathieu%2FManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeGraciaMathieu%2FManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeGraciaMathieu%2FManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DeGraciaMathieu%2FManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DeGraciaMathieu","download_url":"https://codeload.github.com/DeGraciaMathieu/Manager/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065281,"owners_count":21041872,"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":["design-patterns","laravel","manager","package","php"],"created_at":"2024-12-15T17:27:57.721Z","updated_at":"2025-04-09T16:17:57.500Z","avatar_url":"https://github.com/DeGraciaMathieu.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003ca href=\"https://scrutinizer-ci.com/g/DeGraciaMathieu/Manager/\"\u003e\u003cimg src=\"https://scrutinizer-ci.com/g/DeGraciaMathieu/Manager/badges/build.png?b=master\" alt=\"Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://scrutinizer-ci.com/g/DeGraciaMathieu/manager/?branch=master\"\u003e\u003cimg src=\"https://scrutinizer-ci.com/g/DeGraciaMathieu/manager/badges/coverage.png?b=master\" alt=\"Code Coverage\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/degraciamathieu/manager\"\u003e\u003cimg src=\"https://img.shields.io/packagist/v/degraciamathieu/manager.svg?style=flat-square\" alt=\"Latest Version on Packagist\"\u003e\u003c/a\u003e\n\u003ca href='https://packagist.org/packages/degraciamathieu/manager'\u003e\u003cimg src='https://img.shields.io/packagist/dt/degraciamathieu/manager.svg?style=flat-square' /\u003e\u003c/a\u003e \n\u003c/p\u003e\n\n# DeGraciaMathieu/Manager\n\nImplementation of the Manager pattern existing in Laravel framework.\n\n* [Installation](#installation)\n* [Usage](#usage)\n* [Work with singleton](#work-with-singleton)\n* [Example with Laravel](#example-with-laravel)\n\n## Installation\n \n| Manager     | 3.*                | 2.*                | 1.*                |\n|-------------|--------------------|--------------------|--------------------|\n| php ^8.1    | :white_check_mark: | :x:                | :x:                |\n| php 8.0.*   | :white_check_mark: | :white_check_mark: | :x:                |\n| php 7.*     | :x:                | :x:                | :white_check_mark: |\n \n```\ncomposer require degraciamathieu/manager\n```\n\n## Usage\n\nThis package offers an abstract class `DeGraciaMathieu\\Manager\\Manager` which needs to be extended to implement the creation of various Driver classes :\n\n```php\nnamespace App\\Managers;\n\nuse DeGraciaMathieu\\Manager\\Manager;\n\nclass WeatherManager extends Manager {\n\n    public function createOpenweathermapDriver() \n    {\n        return new Openweathermap();\n    }\n\n    public function getDefaultDriver(): string\n    {\n        return 'openweathermap';\n    }\n}\n```\n\nA driver is a class integrating all the logic of an implementation, in our examples the interactions with the APIs of [Openweathermap](https://openweathermap.org/api) :\n\n```php\nnamespace App\\Managers;\n\nclass Openweathermap {\n\n    public function itsRainingNow(string $city): bool\n    {   \n        // call Openweathermap api to know if it is raining in this city\n\n        return true;\n    }\n}\n```\nFrom now on, you can directly call the method of a driver directly from the manager :\n\n```php\n(new WeatherManager())-\u003eitsRainingNow('Paris'); // true\n```\n\nThe manager will call the `itsRainingNow` method of the default driver configured by the `getDefaultDriver` method.\n\nYou can also call any driver from the manager's `driver` method :\n\n```php\n(new WeatherManager())-\u003edriver('openweathermap')-\u003eitsRainingNow('Paris');\n```\n\nNow if you want to create a new implementation, for example if you want to use [Aerisweather](https://www.aerisweather.com/develop/api/) APIs, you just have to create a new driver in your manager :\n\n```php\nnamespace App\\Managers;\n\nuse DeGraciaMathieu\\Manager\\Manager;\n\nclass WeatherManager extends Manager {\n\n    public function createOpenweathermapDriver()\n    {\n        return new Openweathermap();\n    }\n\n    public function createAerisweatherDriver()\n    {\n        return new Aerisweather();\n    }\n\n    public function getDefaultDriver(): string\n    {\n        return 'openweathermap';\n    }\n}\n```\n\n\u003e Tip, the `getDefaultDriver` method is the perfect place to use a configuration or environment variable !\n\n## Add an interface to the drivers \n\nFor more consistency it is advisable to implement an interface to the different drivers :\n\n```php\nnamespace App\\Managers;\n\ninterface Driver {\n    public function itsRainingNow(string $city): bool;\n}\n```\n\nYou obviously need to add this interface to your drivers.\n\n```php\nnamespace App\\Managers;\n\nuse DeGraciaMathieu\\Manager\\Manager;\n\nclass WeatherManager extends Manager {\n\n    public function createOpenweathermapDriver(): Driver\n    {\n        return new Openweathermap();\n    }\n\n    public function createAerisweatherDriver(): Driver\n    {\n        return new Aerisweather();\n    }\n\n    public function getDefaultDriver(): string\n    {\n        return 'openweathermap';\n    }\n}\n```\n\nNow you will be assured that each driver instantiated by the manager will have the same interface.\n\n## Repository class\n\nTo control side effects of drivers, it is advisable to create a class encapsulating the instance of a driver, this class is usually called `Repository` :\n\n```php\nnamespace App\\Managers;\n\nuse DeGraciaMathieu\\Manager\\Manager;\n\nclass WeatherManager extends Manager {\n\n    public function createOpenweathermapDriver(): Repository\n    {\n        $driver = new Openweathermap();\n\n        return new Repository($driver);\n    }\n\n    public function createAerisweatherDriver(): Repository\n    {\n        $driver = new Aerisweather();\n\n        return new Repository($driver);\n    }\n\n    public function getDefaultDriver(): string\n    {\n        return 'openweathermap';\n    }\n}\n```\n\nThe repository is a class providing a bridge between your application and the driver :\n\n```php\nnamespace App\\Managers;\n\nclass Repository {\n\n    public function __construct(\n        private Driver $driver,\n    ){}\n\n    public function itsRainingNow(string $city): bool\n    {\n        return $this-\u003edriver-\u003eitsRainingNow($city);\n    }\n}\n```\n\n\u003e This repository class is an anti-corruption layer\n\nThus, your application will never be aware of which driver it is handling, because it will always be encapsulated in a class repository.\n\nThe repository is also a good place if you need to add specific logic for all drivers.\n\n## Work with singleton\n\nYou can also cache the creation of Drivers with the `$singleton` property.\n\nWith the `singleton` property you will only create one instance of `Openweathermap` driver :\n\n```php\n\u003c?php\n\n$weatherManager = new WeatherManager(singleton: true);\n\n$weatherManager-\u003edriver('openweathermap')-\u003eitsRainingNow('Paris');\n$weatherManager-\u003edriver('openweathermap')-\u003eitsRainingNow('Paris');\n$weatherManager-\u003edriver('openweathermap')-\u003eitsRainingNow('Paris');\n```\n\n\u003e by default, singleton property value is False\n\n## Example with Laravel\n\n[Usage example](https://github.com/DeGraciaMathieu/manager-laravel-10-examples) of the pattern manager in a Laravel project.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdegraciamathieu%2Fmanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdegraciamathieu%2Fmanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdegraciamathieu%2Fmanager/lists"}