PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / trunk
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution vtrunk
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Lock / Lock_Factory.php

Lock_Factory.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution trunk, at src/Performance/Lock/Lock_Factory.php

61 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A factory to create an atomic lock instance.
4 *
5 * @package SolidWP\Performance
6 */
7
8 declare( strict_types=1 );
9
10 namespace SolidWP\Performance\Lock;
11
12 use SolidWP\Performance\Container;
13 use SolidWP\Performance\Lock\Contracts\Blockable_Lock;
14 use SolidWP\Performance\Lock\Contracts\Lock;
15
16 /**
17 * A factory to create an atomic lock instance.
18 *
19 * @package SolidWP\Performance
20 */
21 final class Lock_Factory {
22
23 public const RANDOM_OWNER_LENGTH = 16;
24
25 /**
26 * @var Container
27 */
28 private Container $container;
29
30 /**
31 * @param Container $container The container.
32 */
33 public function __construct( Container $container ) {
34 $this->container = $container;
35 }
36
37 /**
38 * Make an atomic lock instance.
39 *
40 * @param string $name The unique lock name.
41 * @param int $expiration The expiration in seconds.
42 * @param string $owner The owner of the lock.
43 *
44 * @return Blockable_Lock
45 */
46 public function make( string $name, int $expiration, string $owner = '' ): Blockable_Lock {
47 if ( ! strlen( $owner ) ) {
48 $owner = wp_generate_password( self::RANDOM_OWNER_LENGTH, false );
49 }
50
51 $entry = new Lock_Entry( $name, $expiration, $owner );
52
53 // Configure the Lock Driver to accept the entry.
54 $this->container->when( Lock::class )
55 ->needs( Lock_Entry::class )
56 ->give( $entry );
57
58 return $this->container->get( Blockable_Lock::class );
59 }
60 }
61