Skip to content

Commit 495c9fd

Browse files
committed
Merge pull request #159 from AydinHassan/evm-gitignore
EventManager with GitIgnore Listener
2 parents 38a013d + 137e3aa commit 495c9fd

14 files changed

+683
-226
lines changed

src/MagentoHackathon/Composer/Magento/DeployManager.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
namespace MagentoHackathon\Composer\Magento;
1010

11-
1211
use Composer\IO\IOInterface;
1312
use MagentoHackathon\Composer\Magento\Deploy\Manager\Entry;
1413
use MagentoHackathon\Composer\Magento\Deploystrategy\Copy;
14+
use MagentoHackathon\Composer\Magento\Event\EventManager;
15+
use MagentoHackathon\Composer\Magento\Event\PackageDeployEvent;
1516

1617
class DeployManager {
1718

@@ -33,25 +34,36 @@ class DeployManager {
3334
* @var array
3435
*/
3536
protected $sortPriority = array();
36-
37-
38-
public function __construct( IOInterface $io )
37+
38+
/**
39+
* @var EventManager
40+
*/
41+
protected $eventManager;
42+
43+
/**
44+
* @param EventManager $eventManager
45+
*/
46+
public function __construct(EventManager $eventManager)
3947
{
40-
$this->io = $io;
48+
$this->eventManager = $eventManager;
4149
}
42-
43-
44-
public function addPackage( Entry $package )
50+
51+
/**
52+
* @param Entry $package
53+
*/
54+
public function addPackage(Entry $package)
4555
{
4656
$this->packages[] = $package;
4757
}
48-
49-
public function setSortPriority( $priorities )
58+
59+
/**
60+
* @param $priorities
61+
*/
62+
public function setSortPriority($priorities)
5063
{
5164
$this->sortPriority = $priorities;
5265
}
5366

54-
5567
/**
5668
* uses the sortPriority Array to sort the packages.
5769
* Highest priority first.
@@ -83,18 +95,18 @@ function($a, $b)use( $getPriorityValue ){
8395
}
8496
);
8597
}
86-
87-
98+
99+
/**
100+
* Deploy all the queued packages
101+
*/
88102
public function doDeploy()
89103
{
90104
$this->sortPackages();
91105
/** @var Entry $package */
92-
foreach( $this->packages as $package ){
93-
if( $this->io->isDebug() ){
94-
$this->io->write('start magento deploy for '. $package->getPackageName() );
95-
}
106+
foreach($this->packages as $package) {
107+
$this->eventManager->dispatch(new PackageDeployEvent('pre-package-deploy', $package));
96108
$package->getDeployStrategy()->deploy();
109+
$this->eventManager->dispatch(new PackageDeployEvent('post-package-deploy', $package));
97110
}
98111
}
99-
100112
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace MagentoHackathon\Composer\Magento\Event;
4+
5+
use Composer\EventDispatcher\Event;
6+
7+
/**
8+
* Class EventManager
9+
* @package MagentoHackathon\Composer\Magento\Event
10+
* @author Aydin Hassan <[email protected]>
11+
*/
12+
class EventManager
13+
{
14+
/**
15+
* @var array
16+
*/
17+
private $listeners = array();
18+
19+
/**
20+
* @param string $event
21+
* @param callable $callback
22+
*/
23+
public function listen($event, $callback)
24+
{
25+
if (!is_callable($callback)) {
26+
throw new \InvalidArgumentException(sprintf(
27+
'Second argument should be a callable. Got: "%s"',
28+
is_object($callback) ? get_class($callback) : gettype($callback)
29+
));
30+
}
31+
32+
if (!isset($this->listeners[$event])) {
33+
$this->listeners[$event] = array($callback);
34+
} else {
35+
$this->listeners[$event][] = $callback;
36+
}
37+
}
38+
39+
/**
40+
* @param Event $event
41+
*/
42+
public function dispatch(Event $event)
43+
{
44+
if (!isset($this->listeners[$event->getName()])) {
45+
return;
46+
}
47+
48+
foreach ($this->listeners[$event->getName()] as $listener) {
49+
call_user_func_array($listener, array($event));
50+
}
51+
}
52+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace MagentoHackathon\Composer\Magento\Event;
4+
5+
use Composer\EventDispatcher\Event;
6+
use MagentoHackathon\Composer\Magento\Deploy\Manager\Entry;
7+
8+
/**
9+
* Class PackageDeployEvent
10+
* @package MagentoHackathon\Composer\Magento\Event
11+
* @author Aydin Hassan <[email protected]>
12+
*/
13+
class PackageDeployEvent extends Event
14+
{
15+
/**
16+
* @var Entry
17+
*/
18+
protected $deployEntry;
19+
20+
/**
21+
* @param string $name
22+
* @param Entry $deployEntry
23+
*/
24+
public function __construct($name, Entry $deployEntry)
25+
{
26+
parent::__construct($name);
27+
$this->deployEntry = $deployEntry;
28+
}
29+
30+
/**
31+
* @return Entry
32+
*/
33+
public function getDeployEntry()
34+
{
35+
return $this->deployEntry;
36+
}
37+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace MagentoHackathon\Composer\Magento;
4+
5+
/**
6+
* Class GitIgnore
7+
* @package MagentoHackathon\Composer\Magento
8+
* @author Aydin Hassan <[email protected]>
9+
*/
10+
class GitIgnore
11+
{
12+
/**
13+
* @var array
14+
*/
15+
protected $lines = array();
16+
17+
/**
18+
* @var string|null
19+
*/
20+
protected $gitIgnoreLocation;
21+
22+
/**
23+
* @var bool
24+
*/
25+
protected $hasChanges = false;
26+
27+
/**
28+
* @param string $fileLocation
29+
*/
30+
public function __construct($fileLocation)
31+
{
32+
$this->gitIgnoreLocation = $fileLocation;
33+
if (file_exists($fileLocation)) {
34+
$this->lines = array_flip(file($fileLocation, FILE_IGNORE_NEW_LINES));
35+
}
36+
}
37+
38+
/**
39+
* @param string $file
40+
*/
41+
public function addEntry($file)
42+
{
43+
if (!isset($this->lines[$file])) {
44+
$this->lines[$file] = $file;
45+
}
46+
$this->hasChanges = true;
47+
}
48+
49+
/**
50+
* @param array $files
51+
*/
52+
public function addMultipleEntries(array $files)
53+
{
54+
foreach ($files as $file) {
55+
$this->addEntry($file);
56+
}
57+
}
58+
59+
/**
60+
* @param string $file
61+
*/
62+
public function removeEntry($file)
63+
{
64+
if (isset($this->lines[$file])) {
65+
unset($this->lines[$file]);
66+
$this->hasChanges = true;
67+
}
68+
}
69+
70+
/**
71+
* @param array $files
72+
*/
73+
public function removeMultipleEntries(array $files)
74+
{
75+
foreach ($files as $file) {
76+
$this->removeEntry($file);
77+
}
78+
}
79+
80+
/**
81+
* @return array
82+
*/
83+
public function getEntries()
84+
{
85+
return array_values(array_flip($this->lines));
86+
}
87+
88+
/**
89+
* Write the file
90+
*/
91+
public function write()
92+
{
93+
if ($this->hasChanges) {
94+
file_put_contents($this->gitIgnoreLocation, implode("\n", array_flip($this->lines)));
95+
}
96+
}
97+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace MagentoHackathon\Composer\Magento;
4+
5+
use MagentoHackathon\Composer\Magento\Event\PackageDeployEvent;
6+
7+
/**
8+
* Class GitIgnoreListener
9+
* @package MagentoHackathon\Composer\Magento
10+
* @author Aydin Hassan <[email protected]>
11+
*/
12+
class GitIgnoreListener
13+
{
14+
15+
/**
16+
* @var GitIgnore
17+
*/
18+
protected $gitIgnore;
19+
20+
/**
21+
* @param GitIgnore $gitIgnore
22+
*/
23+
public function __construct(GitIgnore $gitIgnore)
24+
{
25+
$this->gitIgnore = $gitIgnore;
26+
}
27+
28+
/**
29+
* Add any files which were deployed to the .gitignore
30+
* Remove any files which were removed to the .gitignore
31+
*
32+
* @param PackageDeployEvent $packageDeployEvent
33+
*/
34+
public function __invoke(PackageDeployEvent $packageDeployEvent)
35+
{
36+
$this->gitIgnore->addMultipleEntries(
37+
$packageDeployEvent->getDeployEntry()->getDeployStrategy()->getDeployedFiles()
38+
);
39+
40+
$this->gitIgnore->removeMultipleEntries(
41+
$packageDeployEvent->getDeployEntry()->getDeployStrategy()->getRemovedFiles()
42+
);
43+
44+
$this->gitIgnore->write();
45+
}
46+
}

0 commit comments

Comments
 (0)