Skip to content

Commit

Permalink
Laracon 2024 (#52710)
Browse files Browse the repository at this point in the history
- deferred functions
- concurrency
- local private files
- cache::flexible
- log() function
  • Loading branch information
taylorotwell authored Sep 11, 2024
1 parent 2f83567 commit c44aa73
Showing 38 changed files with 1,500 additions and 17 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@
"illuminate/bus": "self.version",
"illuminate/cache": "self.version",
"illuminate/collections": "self.version",
"illuminate/concurrency": "self.version",
"illuminate/conditionable": "self.version",
"illuminate/config": "self.version",
"illuminate/console": "self.version",
@@ -106,7 +107,7 @@
"league/flysystem-sftp-v3": "^3.0",
"mockery/mockery": "^1.6",
"nyholm/psr7": "^1.2",
"orchestra/testbench-core": "^9.1.5",
"orchestra/testbench-core": "^9.4.0",
"pda/pheanstalk": "^5.0",
"phpstan/phpstan": "^1.11.5",
"phpunit/phpunit": "^10.5|^11.0",
@@ -131,6 +132,7 @@
"src/Illuminate/Events/functions.php",
"src/Illuminate/Filesystem/functions.php",
"src/Illuminate/Foundation/helpers.php",
"src/Illuminate/Log/functions.php",
"src/Illuminate/Support/helpers.php"
],
"psr-4": {
20 changes: 20 additions & 0 deletions config/concurrency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Default Concurrency Driver
|--------------------------------------------------------------------------
|
| This option determines the default concurrency driver that will be used
| by Laravel's concurrency functions. By default, concurrent work will
| be sent to isolated PHP processes which will return their results.
|
| Supported: "process", "fork", "sync"
|
*/

'driver' => env('CONCURRENCY_DRIVER', 'process'),

];
55 changes: 55 additions & 0 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
@@ -471,6 +471,61 @@ public function rememberForever($key, Closure $callback)
return $value;
}

/**
* Retrieve an item from the cache by key, refreshing it in the background if it is stale.
*
* @template TCacheValue
*
* @param string $key
* @param array{ 0: int, 1: int } $ttl
* @param (callable(): TCacheValue) $callback
* @param array{ seconds?: int, owner?: string }|null $lock
* @return TCacheValue
*/
public function flexible($key, $ttl, $callback, $lock = null)
{
[
$key => $value,
"{$key}:created" => $created,
] = $this->many([$key, "{$key}:created"]);

if ($created === null) {
return tap(value($callback), fn ($value) => $this->putMany([
$key => $value,
"{$key}:created" => Carbon::now()->getTimestamp(),
], $ttl[1]));
}

if (($created + $this->getSeconds($ttl[0])) > Carbon::now()->getTimestamp()) {
return $value;
}

$refresh = function () use ($key, $ttl, $callback, $lock, $created) {
$this->store->lock(
"illuminate:cache:refresh:lock:{$key}",
$lock['seconds'] ?? 0,
$lock['owner'] ?? null,
)->get(function () use ($key, $callback, $created, $ttl) {
if ($created !== $this->get("{$key}:created")) {
return;
}

$this->putMany([
$key => value($callback),
"{$key}:created" => Carbon::now()->getTimestamp(),
], $ttl[1]);
});
};

if (function_exists('defer')) {
defer($refresh, "illuminate:cache:refresh:{$key}");
} else {
$refresh();
}

return $value;
}

/**
* Remove an item from the cache.
*
2 changes: 2 additions & 0 deletions src/Illuminate/Concurrency/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.github export-ignore
.gitattributes export-ignore
100 changes: 100 additions & 0 deletions src/Illuminate/Concurrency/ConcurrencyManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Illuminate\Concurrency;

use Illuminate\Process\Factory as ProcessFactory;
use Illuminate\Support\MultipleInstanceManager;
use RuntimeException;
use Spatie\Fork\Fork;

/**
* @mixin \Illuminate\Contracts\Concurrency\Driver
*/
class ConcurrencyManager extends MultipleInstanceManager
{
/**
* Get a driver instance by name.
*
* @param string|null $name
* @return mixed
*/
public function driver($name = null)
{
return $this->instance($name);
}

/**
* Create an instance of the process concurrency driver.
*
* @param array $config
* @return \Illuminate\Concurrency\ProcessDriver
*/
public function createProcessDriver(array $config)
{
return new ProcessDriver($this->app->make(ProcessFactory::class));
}

/**
* Create an instance of the fork concurrency driver.
*
* @param array $config
* @return \Illuminate\Concurrency\ForkDriver
*/
public function createForkDriver(array $config)
{
if (! $this->app->runningInConsole()) {
throw new RuntimeException('Due to PHP limitations, the fork driver may not be used within web requests.');
}

if (! class_exists(Fork::class)) {
throw new RuntimeException('Please install the "spatie/fork" Composer package in order to utilize the "fork" driver.');
}

return new ForkDriver;
}

/**
* Create an instance of the sync concurrency driver.
*
* @param array $config
* @return \Illuminate\Concurrency\SyncDriver
*/
public function createSyncDriver(array $config)
{
return new SyncDriver;
}

/**
* Get the default instance name.
*
* @return string
*/
public function getDefaultInstance()
{
return $this->app['config']['concurrency.default'] ?? 'process';
}

/**
* Set the default instance name.
*
* @param string $name
* @return void
*/
public function setDefaultInstance($name)
{
$this->app['config']['concurrency.default'] = $name;
}

/**
* Get the instance specific configuration.
*
* @param string $name
* @return array
*/
public function getInstanceConfig($name)
{
return $this->app['config']->get(
'concurrency.drivers.'.$name, ['driver' => $name],
);
}
}
33 changes: 33 additions & 0 deletions src/Illuminate/Concurrency/ConcurrencyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Illuminate\Concurrency;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;

class ConcurrencyServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton(ConcurrencyManager::class, function ($app) {
return new ConcurrencyManager($app);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
ConcurrencyManager::class,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Illuminate\Concurrency\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Throwable;

#[AsCommand(name: 'invoke-serialized-closure')]
class InvokeSerializedClosureCommand extends Command
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'invoke-serialized-closure {code? : The serialized closure}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Invoke the given serialized closure';

/**
* Indicates whether the command should be shown in the Artisan command list.
*
* @var bool
*/
protected $hidden = true;

/**
* Execute the console command.
*
* @return void
*
* @throws \RuntimeException
*/
public function handle()
{
try {
$this->output->write(json_encode([
'successful' => true,
'result' => serialize($this->laravel->call(match (true) {
! is_null($this->argument('code')) => unserialize($this->argument('code')),
isset($_SERVER['LARAVEL_INVOKABLE_CLOSURE']) => unserialize($_SERVER['LARAVEL_INVOKABLE_CLOSURE']),
default => fn () => null,
})),
]));
} catch (Throwable $e) {
report($e);

$this->output->write(json_encode([
'successful' => false,
'exception' => get_class($e),
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]));
}
}
}
28 changes: 28 additions & 0 deletions src/Illuminate/Concurrency/ForkDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Illuminate\Concurrency;

use Closure;
use Illuminate\Foundation\Defer\DeferredCallback;
use Illuminate\Support\Arr;
use Spatie\Fork\Fork;

class ForkDriver
{
/**
* Run the given tasks concurrently and return an array containing the results.
*/
public function run(Closure|array $tasks): array
{
/** @phpstan-ignore class.notFound */
return Fork::new()->run(...Arr::wrap($tasks));
}

/**
* Start the given tasks in the background after the current task has finished.
*/
public function defer(Closure|array $tasks): DeferredCallback
{
return defer(fn () => $this->run($tasks));
}
}
21 changes: 21 additions & 0 deletions src/Illuminate/Concurrency/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Taylor Otwell

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading

0 comments on commit c44aa73

Please sign in to comment.