Skip to content

Commit 5197432

Browse files
committed
Added Processors
1 parent 6364ae1 commit 5197432

13 files changed

+409
-95
lines changed

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
},
2424
"extra": {
2525
"sunscreen": {
26-
"interface": "Jenko\\Sunscreen\\SunscreenInterface"
26+
"interfaces": [
27+
"Jenko\\Sunscreen\\SunscreenInterface"
28+
]
2729
}
2830
}
2931
}
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Jenko\Sunscreen\Processor;
4+
5+
class AdapterMethodsProcessor implements ProcessorInterface
6+
{
7+
/**
8+
* @var array
9+
*/
10+
private $methods;
11+
12+
/**
13+
* @var string
14+
*/
15+
private $interfaceProperty;
16+
17+
/**
18+
* @param array $methods
19+
* @param $interfaceProperty
20+
*/
21+
public function __construct(array $methods, $interfaceProperty)
22+
{
23+
$this->methods = $methods;
24+
$this->interfaceProperty = $interfaceProperty;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function generate($asString = false)
31+
{
32+
$methodsString = '';
33+
/** @var \ReflectionMethod $method */
34+
foreach ($this->methods as $k => $method) {
35+
// Only want public methods on our interface.
36+
if (!$method->isPublic()) {
37+
continue;
38+
}
39+
40+
$params = [];
41+
/** @var \ReflectionParameter $parameter */
42+
foreach ($method->getParameters() as $parameter) {
43+
$params[] = '$' . $parameter->getName();
44+
}
45+
46+
$methodsString .= strtr(
47+
file_get_contents(__DIR__ . '/../../template/AdapterMethodsTemplate.tpl'),
48+
[
49+
'<methodName>' => $method->getName(),
50+
'<parameters>' => rtrim(implode(', ', $params), ','),
51+
'<interfaceProperty>' => $this->interfaceProperty
52+
]
53+
);
54+
55+
if ($k !== count($this->methods)-1) {
56+
$methodsString .= "\n";
57+
}
58+
}
59+
60+
if ($asString === true) {
61+
return $methodsString;
62+
}
63+
64+
// TODO: Generate file or throw exception?
65+
}
66+
}

src/Processor/AdapterProcessor.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Jenko\Sunscreen\Processor;
4+
5+
class AdapterProcessor implements ProcessorInterface
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $interfaceFqn;
11+
12+
/**
13+
* @var string
14+
*/
15+
private $namespace;
16+
17+
/**
18+
* @param string $interfaceFqn
19+
* @param string $namespace
20+
*/
21+
public function __construct($interfaceFqn, $namespace)
22+
{
23+
$this->interfaceFqn = $interfaceFqn;
24+
$this->namespace = $namespace;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function generate($asString = false)
31+
{
32+
$reflected = new \ReflectionClass($this->interfaceFqn);
33+
34+
$interfaceName = $reflected->getShortName();
35+
$interfaceProperty = lcfirst($interfaceName);
36+
37+
// TODO: Inject a factory into class then create methods processor through that.
38+
$methodsProcessor = new AdapterMethodsProcessor($reflected->getMethods(), $interfaceProperty);
39+
40+
$adapterString = strtr(
41+
file_get_contents(__DIR__ . '/../../template/AdapterTemplate.tpl'),
42+
[
43+
'<namespace>' => $this->namespace,
44+
'<interfaceFqn>' => $this->interfaceFqn,
45+
'<className>' => $this->makeClassName($interfaceName),
46+
'<interfaceName>' => $interfaceName,
47+
'<interfaceProperty>' => $interfaceProperty,
48+
'<methods>' => $methodsProcessor->generate(true)
49+
]
50+
);
51+
52+
if ($asString === true) {
53+
return $adapterString;
54+
}
55+
56+
// TODO: Generate file
57+
}
58+
59+
private function makeClassName($interfaceName)
60+
{
61+
$interfaceFqnParts = explode('\\', $this->interfaceFqn);
62+
63+
return reset($interfaceFqnParts) . str_replace('Interface', '', $interfaceName);
64+
}
65+
}

src/Processor/ClassProcessor.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Jenko\Sunscreen\Processor;
4+
5+
class ClassProcessor implements ProcessorInterface
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $classFqn;
11+
12+
/**
13+
* @var string
14+
*/
15+
private $namespace;
16+
17+
/**
18+
* @param string $classFqn
19+
* @param string $namespace
20+
*/
21+
public function __construct($classFqn, $namespace)
22+
{
23+
$this->classFqn = $classFqn;
24+
$this->namespace = $namespace;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function generate($asString = false)
31+
{
32+
$reflected = new \ReflectionClass($this->classFqn);
33+
34+
// TODO: Inject a factory into class then create methods processor through that.
35+
$methodsProcessor = new InterfaceMethodsProcessor($reflected->getMethods());
36+
37+
$class = $reflected->getShortName();
38+
$methods = $methodsProcessor->generate();
39+
40+
$classString = strtr(
41+
file_get_contents(__DIR__ . '/../../template/ClassTemplate.tpl'),
42+
[
43+
'<namespace>' => $this->namespace,
44+
'<className>' => $class,
45+
'<methods>' => $methods
46+
]
47+
);
48+
49+
return $classString;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Jenko\Sunscreen\Processor;
4+
5+
class InterfaceMethodsProcessor implements ProcessorInterface
6+
{
7+
/**
8+
* @var array
9+
*/
10+
private $methods;
11+
12+
/**
13+
* @param array $methods
14+
*/
15+
public function __construct(array $methods)
16+
{
17+
$this->methods = $methods;
18+
}
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function generate($asString = false)
24+
{
25+
$methodsString = '';
26+
/** @var \ReflectionMethod $method */
27+
foreach ($this->methods as $k => $method) {
28+
// Only want public methods on our interface.
29+
if (!$method->isPublic()) {
30+
continue;
31+
}
32+
33+
$params = [];
34+
/** @var \ReflectionParameter $parameter */
35+
foreach ($method->getParameters() as $parameter) {
36+
$params[] = '$' . $parameter->getName();
37+
}
38+
39+
$methodsString .= strtr(
40+
file_get_contents(__DIR__ . '/../../template/InterfaceMethodsTemplate.tpl'),
41+
[
42+
'<docBlock>' => $method->getDocComment(),
43+
'<methodName>' => $method->getName(),
44+
'<parameters>' => rtrim(implode(', ', $params), ',')
45+
]
46+
);
47+
48+
if ($k !== count($this->methods)-1) {
49+
$methodsString .= "\n";
50+
}
51+
}
52+
53+
if ($asString === true) {
54+
return $methodsString;
55+
}
56+
57+
// TODO: Generate file or throw exception?
58+
}
59+
}

src/Processor/InterfaceProcessor.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Jenko\Sunscreen\Processor;
4+
5+
class InterfaceProcessor implements ProcessorInterface
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $interfaceFqn;
11+
12+
/**
13+
* @var string
14+
*/
15+
private $namespace;
16+
17+
/**
18+
* @param string $interfaceFqn
19+
* @param string $namespace
20+
*/
21+
public function __construct($interfaceFqn, $namespace)
22+
{
23+
$this->interfaceFqn = $interfaceFqn;
24+
$this->namespace = $namespace;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function generate($asString = false)
31+
{
32+
$reflected = new \ReflectionClass($this->interfaceFqn);
33+
34+
// TODO: Inject a factory into class then create methods processor through that.
35+
$methodsProcessor = new InterfaceMethodsProcessor($reflected->getMethods());
36+
37+
$interface = $reflected->getShortName();
38+
$methods = $methodsProcessor->generate(true);
39+
40+
$interfaceString = strtr(
41+
file_get_contents(__DIR__ . '/../../template/InterfaceTemplate.tpl'),
42+
[
43+
'<namespace>' => $this->namespace,
44+
'<interfaceName>' => $interface,
45+
'<methods>' => $methods
46+
]
47+
);
48+
49+
if ($asString === true) {
50+
return $interfaceString;
51+
}
52+
53+
// TODO: Generate file
54+
return true;
55+
}
56+
}

src/Processor/ProcessorInterface.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Jenko\Sunscreen\Processor;
4+
5+
interface ProcessorInterface
6+
{
7+
/**
8+
* @param bool $asString
9+
*
10+
* @return mixed
11+
*/
12+
public function generate($asString = false);
13+
}

0 commit comments

Comments
 (0)