Fork me on GitHub

Services

Configuration

Services are inspired by Symfony's Service Container. The link best desribes what services are.

Services in Hathoora PHP Framework are lazy loaded. They are not instantiated until they are called. You can define services in configuration like so:

        
            services:
                assets:     <-- name of service
                    class: \namespace\class

                    # method to excute when service is called
                    method: functionName

                    # params to be passed to method
                    params: [param1, param2]

                    # calls calls are the methods called automatically when service is loaded
                    calls:
                        setContainer: [ @container@ ]  <-- @SERVICE@ format for referencing a service
                        someFunction: [ %hathoora.logger.profiling.enabled% ]  <-- %KEY% format for referencing a configuration

                # hathoora cache service
                cache:
                    class: \hathoora\cache\cache
                    calls:
                        setContainer: [ @container@ ]

                    # static services are cached..
                    type: static


                # example of factory service
                cache.notes:
                    factory_service: @cache@

                    # method to call from factory method when the service is called
                    factory_method: pool

                    # params to pass to factory_method
                    factory_method_args: [ "common" ]
        
    

Built-in Services

Following services are define automatically:

  • container - returns an instance of container
  • translator - this will be defined is hathoora.translation.enabled = 1
  • gulabooAssets - this will be defined is hathoora.gulaboo.assets.enabled = 1

Accessing Services

Services are accessed from container. Container is already available in components like contoller, model, database etc already extend container.

Following example shows how services are used.

        
            if (\hathoora\container::hasService('cache.notes'))
            {
                $service = \hathoora\container::getService('cache.notes');
                $service->doSomething();
            }
        
    

If you have setup a service using method parameter, then you can also pass dynamic variables to it's method. Following example demonstrate that.

        
            # builtin translation service
            service:
                translator:
                    class: \hathoora\translation\translator
                    method: t


            # where t method of \hathoora\translation\translator looks like the following:
            /**
             * Translation function
             */
            public function t($t, $arrToken = null, $lang = null)
            {}


            # using transltor service to pass params to it's method
            $this->getService('translator',
                                            // params to be passed to t()
                                            array('auth_login_failure_error',
                                                array(
                                                    'token1' => 'tokenValue'
                                                ),
                                                $lang = 'en_US'
                                            )
                             );


            $this->getService('translator',
                                            // params to be passed to t()
                                            array('generic_error_message'));
        
    

In this example we are using translator service and pass parameters to t() method of \hathoora\translation\translator class.

Page Took:         3.58 msec
Page Memory:   514.77k
docs / viewController / view (200)
Request UUID: 5a328fa6b2f900.41699304
Get
Post
Cookies
Files
Session
Server
Response
No data.
No data.
No data.
No data.
No data.
REDIRECT_HATHOORA_ENV
prod
REDIRECT_STATUS
200
HATHOORA_ENV
prod
HTTP_HOST
hathoora.org
HTTP_X_FORWARDED_FOR
54.221.93.187
HTTP_X_REAL_IP
54.221.93.187
HTTP_CONNECTION
close
HTTP_ACCEPT_ENCODING
x-gzip, gzip, deflate
HTTP_USER_AGENT
CCBot/2.0 (http://commoncrawl.org/faq/)
HTTP_ACCEPT
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_IF_MODIFIED_SINCE
Tue, 24 Oct 2017 11:04:49 GMT
PATH
/sbin:/usr/sbin:/bin:/usr/bin
SERVER_SIGNATURE
Apache/2.2.15 (CentOS) Server at hathoora.org Port 80
SERVER_SOFTWARE
Apache/2.2.15 (CentOS)
SERVER_NAME
hathoora.org
SERVER_ADDR
192.168.1.11
SERVER_PORT
80
REMOTE_ADDR
207.126.164.119
DOCUMENT_ROOT
/var/websites/hathoora.org/docroot
SERVER_ADMIN
webmaster@hathoora.org
SCRIPT_FILENAME
/var/websites/hathoora.org/docroot/index.php
REMOTE_PORT
44930
REDIRECT_URL
/docs/v1/services
GATEWAY_INTERFACE
CGI/1.1
SERVER_PROTOCOL
HTTP/1.0
REQUEST_METHOD
GET
QUERY_STRING
REQUEST_URI
/docs/v1/services
SCRIPT_NAME
/index.php
PHP_SELF
/index.php
REQUEST_TIME_FLOAT
1513263014.732
REQUEST_TIME
1513263014
Status: 200
Headers:
Content-Type
text/html; charset=UTF-8
Configutation
Key Value
app.admin
Array
(
    [pattern] => /admin(|/)
    [directory] => hathoora
    [namespace] => hathoora\admin
)
app.docs
Array
(
    [pattern] => /docs(|/)
    [directory] => hathoora
    [dispatcher] => Array
        (
            [class] => customDispatcher
            [method] => dispatch
        )

)
app.site
Array
(
    [default] => 1
)
hathoora.logger
Array
(
    [profiling] => Array
        (
            [enabled] => 1
        )

    [logging] => Array
        (
            [enabled] => 1
            [level] => DEBUG
        )

    [webprofiler] => Array
        (
            [enabled] => 1
            [system] => 
            [show_redirects] => 
        )

)
hathoora.template
Array
(
    [engine] => Array
        (
            [name] => Stuob
        )

)
hathoora.database
Array
(
    [default] => mysql://dbuser:dbpassword@dbhost:3306/dbname
)
services.container
Array
(
    [class] => \hathoora\container
    [method] => getContainer
)
Logging (14)
hathoora.logger.logging.enabled: 1
# Time (msec) Level Memory (KB) Message
1 1.912 DEBUG 309.70 Configuration loaded:
Array
(
    [0] => app_prod.yml
    [1] => config_prod.yml
)
2 2.036 DEBUG 323.30 Listener "kernel.terminate[webprofiler]" has been added because of hathoora.logger.webprofiler.enabled
3 2.121 DEBUG 326.70 Event kernel.ready has been registered.
4 2.212 DEBUG 335.06 Calling App dispatcher (Array ( [class] => customDispatcher [method] => dispatch ) )...
5 2.286 DEBUG 349.48 Event kernel.route has been registered.
6 2.401 DEBUG 378.84 Event kernel.controller has been registered.
7 2.423 DEBUG 379.79 Calling controller (\docs\controller\viewController::view) with following params
Array
(
    [0] => v1
    [1] => services
)
8 2.711 INFO 467.14 Template (docs/v1/services.tpl.php) loaded
9 2.806 INFO 475.79 Template (/var/websites/hathoora.org/app/hathoora/docs//resources/templates/components/htmlMeta.tpl.php) loaded
10 2.863 INFO 477.04 Template (/var/websites/hathoora.org/app/hathoora/docs//resources/templates/components/flashMessages.tpl.php) loaded
11 3.390 INFO 438.23 Template (docs.tpl.php) fetched.
12 3.416 DEBUG 437.82 Event kernel.response has been registered.
13 3.434 DEBUG 439.01 Event kernel.terminate has been registered.
14 3.482 DEBUG 442.23 Event kernel.terminate->webprofiler has been notified.
Profiling
template
# Time (msec) Name Cached Took (msec)
1 2.469 docs.tpl.php 0 0.868