|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace StellarWP\Slic\Test\Support\Factories; |
| 4 | + |
| 5 | +use RuntimeException; |
| 6 | + |
| 7 | +class Directory { |
| 8 | + private string $absolutePath; |
| 9 | + |
| 10 | + /** |
| 11 | + * Directory constructor; it will create the directory as side-effect. |
| 12 | + * |
| 13 | + * since TBD |
| 14 | + * |
| 15 | + * @param string $absolutePath The absolute path to the directory to create. |
| 16 | + * |
| 17 | + * @throws RuntimeException On failure to create the directory. |
| 18 | + */ |
| 19 | + public function __construct( string $absolutePath ) { |
| 20 | + $this->absolutePath = $this->createDirectory( $absolutePath ); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Returns the directory absolute path. |
| 25 | + * |
| 26 | + * @return string The directory absolute path. |
| 27 | + */ |
| 28 | + public function getAbsolutePath(): string { |
| 29 | + return $this->absolutePath; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Factory method to create a new directory in the system temp directory. |
| 34 | + * |
| 35 | + * @param string|null $path The relative path of the directory to create, or `null` to generate a random path. |
| 36 | + * |
| 37 | + * @return self |
| 38 | + * |
| 39 | + * @throws RuntimeException If the directory already exists or cannot be created. |
| 40 | + */ |
| 41 | + public static function createTemp( ?string $path = null ): self { |
| 42 | + $path ??= '/slic-test-plugins-dir-' . uniqid( '', true ); |
| 43 | + $absolutePath = sys_get_temp_dir() . '/' . ltrim( $path, '/' ); |
| 44 | + |
| 45 | + return new self( $absolutePath ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates a plugin directory that contains an empty plugin main file. |
| 50 | + * |
| 51 | + * @param string $pluginDirectoryName The name of the plugin directory to create, e.g. `test-plugin`. |
| 52 | + * |
| 53 | + * @return $this |
| 54 | + * |
| 55 | + * @throws RuntimeException On failure to create the plugin directory or the main plugin file. |
| 56 | + */ |
| 57 | + public function createPlugin( string $pluginDirectoryName ): self { |
| 58 | + /** @noinspection UnusedFunctionResultInspection */ |
| 59 | + $this->createDirectory( $this->absolutePath . '/' . ltrim( $pluginDirectoryName, '/' ) ); |
| 60 | + |
| 61 | + $pluginFilePath = $this->absolutePath . "/$pluginDirectoryName/plugin.php"; |
| 62 | + if ( ! file_put_contents( $pluginFilePath, "/**\n* Plugin Name: Test Plugin\n*/" ) ) { |
| 63 | + throw new RuntimeException( "Failed to create $pluginFilePath" ); |
| 64 | + } |
| 65 | + |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Creates a directory. |
| 71 | + * |
| 72 | + * @param string $absolutePath The absolute path to the directory to create. |
| 73 | + * |
| 74 | + * @return string The absolute path to the created directory. |
| 75 | + * |
| 76 | + * @throws RuntimeException On failure to create the directory or if the directory already exists. |
| 77 | + */ |
| 78 | + private function createDirectory( string $absolutePath ): string { |
| 79 | + if ( is_dir( $absolutePath ) ) { |
| 80 | + throw new RuntimeException( "Directory $absolutePath already exists." ); |
| 81 | + } |
| 82 | + |
| 83 | + if ( ! mkdir( $absolutePath, 0777, true ) || ! is_dir( $absolutePath ) ) { |
| 84 | + throw new RuntimeException( "Failed to create plugins directory $absolutePath" ); |
| 85 | + } |
| 86 | + |
| 87 | + return $absolutePath; |
| 88 | + } |
| 89 | +} |
0 commit comments