PHP code example of bnomei / kirby-turbo

1. Go to this page and download the library: Download bnomei/kirby-turbo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bnomei / kirby-turbo example snippets



class ExamplePage extends \Kirby\Cms\Page
{
    use \Bnomei\ModelWithTurbo;
}


return [
    // ✅ preconfigured defaults
    // 'bnomei.turbo.cache.inventory' => ['type' => 'turbo-file'],
    // 'bnomei.turbo.cache.storage' => ['type' => 'redis', 'database' => 0],
    // 'bnomei.turbo.cache.tub' => ['type' => 'turbo-redis', 'database' => 0],
    
    // ⚠️ the UUID cache-driver you need to set yourself!
    'cache' => ['uuid' => ['type' => 'turbo-uuid']],  // exec() + sed
    
    // ... other options
];

\Bnomei\Turbo::flush();            // all
\Bnomei\Turbo::flush('inventory'); // specific one

tub()->set('key', 'value');
$value = tub()->get('key');
$value = tub()->getOrSet('key', fn() => 'value');

tub()->set($pages->pluck('uuid'), $pages->count());

tub()->set([
    kirby()->user()?->id(), 
    kirby()->language()?->code(), 
    $apiUrl
], $apiResponse, 24*60);

tub()->set($keyCanBeStringOrArray, $dataCanBeArrayAndContainingKirbyFields);

tub()->set(
    $page/*->uuid()->toString() + kirby()->language()?->code() */, 
    $pages->toArray(fn($p) => ['title' => $p->title()/*->value()*/])
);

tub()->set('key', fn () => 'value', 'next monday');
tub()->set('key', fn () => 'value', 'last day of this month 23:59:59');

$value = tub()->getOrSet($key, function() use ($page) {
    if ($page->performCheck() === false) { // up to you
        throw new \Bnomei\AbortCachingException();
    }
    return [
        'uuid' => $page->uuid()->id(), 
        'title' => $page->title(), 
        'url' => $page->url()
    ];
});


Kirby::plugin('my/example', [
  'collections' => [
    // collections have to return a closure, and that is why tubs' value is a closure
    'recent-courses' => tubs(
        'recent-courses', // key: array|string
        function () {     // value: closure
            return page('courses')->children()->listed()->sortBy('name')->limit(10);
        }
    ),
    // or with an additional cache around the collection itself to minimize the lookup
    'recent-courses' => tubs(
        'recent-courses', // key: array|string
        function () {     // value: closure
            $uuids = tub()->getOrSet(
                'recent-courses', 
                fn() => page('courses')->children()->listed()->sortBy('name')->limit(10)->values(
                    fn(\Kirby\Cms\Page $p) => $p->uuid()->toString()
                ),
                1 // in minutes
            );
            
            return new \Kirby\Cms\Pages($uuids);
        }
    ),
]);

$actors = page('film/academy-dinosaur')->actors()->toPagesTurbo(); // Pages Collection
$images = page('actors/adam-grant')->gallery()->toFilesTurbo();    // Files Collection

echo page('film')->children()->modified();           // 1734873708
echo page('actors/adam-grant')->files()->modified(); // 1737408738

// Kirby core will scan all dirs and all files
echo site()->modified();

// new helper reads from turbo inventory cache
echo site()->modifiedTurbo(); // 1734873708

// on app initialisation
$kirby = new App([
  'components' => [
    'storage' => function (App $kirby, ModelWithContent $model) {
        return new \Bnomei\TurboStorage($model);
    ]
  ]  
]);

// or in a plugin
App::plugin('my/storage', [
  'components' => [
    'storage' => function (App $kirby, ModelWithContent $model) {
        return new \Bnomei\TurboStorage($model);
    ]
  ]  
]);


// 'vendor/autoload.php';

\Bnomei\TurboStopwatch::before('kirby');
$kirby = new \Kirby\Cms\App;
$render = $kirby->render();
\Bnomei\TurboStopwatch::after('kirby');

\Bnomei\TurboStopwatch::header('turbo.read');  // not iming
\Bnomei\TurboStopwatch::serverTiming();        // all events in a single header

echo $render;