-
Notifications
You must be signed in to change notification settings - Fork 114
Dev.Pi Services
Pi provides a series of service APIs, these services are independent instances, and they are initialized as soon as they are first called. And the instances will be stored in storage for later calling. You can imagine they as global APIs independent from action, module internal APIs and methods but service configuration.
The entry of service is in Pi.php
, we can find a static method named service()
in the class. This method needs two parameters, and the first one of them specify the service name. The string will be use to assemble class name, and the classes are placed in Pi/Application/Service folder. For example Pi::service('asset')
means a class named Pi\Application\Service\Asset
is instantiated, then you can use it to call its public APIs.
Now we can focus on the Pi/Application/Service folder, and find all services provided at the moment:
- Pi::service('api')
- Pi::service('asset')
- Pi::service('audit')
- Pi::service('authentication')
- Pi::service('avatar')
- Pi::service('browser')
- Pi::service('cache')
- Pi::service('captcha')
- Pi::service('comment')
- Pi::service('config')
- Pi::service('cookie')
- Pi::service('database')
- Pi::service('editor')
- Pi::service('event')
- Pi::service('geoIp')
- Pi::service('i18n')
- Pi::service('image')
- Pi::service('log')
- Pi::service('mail')
- Pi::service('markup')
- Pi::service('media')
- Pi::service('memcache')
- Pi::service('memcached')
- Pi::service('module')
- Pi::service('permission')
- Pi::service('registry')
- Pi::service('remote')
- Pi::service('render')
- Pi::service('renderCache')
- Pi::service('security')
- Pi::service('session')
- Pi::service('socialSharing')
- Pi::service('string')
- Pi::service('tag')
- Pi::service('taxonomy')
- Pi::service('theme')
- Pi::service('url')
- Pi::service('user')
- Pi::service('view')
As we mentioned above, services implemented differently according to their configuration. This section we will introduce how to set their configuration.
Each service has a configuration file which is placed in var/config
folder. The file includes the default configuration setting and you should not need to change it by default. But you can also reset it as you wish.
The configuration file name has a special name format just like service.{specify name}.php
. Most of the time, the specify name is service name, for example, the configuration file service.asset.php
belongs to Pi::service('asset')
. But the key determiner is the $fileIdentifier
variable in every service class. For example:
class Demo extends AbstractService
{
protected $fileIdentifier = 'test';
...
}
The example above will load configuration file service.test.php
other than service.demo.php
.
Note that, the $fileIdentifier
override the same variable in its parent class AbstractService
, which is empty string. Therefore, if the service class donot specify the variable, it means this service class donot need configuration file.
Sometime we just want to change the config at the moment, we can use the parameters to change configuration temporarily.
We talked the first parameter of Pi::service()
method above, and it specify the class name. Then we can find it has the second parameter which has default value we sometime donnot need to set. This parameter is use to set the service configuration when it called.
For example:
$options = ['active' => false];
Pi::service('log', $options)->log(Pi\Log\Logger::INFO, 'Log info');
Wherever the above example is called, the site's debug panel will be closed, and nothing will be logged out.