Libraries in hacks implement the data access function and act as a layer between modules and a database or third-party services. Within the MVC framework, they are most similar to "models".

Creating a library

Working with libraries is carried out in the "Libraries" section by clicking the "Files" button. The library files themselves are located in the lib folder of your hack. The file name with the library can consist of small Latin letters and numbers. It must begin with a letter, not a number. You cannot use capital letters or special characters, including hyphens or underscores.

When a library is created, the corresponding class is automatically created in the file. The class name consists of the word hack, the name of the hack and the name of the library, separated by an underscore, for example: hack_proxy_items.

class hack_proxy_items { … }

Be sure to include the following constructor in the library:

private $core;
public function __construct( $core ) {
  $this->core = $core;
  // Other initialisation code
}

The use of $core is required for almost all tasks in working with the system. The library constructor is always called with the $core parameter.

Connecting and using libraries

The library is automatically loaded and initialized on the first call. The library is called by the code:

$core->hack->(name)->(library)

For example, to access the items library of the proxy module, the following call will be used:

$core->hack->proxy->items

It is recommended to immediately access nested functions or fields of the library itself, for example:

$core->hack->proxy->items->add( $item );
$core->hack->proxy->items->mode = 10;

Redefining system libraries

It is not recommended to do this. But in exceptional cases, you can extend the system libraries with hacks. This can only be done with great care.

The file must have the same name as the original library. It contains a class called hack_name that inherits from name, where name is the name of the source library.

Example:

class hack_address extends address {
  public function check( $geo, $i, $a, $c, $s, $h ) { 
    if ( $geo == 'kz' ) return $core->hack->cisaddr->kz->check( $i, $a, $c, $s, $h );
    if ( $geo == 'kg' ) return $core->hack->cisaddr->kg->check( $i, $a, $c, $s, $h );
    return parent::check( $geo, $i, $a, $c, $s, $h );
  }
}

The list of redefined libraries is specified in the initialization array with the lib key. It should contain the names of the library files without the extension.

Example:

return [
  /* other initialization commands */
  'lib' => [ 'address' ]
];