Skip to content

Commit

Permalink
Add new filesystem structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Miraeld committed Nov 15, 2024
1 parent 7351ff4 commit cbe336f
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
46 changes: 46 additions & 0 deletions inc/Engine/Common/AbstractFileSystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Common;

use WP_Filesystem_Direct;

abstract class AbstractFileSystem {

/**
* WP Filesystem instance.
*
* @var WP_Filesystem_Direct
*/
protected $filesystem;

/**
* Constructor method.
* Initializes a new instance of the Controller class.
*
* @param WP_Filesystem_Direct $filesystem Filesystem class.
*/
public function __construct( $filesystem = null ) {
$this->filesystem = $filesystem ?? rocket_direct_filesystem();
}

/**
* Checks if a given path exists in the filesystem.
*
* @param string $path The path to check.
* @return bool True if the path exists, false otherwise.
*/
public function exists( string $path ): bool {
return $this->filesystem->exists( $path );
}

/**
* Retrieves the contents of a file at the given path.
*
* @param string $path The path to the file.
* @return string|false The file contents on success, false on failure.
*/
public function get_contents( string $path ) {
return $this->filesystem->get_contents( $path );
}
}
49 changes: 49 additions & 0 deletions inc/Engine/Media/Fonts/Controller/Filesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Fonts\Controller;

use WP_Rocket\Engine\Common\AbstractFileSystem;
use WP_Rocket\Logger\Logger;
use WP_Filesystem_Direct;

class Filesystem extends AbstractFileSystem {

/**
* Path to the fonts storage
*
* @var string
*/
private $path;

Check failure on line 18 in inc/Engine/Media/Fonts/Controller/Filesystem.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Property WP_Rocket\Engine\Media\Fonts\Controller\Filesystem::$path is never read, only written.

/**
* Version of the fonts.
*
* @var int
*/
private $version;

Check failure on line 25 in inc/Engine/Media/Fonts/Controller/Filesystem.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Property WP_Rocket\Engine\Media\Fonts\Controller\Filesystem::$version is unused.

/**
* Instantiate the class
*
* @param string $base_path Base path to the fonts storage.
* @param WP_Filesystem_Direct $filesystem WP Filesystem instance.
*/
public function __construct( $base_path, $filesystem = null ) {
parent::__construct( is_null( $filesystem ) ? rocket_direct_filesystem() : $filesystem );

$this->path = $base_path . get_current_blog_id() . '/';
}

/**
* Hash the url
*
* @param string $font_url Font url.
*
* @return string
*/
private function hash_url( string $font_url ): string {

Check failure on line 46 in inc/Engine/Media/Fonts/Controller/Filesystem.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Method WP_Rocket\Engine\Media\Fonts\Controller\Filesystem::hash_url() is unused.
return md5( $font_url );
}
}
11 changes: 6 additions & 5 deletions inc/Engine/Media/Fonts/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace WP_Rocket\Engine\Media\Fonts\Frontend;

use WP_Rocket\Engine\Media\Fonts\Context\Context;
use WP_Rocket\Engine\Media\Fonts\Controller\Filesystem;
use WP_Rocket\Engine\Optimization\RegexTrait;
use WP_Rocket\Logger\Logger;
use WP_Filesystem_Direct;
Expand All @@ -29,7 +30,7 @@ class Controller {
/**
* WordPress filesystem.
*
* @var WP_Filesystem_Direct
* @var WP_Rocket\Engine\Media\Fonts\Controller\Filesystem;

Check failure on line 33 in inc/Engine/Media/Fonts/Frontend/Controller.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

PHPDoc tag @var has invalid value (WP_Rocket\Engine\Media\Fonts\Controller\Filesystem;): Unexpected token ";", expected TOKEN_HORIZONTAL_WS at offset 93 on line 4
*/
private $filesystem;

Expand All @@ -43,10 +44,10 @@ class Controller {
/**
* Constructor.
*
* @param Context $context Context instance.
* @param WP_Filesystem_Direct|null $filesystem WordPress filesystem.
* @param Context $context Context instance.
* @param WP_Rocket\Engine\Media\Fonts\Controller\Filesystem|null $filesystem WordPress filesystem.
*/
public function __construct( Context $context, ?WP_Filesystem_Direct $filesystem ) {
public function __construct( Context $context, ?Filesystem $filesystem ) {

Check failure on line 50 in inc/Engine/Media/Fonts/Frontend/Controller.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

PHPDoc tag @param for parameter $filesystem with type WP_Rocket\Engine\Media\Fonts\Frontend\WP_Rocket\Engine\Media\Fonts\Controller\Filesystem|null is not subtype of native type WP_Rocket\Engine\Media\Fonts\Controller\Filesystem|null.

Check failure on line 50 in inc/Engine/Media/Fonts/Frontend/Controller.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Parameter $filesystem of method WP_Rocket\Engine\Media\Fonts\Frontend\Controller::__construct() has invalid type WP_Rocket\Engine\Media\Fonts\Frontend\WP_Rocket\Engine\Media\Fonts\Controller\Filesystem.
$this->context = $context;
$this->base_path = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH', '' ) . 'fonts/' . get_current_blog_id() . '/';
$this->base_url = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_URL', '' ) . 'fonts/' . get_current_blog_id() . '/';
Expand Down Expand Up @@ -130,7 +131,7 @@ protected function get_optimized_markup( string $hash, string $original_url ): s
*
* @param bool $enable Tells if we are enabling or not the inline css output.
*/
$inline_fonts_css = wpm_apply_filters_typed( 'boolean', 'rocket_host_fonts_locally_inline_css', false );
$inline_fonts_css = wpm_apply_filters_typed( 'boolean', 'rocket_host_fonts_locally_inline_css', true );
if ( $inline_fonts_css ) {
$raw_path = $this->base_path . $path . '.css';
$inline_css = $this->get_font_inline_css( $gf_parameters, $raw_path );
Expand Down
11 changes: 10 additions & 1 deletion inc/Engine/Media/Fonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use WP_Rocket\Engine\Media\Fonts\Frontend\Controller as FrontendController;
use WP_Rocket\Engine\Media\Fonts\Frontend\Subscriber as FrontendSubscriber;
use WP_Filesystem_Direct;
use WP_Rocket\Engine\Media\Fonts\Controller\Filesystem;

class ServiceProvider extends AbstractServiceProvider {
/**
Expand All @@ -27,6 +28,7 @@ class ServiceProvider extends AbstractServiceProvider {
'media_fonts_context',
'media_fonts_frontend_controller',
'media_fonts_frontend_subscriber',
'media_fonts_filesystem',
];

/**
Expand All @@ -49,6 +51,13 @@ public function register(): void {
$this->getContainer()->add( 'media_fonts_settings', Settings::class );
$this->getContainer()->add( 'wp_direct_filesystem', WP_Filesystem_Direct::class )
->addArgument( [] );
$this->getContainer()->add( 'media_fonts_filesystem', Filesystem::class )
->addArguments(
[
wpm_apply_filters_typed( 'string', 'rocket_host_font_cache_root', 'fonts/' . get_current_blog_id() ),
'wp_direct_filesystem',
]
);
$this->getContainer()->addShared( 'media_fonts_admin_subscriber', AdminSubscriber::class )
->addArgument( 'media_fonts_settings' );

Expand All @@ -58,7 +67,7 @@ public function register(): void {
->addArguments(
[
$this->getContainer()->get( 'media_fonts_context' ),
$this->getContainer()->get( 'wp_direct_filesystem' ),
$this->getContainer()->get( 'media_fonts_filesystem' ),
]
);
$this->getContainer()->add( 'media_fonts_frontend_subscriber', FrontendSubscriber::class )
Expand Down

0 comments on commit cbe336f

Please sign in to comment.