{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Service Provider","type":"basic","slug":"supporting-structure-service-provider","excerpt":"","body":"# Service Provider\n\nWe're building applications that has a single purpose in which it must be separated under your underlying app.\n\nThis page helps you to create your own service, or helps you on how to handle a service.\n\nI you'd read the [Module](doc:mvc-module), there is a sample to create your own service provider.\n\n\n###  Index\n- [Basic Usage](#basic-usage)\n- [Relying on a Service](#relying-service)\n- [Vendor Publish](#vendor)\n\n\n---\n\n\n<a name=\"basic-usage\"></a>\n# Basic Usage\n\nTo create a service, you must create a class, let's assume we're building a bridge to wrap a certain library.\n\nLet's assume we have this package to be imported on our project jenssegers/agent, now we want to easily create a dependency injection for this project rather than having an instance everytime we need it.\n\n```shell\ncd /var/www/my-slayer-agent/\nmkdir src/\ncomposer init\n```\n\nNow manage your composer, write the essential things. Upon doing that you might want to add this to your composer.json\n\n```json\n    \"autoload\": {\n        \"psr-4\": {\n            \"MySlayerAgent\\\\Agent\\\\\": \"src/\"\n        }\n    }\n```\n\nRun the initial ``composer dumpautoload`` so that it will register as a psr4, it interprets your ``src/`` folder having this namespace ``MySlayerAgent\\Agent``. Let's create a class name it as ***AgentServiceProvider***\n\n```php\n// root-folder/src/AgentServiceProvider.php\n<?php\nnamespace MySlayerAgent\\Agent;\n\nuse Jenssegers\\Agent\\Agent;\nuse Clarity\\Providers\\ServiceProvider;\n\nclass AgentServiceProvider extends ServiceProvider\n{\n    protected $alias = 'agent';\n    protected $shared = false;\n\n    public function register()\n    {\n        return new Agent;\n    }\n}\n```\n\nRegister this clas inside ``project-name/config/app.php`` under ``services``, as **MySlayerAgent\\Agent\\AgentServiceProvider::class**\n\nThe above class will be injected as dependency using the alias ``agent``, the register() function returns an instance of ``\\Jenssegers\\Agent\\Agent`` in which you can call using the ``di()`` function.\n\n```php\ndi()->get('agent')->is('Windows');\ndi()->get('agent')->is('Firefox');\ndi()->get('agent')->is('iPhone');\ndi()->get('agent')->is('OS X');\ndi()->get('agent')->isAndroidOS();\ndi()->get('agent')->isNexus();\ndi()->get('agent')->isSafari();\ndi()->get('agent')->isMobile();\ndi()->get('agent')->isTablet();\ndi()->get('agent')->match('regexp');\ndi()->get('agent')->languages();\n```\n\nYou could even create your own facade.\n\n```php\n// root-folder/src/AgentFacade.php\n<?php\nnamespace MySlayerAgent\\Agent;\n\nuse Clarity\\Facades\\Facade;\n\nclass AgentFacade extends Facade\n{\n    protected static function getFacadeAccessor()\n    {\n        return 'agent';\n    }\n}\n```\n\nRegister it as well at ``app.php`` config as ``'Agent' => MySlayerAgent\\Agent\\AgentFacade::class``\n\nNow you can easily call it through this\n\n```php\n\\Agent::is('Windows');\n\\Agent::is('Firefox');\n\\Agent::is('iPhone');\n\\Agent::is('OS X');\n\\Agent::isAndroidOS();\n\\Agent::isNexus();\n\\Agent::isSafari();\n\\Agent::isMobile();\n\\Agent::isTablet();\n\\Agent::match('regexp');\n\\Agent::languages();\n```\n\n---\n\n\n<a name=\"relying-service\"></a>\n# Relying on a Service\n\nTo rely on other services, there is a ``boot()`` function that you could call.\n\n```php\n    public function boot()\n    {\n        # add some event manager and so on ...\n        // di()->get('log')->...\n    }\n```\n\nThere is also an example, find ``project-name/components/Providers/``, check the ``Application.php`` and ``Dispatcher.php``, we call the dependency injection itself to create an event.\n\n***Application.php:***\n```php\n    public function boot()\n    {\n        $app = di()->get('application');\n\n        $event_manager = new EventsManager;\n\n        $event_manager->attach('application', new ApplicationEventListener);\n\n        $app->setEventsManager($event_manager);\n    }\n```\n\n\n---\n\n<a name=\"vendor\"></a>\n# Vendor Publish\n\nSo you have files that you want to import from the root path of your project. There is an option to do that.\n\nUnder ``boot()`` function, you can call the ``publish(<array folders>, <tag name>)`` command.\n\n```php\n    public function boot()\n    {\n        $this->publish([\n            __DIR__.'/resources/views' => base_path('resources/views/vendor/popeye'),\n        ], 'view');\n    \n        $this->publish([\n            __DIR__.'/dist' => base_path(),\n        ], 'dist');\n    }\n```\n\nThe above code shows, on how you could publish your folders. Try to run this on your console. Here's the console format ``php brood vendor:publish <provider alias>``, and there is a *tag* option.\n\n```php\nphp brood vendor:publish popeye\n```\n\nThe above console command will iterate all the registered tags and requires you to confirm one-by-one.\n\nTo make a specific publish, you can add ``--tag`` in the command\n```php\nphp brood vendor:publish popeye --tag=dist\n```","updates":[],"order":3,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"56e26e555ab0871700957c04","project":"56c111095abfe40d00be875a","user":"56c1105874f0b417004baadc","createdAt":"2016-03-11T07:05:57.896Z","__v":14,"category":{"sync":{"isSync":false,"url":""},"pages":["56e26e3a8d79b50e0031d8f6","56e26e42353e060e00b96826","56e26e4d95d1c60e00a969db","56e26e555ab0871700957c04","56e26e6f5ab0871700957c06","56e26e7d7d54ba0e004635a3","56e26e85353e060e00b96828"],"title":"Supporting Structure","slug":"supporting-structure","order":3,"from_sync":false,"reference":false,"_id":"56c42826c0c4630d004e86cb","__v":7,"createdAt":"2016-02-17T07:58:30.542Z","project":"56c111095abfe40d00be875a","version":"56c111095abfe40d00be875d"},"version":{"version":"1.3.0","version_clean":"1.3.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["56c1110a5abfe40d00be875e","56c413a254b6030d00ec299d","56c4275048213b1700af6e33","56c42826c0c4630d004e86cb","56c4282cbc41330d009f2607","56c4284ad1f6d91700d3697e","56e271c195d1c60e00a969ee"],"_id":"56c111095abfe40d00be875d","releaseDate":"2016-02-14T23:43:05.566Z","__v":7,"createdAt":"2016-02-14T23:43:05.566Z","project":"56c111095abfe40d00be875a"},"githubsync":"","parentDoc":null}
# Service Provider We're building applications that has a single purpose in which it must be separated under your underlying app. This page helps you to create your own service, or helps you on how to handle a service. I you'd read the [Module](doc:mvc-module), there is a sample to create your own service provider. ### Index - [Basic Usage](#basic-usage) - [Relying on a Service](#relying-service) - [Vendor Publish](#vendor) --- <a name="basic-usage"></a> # Basic Usage To create a service, you must create a class, let's assume we're building a bridge to wrap a certain library. Let's assume we have this package to be imported on our project jenssegers/agent, now we want to easily create a dependency injection for this project rather than having an instance everytime we need it. ```shell cd /var/www/my-slayer-agent/ mkdir src/ composer init ``` Now manage your composer, write the essential things. Upon doing that you might want to add this to your composer.json ```json "autoload": { "psr-4": { "MySlayerAgent\\Agent\\": "src/" } } ``` Run the initial ``composer dumpautoload`` so that it will register as a psr4, it interprets your ``src/`` folder having this namespace ``MySlayerAgent\Agent``. Let's create a class name it as ***AgentServiceProvider*** ```php // root-folder/src/AgentServiceProvider.php <?php namespace MySlayerAgent\Agent; use Jenssegers\Agent\Agent; use Clarity\Providers\ServiceProvider; class AgentServiceProvider extends ServiceProvider { protected $alias = 'agent'; protected $shared = false; public function register() { return new Agent; } } ``` Register this clas inside ``project-name/config/app.php`` under ``services``, as **MySlayerAgent\Agent\AgentServiceProvider::class** The above class will be injected as dependency using the alias ``agent``, the register() function returns an instance of ``\Jenssegers\Agent\Agent`` in which you can call using the ``di()`` function. ```php di()->get('agent')->is('Windows'); di()->get('agent')->is('Firefox'); di()->get('agent')->is('iPhone'); di()->get('agent')->is('OS X'); di()->get('agent')->isAndroidOS(); di()->get('agent')->isNexus(); di()->get('agent')->isSafari(); di()->get('agent')->isMobile(); di()->get('agent')->isTablet(); di()->get('agent')->match('regexp'); di()->get('agent')->languages(); ``` You could even create your own facade. ```php // root-folder/src/AgentFacade.php <?php namespace MySlayerAgent\Agent; use Clarity\Facades\Facade; class AgentFacade extends Facade { protected static function getFacadeAccessor() { return 'agent'; } } ``` Register it as well at ``app.php`` config as ``'Agent' => MySlayerAgent\Agent\AgentFacade::class`` Now you can easily call it through this ```php \Agent::is('Windows'); \Agent::is('Firefox'); \Agent::is('iPhone'); \Agent::is('OS X'); \Agent::isAndroidOS(); \Agent::isNexus(); \Agent::isSafari(); \Agent::isMobile(); \Agent::isTablet(); \Agent::match('regexp'); \Agent::languages(); ``` --- <a name="relying-service"></a> # Relying on a Service To rely on other services, there is a ``boot()`` function that you could call. ```php public function boot() { # add some event manager and so on ... // di()->get('log')->... } ``` There is also an example, find ``project-name/components/Providers/``, check the ``Application.php`` and ``Dispatcher.php``, we call the dependency injection itself to create an event. ***Application.php:*** ```php public function boot() { $app = di()->get('application'); $event_manager = new EventsManager; $event_manager->attach('application', new ApplicationEventListener); $app->setEventsManager($event_manager); } ``` --- <a name="vendor"></a> # Vendor Publish So you have files that you want to import from the root path of your project. There is an option to do that. Under ``boot()`` function, you can call the ``publish(<array folders>, <tag name>)`` command. ```php public function boot() { $this->publish([ __DIR__.'/resources/views' => base_path('resources/views/vendor/popeye'), ], 'view'); $this->publish([ __DIR__.'/dist' => base_path(), ], 'dist'); } ``` The above code shows, on how you could publish your folders. Try to run this on your console. Here's the console format ``php brood vendor:publish <provider alias>``, and there is a *tag* option. ```php php brood vendor:publish popeye ``` The above console command will iterate all the registered tags and requires you to confirm one-by-one. To make a specific publish, you can add ``--tag`` in the command ```php php brood vendor:publish popeye --tag=dist ```