PHP code example of edram / laravel-temp-workspace

1. Go to this page and download the library: Download edram/laravel-temp-workspace 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/ */

    

edram / laravel-temp-workspace example snippets


use Edram\LaravelTempWorkspace\Facades\LaravelTempWorkspace;

$workspace = LaravelTempWorkspace::create();
$workspace->put('hello.txt', 'Hello, workspace!');

$contents = $workspace->get('hello.txt');

// Optionally delete the workspace directory and its contents early.
// $workspace->destroy();

namespace App\TempWorkspaces;

use Edram\LaravelTempWorkspace\TempWorkspace;
use Illuminate\Http\Client\Factory as Http;

final class DownloadFile extends TempWorkspace
{
    public function __construct(
        public readonly string $url,
    ) {}

    public function handle(Http $http): string
    {
        $filename = 'download.bin';

        $this->put(
            $filename,
            $http->timeout(60)->get($this->url)->throw()->body(),
        );

        return $this->checksum($filename, ['checksum_algo' => 'md5']);
    }
}

use App\TempWorkspaces\DownloadFile;
use Edram\LaravelTempWorkspace\WorkspaceManager;

$workspace = new DownloadFile($url);
$id = $workspace->id();

$md5 = app(WorkspaceManager::class)->perform($workspace);

use Edram\LaravelTempWorkspace\Facades\LaravelTempWorkspace;
use Edram\LaravelTempWorkspace\TempWorkspace;
use Illuminate\Http\Client\Factory as Http;

$md5 = LaravelTempWorkspace::perform(
    function (TempWorkspace $workspace, Http $http) use ($url): string {
        $workspace->put(
            'download.bin',
            $http->timeout(60)->get($url)->throw()->body(),
        );

        return $workspace->checksum('download.bin', ['checksum_algo' => 'md5']);
    },
);

use Edram\LaravelTempWorkspace\WorkspaceManager;

$workspace = app(WorkspaceManager::class)->create();
$workspace->put('input.txt', 'Workspace contents');

$contents = $workspace->get('input.txt');
$path = $workspace->path('input.txt');

public function failed(\Throwable $exception): void
{
    report($exception);
}

public function terminate(\Psr\Log\LoggerInterface $logger): void
{
    $logger->info('Workspace terminated.', ['workspace_id' => $this->id()]);
}

use Edram\LaravelTempWorkspace\TempWorkspace;
use Psr\Log\LoggerInterface;

$workspace->terminating(function (TempWorkspace $current, LoggerInterface $logger): void {
    $logger->info('Cleaning temporary files.', ['workspace_id' => $current->id()]);
});

use Edram\LaravelTempWorkspace\WorkspaceManager;

app(WorkspaceManager::class)->cleanup();