MageWork

Back to home

Framework tools

Page

<?php

$page = App::page();

The page method allows retrieving the current page object from anywhere.

<?php

$route = App::getRoute();

The getRoute method allows retrieving the current (cleaned) route name from anywhere (e.g. /contact.html).

/my-page.html = /my-page.html
/my-page/     = /my-page
/my-page/?q=1 = /my-page
/my-page?q=1  = /my-page

Encryption

<?php

$var = App::encryption()->crypt('message');

echo App::encryption()->decrypt($var);

The encryption key is stored in var/encryption (read-only 0400 file). If the key is lost, it will be impossible to decrypt the data ever again.

Pass true as the second argument to use a fixed initialization vector (same input always produces the same output). Only do this for values that are guaranteed unique.

<?php

$token = App::encryption()->crypt($uuid, true);

Cache

<?php

App::cache()->set('foo', 'bar');

echo App::cache()->get('foo');

The cache file is stored in var/cache. The default cache lifetime is 86400 seconds. Update it before assignment:

<?php

App::cache()->setLifetime(3600)->set('foo', 'bar');
MethodDescription
setStore one value
bulkStore several values at once (array)
getRead one value (null if missing or expired)
allRead every cached value
setLifetime / getLifetimeLifetime in seconds (0 disables caching)
cleanByKeyRemove one key if it is expired
cleanExpiredRemove every expired key
cleanAllEmpty the cache

Session

<?php

App::session()?->set('foo', 'bar');

echo App::session()?->get('foo');

Sessions are stored in var/session. The session cookie is named after the package and its lifetime, SameSite and HttpOnly flags come from the configuration (see Configuration).

MethodDescription
setStore one value, or several with an array
getRead one value (all values with no argument)
pullRead a value and remove it
hasCheck a value exists
id / regenerateCurrent session id / regenerate it
destroyDestroy the session and clear the cookie

App::session(false) returns the session only if one already exists, without starting a new one.

Log

<?php

App::log('message');

Logs are stored in var/log/app.log. A Throwable, an array or an object is formatted automatically.

The second parameter sets the level (default Logger::INFO):

<?php

App::log('message', Logger::WARN);

Logger::EMERG;  // Emergency: system is unusable
Logger::ALERT;  // Alert: action must be taken immediately
Logger::CRIT;   // Critical: critical conditions
Logger::ERR;    // Error: error conditions
Logger::WARN;   // Warning: warning conditions
Logger::NOTICE; // Notice: normal but significant condition
Logger::INFO;   // Informational: informational messages
Logger::DEBUG;  // Debug: debug messages

App::getLogger() returns the Logger instance. Use trim() to cap the file size:

<?php

App::getLogger()?->trim(1000); // keep only the last 1000 lines

Database

<?php

$id = App::db()->insert('table', ['name' => 'John Doe']);

App::db()->query('UPDATE table SET name = ? WHERE id = ?', ["Jane Doe", 1]);

$result = App::db()->query('SELECT * FROM table')->fetchAll();

$result = App::db()->query('SELECT * FROM table WHERE id = ?', [1])->fetch();

Config

<?php

echo App::getConfig('app.session_lifetime');

The second argument is the value returned when the path does not exist:

<?php

$lifetime = App::getConfig('app.session_lifetime', 3600);

Escaper

<?php

App::escapeHtml('<a href="#">Escaped HTML</a>');

App::escapeHtmlAttr('my-class');

App::escapeUrl('https://www.example.com');

App::escaper()->escapeQuotes("that's true");
<a href="#" onclick="alert('<?= App::escaper()->escapeQuotes("that's true") ?>')">Alert</a>
<?php

$indexes = [
    1 => 'Phillip J. Fry',
    2 => 'Leela Turanga',
    3 => 'Bender Bending Rodriguez',
    4 => 'Capitaine Zapp Brannigan',
    5 => 'Amy Wong',
];

$search = new Search();

$result = $search->search('leilla captain', $indexes);
array(2) {
    [2]=> string(13) "Leela Turanga"
    [4]=> string(24) "Capitaine Zapp Brannigan"
}

The constructor tunes the matching: new Search($minChar, $minDistance) — minimum word length (default 3) and maximum Levenshtein distance ratio for a fuzzy match (default 0.4, lower is stricter).

<?php

$search = new Search(3, 0.2); // stricter matching