Skip to content

Commit e08b901

Browse files
committed
getting again to 80%
added the macro and command pattern to decouple the partsinstaller from directly executing commands, it can now do dryInstall and Install added commands for previous parts refactored all parts to use the new installer interface (testing could be better, but thats not so easy for all cases)
1 parent a8d281c commit e08b901

40 files changed

+1023
-179
lines changed

Diff for: lib/Webforge/CMS/Navigation/Node.php

-4
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,5 @@ public function getLft();
2525

2626
public function setLft($left);
2727

28-
public function setRoot($root);
29-
30-
public function getRoot();
31-
3228
}
3329
?>

Diff for: lib/Webforge/CMS/Navigation/SimpleNode.php

+5-23
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
class SimpleNode implements Node {
1010

11-
protected $lft, $rgt, $root, $depth, $title, $parent;
11+
protected $lft, $rgt, $depth, $title, $parent;
1212

1313
public function __construct(Array $node) {
1414
$this->title = $node['title'];
@@ -22,10 +22,6 @@ public function __construct(Array $node) {
2222
if (isset($node['rgt']))
2323
$this->rgt = $node['rgt'];
2424

25-
if (isset($node['root'])) {
26-
$this->root = $node['root'];
27-
}
28-
2925
if (isset($node['parent'])) {
3026
$this->parent = $node['parent'];
3127
}
@@ -41,7 +37,6 @@ public function unwrap() {
4137
'rgt' => $this->rgt,
4238
'lft' => $this->lft,
4339
'depth' => $this->depth
44-
//'root' => $this->root
4540
);
4641
}
4742

@@ -53,6 +48,8 @@ public function equalsNode(Node $other = NULL) {
5348
return isset($other) && $other->getTitle() === $this->getTitle();
5449
}
5550

51+
// @codeCoverageIgnoreStart
52+
5653
/**
5754
* @param TestNode $parent
5855
* @chainable
@@ -84,8 +81,6 @@ public function setTitle($title) {
8481
public function getTitle() {
8582
return $this->title;
8683
}
87-
88-
8984

9085
/**
9186
* @param integer $depth
@@ -103,21 +98,6 @@ public function getDepth() {
10398
return $this->depth;
10499
}
105100

106-
/**
107-
* @param integer $root
108-
* @chainable
109-
*/
110-
public function setRoot($root) {
111-
$this->root = $root;
112-
return $this;
113-
}
114-
115-
/**
116-
* @return integer
117-
*/
118-
public function getRoot() {
119-
return $this->root;
120-
}
121101

122102
/**
123103
* @param int $rgt
@@ -150,5 +130,7 @@ public function setLft($lft) {
150130
public function getLft() {
151131
return $this->lft;
152132
}
133+
134+
// @codeCoverageIgnoreEnd
153135
}
154136
?>

Diff for: lib/Webforge/Code/GlobalClassFileMapper.php

-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
class GlobalClassFileMapper implements ClassFileMapper {
3232

3333
const WITH_RESOLVING = 0x000001;
34-
const WITH_INCLUDE_PATH = 0x000002;
3534

3635
/**
3736
* A Registry for Packages installed on the host (e.g.)
@@ -126,10 +125,6 @@ function ($file) use ($testsDir) {
126125
}
127126

128127
protected function validateFile(File $file, $flags = 0x0000) {
129-
if ($flags & self::WITH_INCLUDE_PATH && $file->isRelative()) {
130-
throw new \Psc\Code\NotImplementedException('YAGNI: composer was smart enough');
131-
}
132-
133128
if ($flags & self::WITH_RESOLVING) {
134129
$file->resolvePath();
135130
}

Diff for: lib/Webforge/Code/Test/ConsoleOutput.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Webforge\Code\Test;
4+
5+
class ConsoleOutput extends \Symfony\Component\Console\Output\Output {
6+
7+
public $stream;
8+
9+
public function doWrite($msg, $newline) {
10+
$this->stream .= $msg.($newline ? "\n" : '');
11+
}
12+
}
13+
?>

Diff for: lib/Webforge/Code/Test/InstallerPartTestCase.php

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Webforge\Code\Test;
4+
5+
use Webforge\Setup\Installer\PartsInstaller;
6+
use Webforge\Setup\Installer\Macro;
7+
use Webforge\Setup\Installer\Command;
8+
use Webforge\Framework\Container;
9+
use Psc\System\Dir;
10+
11+
class InstallerPartTestCase extends MacroTestCase {
12+
13+
protected $installer;
14+
protected $container;
15+
16+
public function setUp() {
17+
parent::setUp();
18+
19+
$this->target = Dir::createTemporary();
20+
$this->container = new Container();
21+
$this->container->initLocalPackageFromDirectory(Dir::factoryTS(__DIR__));
22+
23+
$this->installer = new PartsInstaller(array(), $this->container);
24+
}
25+
26+
public function findCopyCmds(Macro $macro) {
27+
$copies = array();
28+
foreach ($macro->getCommands() as $command) {
29+
if ($this->isCmd('Copy', $command)) {
30+
$copies[] = $command;
31+
}
32+
}
33+
return $copies;
34+
}
35+
36+
public function findWriteOrWriteTemplateCmds(Macro $macro) {
37+
$commands = array();
38+
foreach ($macro->getCommands() as $command) {
39+
if ($this->isCmd('Write', $command) || $this->isCmd('WriteTemplate', $command)) {
40+
$commands[] = $command;
41+
}
42+
}
43+
return $commands;
44+
}
45+
46+
public function getCopiedFiles($macro) {
47+
$copiedFiles = array();
48+
foreach ($this->findCopyCmds($macro) as $copy) {
49+
$copiedFiles[(string) $copy->getSource()] = $copy->getDestination()->getUrl($this->target);
50+
51+
$this->assertFileExists($copy->getSource(), 'source does not exist: '. $copy->describe());
52+
}
53+
54+
return $copiedFiles;
55+
}
56+
57+
public function getWrittenFiles($macro) {
58+
$files = array();
59+
foreach ($macro->getCommands() as $write) {
60+
if ($this->isCmd('WriteTemplate', $write)) {
61+
$files[(string) $write->getTemplate()] = $write->getDestination()->getUrl($this->target);
62+
$this->assertFileExists($write->getTemplate(), 'template does not exist: '. $write->describe());
63+
} elseif($this->isCmd('Write', $write)) {
64+
$files[] = $write->getDestination()->getUrl($this->target);
65+
}
66+
}
67+
68+
return $files;
69+
}
70+
71+
public function isCmd($name, Command $command) {
72+
$class = 'Webforge\Setup\Installer\\'.$name.'Cmd';
73+
return $command instanceof $class;
74+
}
75+
76+
public function debugMacro(Macro $macro) {
77+
$x = 0;
78+
$str = '';
79+
foreach ($macro->getCommands() as $command) {
80+
$str .= sprintf("[%s] %s\n", $x, $command->describe());
81+
$x++;
82+
}
83+
84+
return $str;
85+
}
86+
87+
public function tearDown() {
88+
if (isset($this->target)) {
89+
$this->target->delete();
90+
}
91+
}
92+
}
93+
?>

Diff for: lib/Webforge/Code/Test/MacroTestCase.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Webforge\Code\Test;
4+
5+
class MacroTestCase extends Base {
6+
7+
protected $macro;
8+
9+
}
10+
?>

Diff for: lib/Webforge/Common/Command.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Webforge\Common;
4+
5+
/**
6+
* A Command is the abstraction for a part of a program that gets executed
7+
*
8+
*/
9+
interface Command {
10+
11+
const WARNING = 'Webforge.Common.Command.Warning';
12+
13+
/**
14+
* Run the actual command
15+
*/
16+
public function execute();
17+
18+
/**
19+
* Return a textual Representation of the command
20+
*
21+
*/
22+
public function describe();
23+
}
24+
?>

Diff for: lib/Webforge/Common/Macro.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Webforge\Common;
4+
5+
interface Macro {
6+
7+
/**
8+
* @return Command[]
9+
*/
10+
public function getCommands();
11+
12+
13+
public function execute();
14+
15+
}
16+
?>

Diff for: lib/Webforge/Setup/Installer/Command.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Webforge\Setup\Installer;
4+
5+
use Psc\Code\Event\Dispatcher as EventDispatcher;
6+
use Psc\Code\Event\Manager as EventManager;
7+
use Psc\Code\Event\Subscriber as EventSubscriber;
8+
9+
abstract class Command implements \Webforge\Common\Command, EventDispatcher {
10+
11+
const IF_NOT_EXISTS = Installer::IF_NOT_EXISTS;
12+
13+
protected function warn($msg) {
14+
$this->getManager()->dispatchEvent(
15+
self::WARNING,
16+
(object) array('msg'=>$msg),
17+
$this
18+
);
19+
}
20+
21+
public function getManager() {
22+
if (!isset($this->manager)) {
23+
$this->manager = new EventManager();
24+
}
25+
26+
return $this->manager;
27+
}
28+
29+
public function subscribe(EventSubscriber $subscriber, $eventType = NULL) {
30+
$this->getManager()->bind(
31+
$subscriber,
32+
$eventType
33+
);
34+
}
35+
}
36+
?>

Diff for: lib/Webforge/Setup/Installer/ContainerAwarePart.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ abstract class ContainerAwarePart extends Part implements ContainerAware {
1313
*/
1414
protected $container;
1515

16+
// @codeCoverageIgnoreStart
1617
public function setContainer(Container $container) {
1718
$this->container = $container;
1819
return $this;
1920
}
21+
// @codeCoverageIgnoreEnd
2022
}
2123
?>

Diff for: lib/Webforge/Setup/Installer/CopyCmd.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Webforge\Setup\Installer;
4+
5+
use Psc\System\Dir;
6+
use Psc\System\File;
7+
8+
class CopyCmd extends Command {
9+
10+
/**
11+
* @var Psc\System\Dir|Psc\System\File
12+
*/
13+
protected $source;
14+
15+
/**
16+
* @var Psc\System\Dir|Psc\System\File
17+
*/
18+
protected $destination;
19+
20+
/**
21+
* @param Dir|File $source
22+
* @param Dir|File $destination if $destination is a Dir and $source is a file, the a file with $source->getName() will be copied to $destination
23+
*/
24+
public function __construct($source, $destination, $flags = 0x000000) {
25+
$this->source = $source;
26+
$this->destination = $destination;
27+
$this->flags = $flags;
28+
}
29+
30+
public function execute() {
31+
if ($this->source instanceof File && $this->destination instanceof File || $this->source instanceof Dir) {
32+
if (($this->flags & self::IF_NOT_EXISTS) && $this->destination->exists()) {
33+
$this->warn('will not overwrite (per request): '.$this->destination);
34+
35+
return $this;
36+
}
37+
}
38+
39+
$this->source->copy($this->destination);
40+
41+
return $this;
42+
}
43+
44+
public function describe() {
45+
return sprintf("copy '%s' to '%s'", $this->source, $this->destination);
46+
}
47+
48+
public function getSource() {
49+
return $this->source;
50+
}
51+
52+
public function getDestination() {
53+
return $this->destination;
54+
}
55+
}
56+
?>

Diff for: lib/Webforge/Setup/Installer/CreateBootstrapPart.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public function __construct() {
1111
}
1212

1313
public function installTo(Dir $target, Installer $installer) {
14-
$resources = $this->container->getResourceDirectory();
1514

1615
$installer->copy(
17-
$resources->sub('installTemplates/')->getFile('bootstrap.template.php'),
16+
$installer->getInstallTemplates()->getFile('bootstrap.template.php'),
1817
$target->getFile('bootstrap.php'),
1918
Installer::IF_NOT_EXISTS
2019
);
20+
2121
}
2222
}
2323
?>

0 commit comments

Comments
 (0)