Skip to main content
Version: 2.0.0

The Packages Registry

In Jalno, the packages\base\Packages class was created for the technical management of packages. This class provides the ability to register a package, enable a language within a package, access all active packages, and also access each package individually.

This class is used by the framework, but it is also made available so that whenever a programmer intends to perform an operation outside the framework's normal flow, they can freely use this class to do so.

The following methods are defined in the Packages class. For ease of use, the methods are defined as static.

MethodUsage
package(string $name): ?PackageAccess a package
get(?array $names): arrayAccess all active packages
register(Package $package)Register a package
registerTranslates(string $code)Enable a language across the packages

Accessing Packages

In the Packages class, the package and get methods are defined for accessing packages.

By calling the package method, you can access any of the active packages defined in the project. If the specified package exists, the output of this method is an object of the packages\base\Package class, which contains the package's details such as its dependent packages, routing file, translation files, and in general all of the settings configured in the package structure file (package.json).

If the specified package does not exist, a packages\base\IO\NotFoundException exception is thrown.

use packages\base\Packages;


$package = Packages::package("packagename");

Example package.json file:

package.json
{
"permissions": "*",
"routing": "routing.json",
"frontend": "frontend",
"autoload": {
"directories": ["controllers", "Models", "listeners", "events"]
},
"dependencies": ["userpanel"],
"languages":{
"fa_IR": "langs/fa_IR.json",
"en_US": "langs/en_US.json"
},
"events": [
{
"name": "packages/userpanel/events/usertype_permissions_list",
"listener": "listeners/settings/usertypes/Permissions@list"
}
],
"upload_path": "storage/public/upload/"
}

Example controller file demonstrating access to a package's information:

controllers/Albums.php
<?php
namespace packages\packagename\controllers;

use themes\themename\views;
use packages\base\{Controller, Response, View, Packages, IO\File};

class Albums extends Controller {

public function insertImg(): Response {

$view = view::byName(views\Albums\InsertImg::class);
$this->response->setView($view);

if (Http::is_post()){
$inputs = $this->checkinputs([
'img' =>[
'type' => 'image',
"max-size" => 2097152, // Byte
"obj" => true,
"extension" => array('jpeg', 'jpg')
]
]);

$package = Packages::package('packagename');

$path = $package->getOption("upload_path") . $inputs['img']->getFile()->md5() . '.' . $inputs['img']->getExtension();

$img = $package->getFile($path);

$directory = $img->getDirectory();
if (!$directory->exists()) {
$directory->make(true);
}

$inputs['img']->saveToFile($img);
}

$this->response->setStatus(true);
return $this->response;
}
}

The get method is defined for accessing all active packages. The output of the get method is an array of Package class objects. You can pass an array of the names of the packages you need as the input argument of the get method; in that case, the method's output is an array of the requested packages.

use packages\base\Packages;


Packages::get();

Packages::get(["my_package", "PhpParser"]);

Registering a Package

The register method is created for registering a package. This method is used by the packages\base\Loader class when the framework scans and loads the packages. The input of this method is an object of the packages\base\Package class.

Enabling a Package's Language

The registerTranslates method takes a full language code, then reads and activates the packages' translation files for that language code. The language code can be the default language, the active language, or any desired language.

The registerTranslates method is also used when packages are loaded and registered in the packages\base\Loader class.

use packages\base\Packages;


Packages::registerTranslates("en_US");