Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 18a16a0

Browse files
committed
Extract Component object
1 parent 5ef4daf commit 18a16a0

12 files changed

Lines changed: 270 additions & 182 deletions

class/Component.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace ThemeViz;
4+
5+
class Component
6+
{
7+
private $componentsFile;
8+
private $css;
9+
private $screen;
10+
private $themeConfig;
11+
12+
public function __construct($componentsFile, $css, $screen, $themeConfig)
13+
{
14+
$this->componentsFile = $componentsFile;
15+
$this->css = $css;
16+
$this->screen = $screen;
17+
$this->themeConfig = $themeConfig;
18+
}
19+
20+
/**
21+
* @return mixed
22+
*/
23+
public function getScenarios()
24+
{
25+
$defaultScenario = [];
26+
$scenarios = $this->screen["scenarios"] ?: [$defaultScenario];
27+
28+
return array_map(function ($scenario) {
29+
return $this->getScenarioData($scenario);
30+
}, $scenarios);
31+
}
32+
33+
/**
34+
* @param $scenario
35+
* @return array
36+
*/
37+
private function getScenarioData($scenario)
38+
{
39+
$classes = implode(",", $this->componentsFile["wrapperClasses"] ?? []);
40+
$useBootstrap = $this->themeConfig["depends"]["settings"]["global_bootstrap"] ?? FALSE;
41+
$componentData = [
42+
"themeviz_component_path" => $this->screen["path"],
43+
"themeviz_wrapper_classes" => $classes,
44+
"themeviz_css" => $this->css,
45+
"themeviz_use_bootstrap" => $useBootstrap
46+
];
47+
$themeDefaults = $this->componentsFile["defaults"]["twig"] ?? [];
48+
49+
return array_merge_recursive($componentData, $themeDefaults, $scenario);
50+
}
51+
}

class/ComponentRepository.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace ThemeViz;
4+
5+
6+
class ComponentRepository
7+
{
8+
/** @var Filesystem $filesystem */
9+
private $filesystem;
10+
11+
/** @var LessCompiler $lessCompiler */
12+
private $lessCompiler;
13+
14+
private $css;
15+
16+
private $themeConfig;
17+
private $componentsFile;
18+
19+
public function __construct(Filesystem $filesystem, LessCompiler $lessCompiler)
20+
{
21+
$this->filesystem = $filesystem;
22+
$this->lessCompiler = $lessCompiler;
23+
}
24+
25+
public function getComponent($screen)
26+
{
27+
$this->themeConfig = $this->getThemeConfig();
28+
$this->componentsFile = $this->getComponentsFile();
29+
30+
$this->css = $this->css ?? $this->lessCompiler->getCss($this->themeConfig, $this->componentsFile);
31+
32+
return new Component($this->componentsFile, $this->css, $screen, $this->themeConfig);
33+
}
34+
35+
/**
36+
* @return array
37+
* @throws \Exception
38+
*/
39+
private function getComponentsFile(): array
40+
{
41+
return $this->getCachedDecodedJsonFile(
42+
"componentsFile",
43+
THEMEVIZ_THEME_PATH . "/components.json"
44+
);
45+
}
46+
47+
/**
48+
* @return array
49+
* @throws \Exception
50+
*/
51+
private function getThemeConfig(): array
52+
{
53+
return $this->getCachedDecodedJsonFile(
54+
"themeConfig",
55+
THEMEVIZ_THEME_PATH . "/theme.conf"
56+
);
57+
}
58+
59+
/**
60+
* @param $fieldName
61+
* @param $path
62+
* @return mixed
63+
* @throws \Exception
64+
*/
65+
private function getCachedDecodedJsonFile($fieldName, $path)
66+
{
67+
if (!$this->$fieldName) {
68+
$json = $this->filesystem->getFile($path);
69+
70+
$this->$fieldName = json_decode($json, TRUE);
71+
72+
if ($json && $this->$fieldName === NULL) {
73+
throw new \Exception("Error attempting to decode json file: $path");
74+
}
75+
}
76+
77+
return $this->$fieldName;
78+
}
79+
}

class/Factory.php

Lines changed: 68 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ class Factory
2222
/** @var Twig $twig */
2323
private $twig;
2424

25-
public function __construct(
25+
/**
26+
* Factory constructor.
27+
* @param Filesystem|null $filesystem
28+
* @param Firefox|null $firefox
29+
* @param Git|null $git
30+
* @param Less|null $less
31+
* @param Pixelmatch|null $pixelmatch
32+
* @param Twig|null $twig
33+
*/
34+
public function __construct(
2635
Filesystem $filesystem = null,
2736
Firefox $firefox = null,
2837
Git $git = null,
@@ -39,143 +48,64 @@ public function __construct(
3948
$this->twig = $twig;
4049
}
4150

42-
/**
43-
* @return Renderer
44-
*/
45-
public function getRenderer()
46-
{
47-
return $this->getObject(
48-
"Renderer",
49-
$this->getDiffer(),
50-
$this->getFilesystem(),
51-
$this->getGit(),
52-
$this->getPhotographer(),
53-
$this->getScenarioStorage(),
54-
$this->getSummaryCompiler(),
55-
$this->getTwigCompiler()
56-
);
57-
}
58-
59-
/**
60-
* @return TwigCompiler
61-
*/
62-
public function getTwigCompiler()
63-
{
64-
return $this->getObject(
65-
"TwigCompiler",
66-
$this->getLessCompiler(),
67-
$this->getTwig()
68-
);
69-
}
70-
71-
/**
72-
* @return LessCompiler
73-
*/
74-
public function getLessCompiler()
75-
{
76-
return $this->getObject(
77-
"LessCompiler",
78-
$this->getFilesystem(),
79-
$this->getLess()
80-
);
81-
}
82-
83-
/**
84-
* @return Differ
85-
*/
86-
public function getDiffer()
87-
{
88-
return $this->getObject(
89-
"Differ",
90-
$this->getFilesystem(),
91-
$this->getPixelmatch()
92-
);
93-
}
94-
95-
/**
96-
* @return Photographer
97-
*/
98-
public function getPhotographer()
99-
{
100-
return $this->getObject(
101-
"Photographer",
102-
$this->getFilesystem(),
103-
$this->getFirefox()
104-
);
105-
}
106-
107-
/**
108-
* @return SummaryCompiler
109-
*/
110-
public function getSummaryCompiler()
111-
{
112-
return $this->getObject(
113-
"SummaryCompiler",
114-
$this->getFilesystem(),
115-
$this->getTwig()
116-
);
117-
}
118-
119-
/**
120-
* @return ScenarioStorage
121-
*/
122-
public function getScenarioStorage()
123-
{
124-
return $this->getObject(
125-
"ScenarioStorage",
126-
$this->getFilesystem()
127-
);
128-
}
129-
130-
/**
131-
* @return Filesystem
132-
*/
133-
public function getFilesystem()
134-
{
135-
return $this->getObject("Filesystem");
136-
}
137-
138-
/**
139-
* @return Firefox
140-
*/
141-
public function getFirefox()
142-
{
143-
return $this->getObject("Firefox");
144-
}
145-
146-
/**
147-
* @return Git
148-
*/
149-
public function getGit()
150-
{
151-
return $this->getObject("Git");
152-
}
153-
154-
/**
155-
* @return Less
156-
*/
157-
public function getLess()
158-
{
159-
return $this->getObject("Less");
160-
}
161-
162-
/**
163-
* @return Pixelmatch
164-
*/
165-
public function getPixelmatch()
166-
{
167-
return $this->getObject("Pixelmatch");
168-
}
169-
170-
/**
171-
* @return Twig
172-
*/
173-
public function getTwig()
174-
{
175-
return $this->getObject("Twig");
176-
}
177-
178-
private function getObject($class, ...$dependencies)
51+
/**
52+
* @param $method
53+
* @param array $args
54+
* @return null
55+
* @throws \ReflectionException
56+
*/
57+
public function __call($method, $args = [])
58+
{
59+
$isGet = substr( $method, 0, 3 ) === "get";
60+
61+
if (!$isGet) return null;
62+
63+
$name = substr($method, 3, strlen($method) - 3);
64+
$dependencyNames = $this->getDependencyNames($name);
65+
$dependencies = array_map(function($dependencyName) {
66+
$methodName = "get$dependencyName";
67+
return $this->$methodName();
68+
}, $dependencyNames);
69+
70+
return $this->getObject($name, ...$dependencies);
71+
}
72+
73+
/**
74+
* @param $name
75+
* @return array|mixed
76+
* @throws \ReflectionException
77+
*/
78+
private function getDependencyNames($name)
79+
{
80+
$simpleName = $this->getSimpleClassName($name);
81+
$reflection = new \ReflectionClass("\\ThemeViz\\$simpleName");
82+
$constructor = $reflection->getConstructor();
83+
$params = ($constructor) ? $constructor->getParameters() : [];
84+
85+
return array_map(function($param) {
86+
$name = $param->getClass()->name;
87+
88+
return $this->getSimpleClassName($name);
89+
}, $params);
90+
}
91+
92+
/**
93+
* @param $name
94+
* @return string
95+
*/
96+
private function getSimpleClassName($name): string
97+
{
98+
$nameFragments = explode("\\", $name);
99+
100+
return end($nameFragments);
101+
}
102+
103+
/**
104+
* @param $class
105+
* @param mixed ...$dependencies
106+
* @return mixed
107+
*/
108+
private function getObject($class, ...$dependencies)
179109
{
180110
$fullClassName = "\\ThemeViz\\$class";
181111
$propertyName = lcfirst($class);

class/LessCompiler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private function parseLessDefaults($componentsFile): void
5353
private function parseBaseLess(): void
5454
{
5555
$baseLess = $this->filesystem->getFile(THEMEVIZ_BASE_PATH . "/view/base.less") ?? "";
56+
5657
$this->less->parse($baseLess);
5758
}
5859

class/Renderer.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public function compile()
5555
$this->themeConfig = $this->getThemeConfig();
5656
$this->componentsFile = $this->getComponentsFile();
5757

58-
if (!$this->themeConfig) {
58+
if ($this->themeConfig === null) {
5959
throw new \Exception("Missing theme.conf file!");
6060
}
6161

62-
if (!$this->componentsFile) {
62+
if ($this->componentsFile === null) {
6363
throw new \Exception("Missing components file!");
6464
}
6565

@@ -82,8 +82,7 @@ public function compile()
8282

8383
/**
8484
* @param $buildName
85-
* @throws \Less_Exception_Parser
86-
*/
85+
*/
8786
private function makeBuild($buildName): void
8887
{
8988
if (!$components = $this->twigCompiler->compileTwig($this->themeConfig, $this->componentsFile)) return;

0 commit comments

Comments
 (0)