33namespace IonBazan \ComposerDiff \Command ;
44
55use Composer \Command \BaseCommand ;
6- use Composer \DependencyResolver \Operation \InstallOperation ;
7- use Composer \DependencyResolver \Operation \OperationInterface ;
8- use Composer \DependencyResolver \Operation \UninstallOperation ;
9- use Composer \DependencyResolver \Operation \UpdateOperation ;
10- use IonBazan \ComposerDiff \MarkdownTable ;
6+ use IonBazan \ComposerDiff \Formatter \Formatter ;
7+ use IonBazan \ComposerDiff \Formatter \MarkdownListFormatter ;
8+ use IonBazan \ComposerDiff \Formatter \MarkdownTableFormatter ;
119use IonBazan \ComposerDiff \PackageDiff ;
1210use Symfony \Component \Console \Input \InputInterface ;
1311use Symfony \Component \Console \Input \InputOption ;
@@ -36,6 +34,7 @@ protected function configure()
3634 ->addOption ('no-dev ' , null , InputOption::VALUE_NONE , 'Ignore dev dependencies ' )
3735 ->addOption ('no-prod ' , null , InputOption::VALUE_NONE , 'Ignore prod dependencies ' )
3836 ->addOption ('with-platform ' , 'p ' , InputOption::VALUE_NONE , 'Include platform dependencies (PHP version, extensions, etc.) ' )
37+ ->addOption ('format ' , 'f ' , InputOption::VALUE_REQUIRED , 'Output format (mdtable, mdlist) ' , 'mdtable ' )
3938 ;
4039 }
4140
@@ -44,63 +43,32 @@ protected function execute(InputInterface $input, OutputInterface $output)
4443 $ base = $ input ->getOption ('base ' );
4544 $ target = $ input ->getOption ('target ' );
4645 $ withPlatform = $ input ->getOption ('with-platform ' );
46+ $ formatter = $ this ->getFormatter ($ input , $ output );
4747
4848 if (!$ input ->getOption ('no-prod ' )) {
4949 $ operations = $ this ->packageDiff ->getPackageDiff ($ base , $ target , false , $ withPlatform );
50- $ this -> displayTable ($ operations , 'Prod Packages ' , $ output );
50+ $ formatter -> render ($ operations , 'Prod Packages ' , $ output );
5151 }
5252
5353 if (!$ input ->getOption ('no-dev ' )) {
5454 $ operations = $ this ->packageDiff ->getPackageDiff ($ base , $ target , true , $ withPlatform );
55- $ this -> displayTable ($ operations , 'Dev Packages ' , $ output );
55+ $ formatter -> render ($ operations , 'Dev Packages ' , $ output );
5656 }
5757
5858 return 0 ;
5959 }
6060
61- protected function displayTable (array $ operations , $ header , OutputInterface $ output )
62- {
63- if (!\count ($ operations )) {
64- return ;
65- }
66-
67- $ rows = array ();
68-
69- foreach ($ operations as $ operation ) {
70- $ rows [] = $ this ->getTableRow ($ operation );
71- }
72-
73- $ table = new MarkdownTable ($ output );
74- $ table ->setHeaders (array ($ header , 'Base ' , 'Target ' ))->setRows ($ rows )->render ();
75- $ output ->writeln ('' );
76- }
77-
78- protected function getTableRow (OperationInterface $ operation )
61+ /**
62+ * @return Formatter
63+ */
64+ private function getFormatter (InputInterface $ input , OutputInterface $ output )
7965 {
80- if ($ operation instanceof InstallOperation) {
81- return array (
82- $ operation ->getPackage ()->getName (),
83- 'New ' ,
84- $ operation ->getPackage ()->getFullPrettyVersion (),
85- );
86- }
87-
88- if ($ operation instanceof UpdateOperation) {
89- return array (
90- $ operation ->getInitialPackage ()->getName (),
91- $ operation ->getInitialPackage ()->getFullPrettyVersion (),
92- $ operation ->getTargetPackage ()->getFullPrettyVersion (),
93- );
66+ switch ($ input ->getOption ('format ' )) {
67+ case 'mdlist ' :
68+ return new MarkdownListFormatter ($ output );
69+ case 'mdtable ' :
70+ default :
71+ return new MarkdownTableFormatter ($ output );
9472 }
95-
96- if ($ operation instanceof UninstallOperation) {
97- return array (
98- $ operation ->getPackage ()->getName (),
99- $ operation ->getPackage ()->getFullPrettyVersion (),
100- 'Removed ' ,
101- );
102- }
103-
104- throw new \InvalidArgumentException ('Invalid operation ' );
10573 }
10674}
0 commit comments