Skip to content

Commit a1a5849

Browse files
author
alexandresalome
committed
remove event dispatcher, replace with logger
1 parent 99eda13 commit a1a5849

File tree

9 files changed

+30
-193
lines changed

9 files changed

+30
-193
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
},
2424

2525
"require": {
26-
"symfony/process": "<3",
27-
"symfony/event-dispatcher": "<3"
26+
"symfony/process": "<3"
2827
},
2928
"require-dev": {
3029
"psr/log": "~1"

doc/api/repository.rst

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,25 @@ Your ``HEAD`` can be attached (using a reference) or detached (using a commit).
4949
echo "Sorry man\n";
5050
}
5151
52-
Event dispatcher
53-
----------------
52+
Logger
53+
------
5454

55-
Each repository has an event dispatcher. You can listen to ``pre_command`` and
56-
``post_command`` using method *addListener* on *Repository*:
55+
If you are developing, you may appreciate to have a logger inside repository, telling
56+
you every executed command.
57+
58+
To do so, inject a logger inside the Repository:
5759

5860
.. code-block:: php
5961
60-
$repository->addListener(Events::POST_COMMAND, function ($event) {
61-
echo 'Command: '.$event->getCommand().' '.implode(' ', $event->getArgs())."\n";
62-
echo 'Execution time: '.round($event->getDuration(), 2).'s';
63-
});
62+
$repository->setLogger(new Monolog\Logger('repository'));
63+
64+
$repository->run('fetch', array('--all'));
65+
66+
This will output:
67+
68+
.. code-block:: text
6469
65-
$repository->getReferences()->getBranch('master')->getCommit()->getMessage();
70+
info run command: fetch "--all"
71+
debug last command (fetch) duration: 23.24ms
72+
debug last command (fetch) return code: 0
73+
debug last command (fetch) output: Fetching origin

src/Gitonomy/Git/Event/CommandEvent.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/Gitonomy/Git/Event/Events.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Gitonomy/Git/Event/PostCommandEvent.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Gitonomy/Git/Event/PreCommandEvent.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Gitonomy/Git/Repository.php

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@
1212

1313
namespace Gitonomy\Git;
1414

15-
use Symfony\Component\EventDispatcher\EventDispatcher;
1615
use Symfony\Component\Process\ProcessBuilder;
1716
use Symfony\Component\Process\Process;
1817
use Psr\Log\LoggerInterface;
1918

20-
use Gitonomy\Git\Event\Events;
21-
use Gitonomy\Git\Event\PreCommandEvent;
22-
use Gitonomy\Git\Event\PostCommandEvent;
2319
use Gitonomy\Git\Exception\RuntimeException;
2420

2521
/**
@@ -98,8 +94,6 @@ public function __construct($dir, $workingDir = null, LoggerInterface $logger =
9894
$this->objects = array();
9995

10096
$this->logger = $logger;
101-
102-
$this->eventDispatcher = new EventDispatcher();
10397
}
10498

10599
public function isBare()
@@ -355,35 +349,25 @@ public function run($command, $args = array())
355349
{
356350
$process = $this->getProcess($command, $args);
357351

358-
if ($this->eventDispatcher->hasListeners()) {
359-
$event = new PreCommandEvent($process, $command, $args);
360-
$this->eventDispatcher->dispatch(Events::PRE_COMMAND, $event);
361-
}
362-
363352
if ($this->logger) {
364-
$this->logger->info(sprintf('run command: "%", args: "%s"', $command, print_r($args, true)));
353+
$this->logger->info(sprintf('run command: %s "%s" ', $command, implode('", "', $args)));
365354
}
366355

367356
$before = microtime(true);
368357
$process->run();
369358
$duration = microtime(true) - $before;
370359

371-
if ($this->eventDispatcher->hasListeners()) {
372-
$event = new PostCommandEvent($process, $command, $args);
373-
$event->setDuration($duration);
374-
$this->eventDispatcher->dispatch(Events::POST_COMMAND, $event);
375-
}
376-
377360
$output = $process->getOutput();
378361

379362
if ($this->logger) {
380-
$this->logger->debug(sprintf('last command ("%s") return code: "%s"', $command, $process->getExitCode()));
381-
$this->logger->debug(sprintf('last command ("%s") output: "%s"', $command, $output));
363+
$this->logger->debug(sprintf('last command (%s) duration: %sms', $command, sprintf('%.2f', $duration*1000)));
364+
$this->logger->debug(sprintf('last command (%s) return code: %s', $command, $process->getExitCode()));
365+
$this->logger->debug(sprintf('last command (%s) output: %s', $command, $output));
382366
}
383367

384368
if (!$process->isSuccessful()) {
385369
if ($this->logger) {
386-
$this->logger->error(sprintf('last command ("%s") error output: "%s"', $command, $process->getErrorOutput()));
370+
$this->logger->error(sprintf('last command (%s) error output: "%s"', $command, $process->getErrorOutput()));
387371
}
388372

389373
throw new RuntimeException($process);
@@ -392,11 +376,6 @@ public function run($command, $args = array())
392376
return $output;
393377
}
394378

395-
public function addListener($eventName, $listener, $priority = 0)
396-
{
397-
$this->eventDispatcher->addListener($eventName, $listener, $priority);
398-
}
399-
400379
/**
401380
* @return WorkingCopy
402381
*/

tests/Gitonomy/Git/Tests/AbstractTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ public static function provideFoobar()
6161
public static function createFoobarRepository($bare = true)
6262
{
6363
if (null === self::$localRepository) {
64-
self::$localRepository = Admin::cloneTo(self::createTempDir(), self::REPOSITORY_URL);
65-
self::registerDeletion(self::$localRepository);
64+
if (is_dir('/tmp/foobar_test')) {
65+
self::$localRepository = new Repository('/tmp/foobar_test');
66+
} else {
67+
self::$localRepository = Admin::cloneTo('/tmp/foobar_test', self::REPOSITORY_URL);
68+
}
6669
}
6770

6871
$repository = self::$localRepository->cloneTo(self::createTempDir(), $bare);

tests/Gitonomy/Git/Tests/RepositoryTest.php

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
use Gitonomy\Git\Repository;
1616
use Gitonomy\Git\Blob;
17-
use Gitonomy\Git\Event\Events;
18-
use Gitonomy\Git\Event\PreCommandEvent;
19-
use Gitonomy\Git\Event\PostCommandEvent;
2017

2118
class RepositoryTest extends AbstractTest
2219
{
@@ -49,56 +46,6 @@ public function testIsBare()
4946
$this->assertFalse($notBare->isBare(), "Working copy is not bare");
5047
}
5148

52-
/**
53-
* @dataProvider provideFoobar
54-
*/
55-
public function testEventDispatcher_Basis($repository)
56-
{
57-
$test = $this;
58-
59-
$before = false;
60-
$repository->addListener(Events::PRE_COMMAND, function ($event) use ($test, &$before) {
61-
$test->assertTrue($event instanceof PreCommandEvent);
62-
$test->assertEquals('remote', $event->getCommand(), "command is remote");
63-
$test->assertEquals(array('-v'), $event->getArgs(), "args is -v");
64-
$before = true;
65-
});
66-
67-
$after = false;
68-
$repository->addListener(Events::POST_COMMAND, function ($event) use ($test, &$after) {
69-
$test->assertTrue($event instanceof PostCommandEvent);
70-
$test->assertEquals('remote', $event->getCommand(), "command is remote");
71-
$test->assertEquals(array('-v'), $event->getArgs(), "args is -v");
72-
$after = true;
73-
});
74-
75-
$repository->run('remote', array('-v'));
76-
77-
$this->assertTrue($before, "pre-command called");
78-
$this->assertTrue($after, "post-command called");
79-
}
80-
81-
/**
82-
* @dataProvider provideFoobar
83-
*/
84-
public function testEventDispatcher_Error($repository)
85-
{
86-
$test = $this;
87-
88-
$after = false;
89-
$repository->addListener(Events::POST_COMMAND, function ($event) use ($test, &$after) {
90-
$after = true;
91-
});
92-
93-
try {
94-
$repository->run('foobar');
95-
$this->fail("expected exception on invalid command");
96-
} catch (\RuntimeException $e) {
97-
}
98-
99-
$this->assertTrue($after, "post-command called");
100-
}
101-
10249
/**
10350
* @dataProvider provideFoobar
10451
*/
@@ -114,7 +61,7 @@ public function testLoggerOk($repository)
11461
->method('info')
11562
;
11663
$logger
117-
->expects($this->exactly(2))
64+
->expects($this->exactly(3)) // duration, return code and output
11865
->method('debug')
11966
;
12067

@@ -139,7 +86,7 @@ public function testLoggerNOk($repository)
13986
->method('info')
14087
;
14188
$logger
142-
->expects($this->exactly(2))
89+
->expects($this->exactly(3)) // duration, return code and output
14390
->method('debug')
14491
;
14592
$logger

0 commit comments

Comments
 (0)