Skip to content

Commit

Permalink
Increase testability: no direct file access in ComponentFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw authored and malberts committed Nov 8, 2021
1 parent 34f00eb commit f610f5e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"ext-dom": "*",
"ext-filter": "*",
"composer/installers": "^1.0.12",
"mediawiki/bootstrap": "^4.5"
"mediawiki/bootstrap": "^4.5",
"jeroen/file-fetcher": "^6|^5|^4.4"
},
"require-dev": {
"php": ">=7.4.3",
Expand Down
9 changes: 4 additions & 5 deletions src/Chameleon.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace Skins\Chameleon;

use Bootstrap\BootstrapManager;
use FileFetcher\SimpleFileFetcher;
use MediaWiki\MediaWikiServices;
use OutputPage;
use QuickTemplate;
Expand Down Expand Up @@ -128,14 +129,12 @@ protected function setupTemplateForOutput() {
return $template;
}

/**
* @return ComponentFactory
*/
public function getComponentFactory() {
public function getComponentFactory(): ComponentFactory {
if ( !isset( $this->componentFactory ) ) {
$this->componentFactory = new ComponentFactory(
$this->getLayoutFilePath(),
MediaWikiServices::getInstance()->getHookContainer()
MediaWikiServices::getInstance()->getHookContainer(),
new SimpleFileFetcher()
);
}

Expand Down
18 changes: 6 additions & 12 deletions src/ComponentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@

use DOMDocument;
use DOMElement;
use FileFetcher\FileFetcher;
use MediaWiki\HookContainer\HookContainer;
use MWException;
use QuickTemplate;
use RuntimeException;
use Skins\Chameleon\Components\Component;
use Skins\Chameleon\Components\Container;

/**
* Class ComponentFactory
*
* @author Stephan Gambke
* @since 1.0
* @ingroup Skins
Expand All @@ -53,12 +51,14 @@ class ComponentFactory {
private $skinTemplate;

private HookContainer $hookContainer;
private FileFetcher $fileFetcher;

private const NAMESPACE_HIERARCHY = 'Skins\\Chameleon\\Components';

public function __construct( string $layoutFileName, HookContainer $hookContainer ) {
public function __construct( string $layoutFileName, HookContainer $hookContainer, FileFetcher $fileFetcher ) {
$this->setLayoutFileName( $layoutFileName );
$this->hookContainer = $hookContainer;
$this->fileFetcher = $fileFetcher;
}

/**
Expand Down Expand Up @@ -99,7 +99,7 @@ private function getDomDocument(): DOMDocument {
}

private function getLayoutXml(): string {
$xml = file_get_contents( $this->layoutFileName );
$xml = $this->fileFetcher->fetchFile( $this->layoutFileName );

$this->hookContainer->run(
Chameleon::HOOK_GET_LAYOUT_XML,
Expand All @@ -110,13 +110,7 @@ private function getLayoutXml(): string {
}

private function setLayoutFileName( string $fileName ) {
$fileName = $this->sanitizeFileName( $fileName );

if ( !is_readable( $fileName ) ) {
throw new RuntimeException( "Expected an accessible {$fileName} layout file" );
}

$this->layoutFileName = $fileName;
$this->layoutFileName = $this->sanitizeFileName( $fileName );
}

/**
Expand Down

0 comments on commit f610f5e

Please sign in to comment.