Skip to content
linzongshu edited this page Oct 17, 2015 · 6 revisions

Brief

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.

How to recognize services

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.

Provided Service APIs

Now we can focus on the Pi/Application/Service folder, and find all services provided at the moment:

Service configuration

As we mentioned above, services implemented differently according to their configuration. This section we will introduce how to set their configuration.

From configuration file

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.

From parameters

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.

Clone this wiki locally