99
1010namespace  Youwe \TestingSuite \Composer \Installer ;
1111
12- use  Composer \Factory ;
1312use  Composer \IO \IOInterface ;
14- use  Composer \Json \JsonFile ;
1513use  Exception ;
1614use  Youwe \FileMapping \FileMappingInterface ;
15+ use  Youwe \TestingSuite \Composer \ComposerJsonWriter ;
1716use  Youwe \TestingSuite \Composer \MappingResolver ;
1817
1918/** 
2221 */ 
2322class  ArchiveExcludeInstaller implements  InstallerInterface
2423{
25-     /** @var JsonFile */ 
26-     private  $ file ;
27- 
28-     /** @var MappingResolver */ 
29-     private  $ resolver ;
30- 
31-     /** @var IOInterface */ 
32-     private  $ io ;
33- 
34-     /** @var string */ 
35-     private  $ destination ;
36- 
37-     /** @var array */ 
38-     private  $ defaults  = [
24+     private  const  DEFAULTS  = [
3925        '/docker-compose.yml ' ,
4026        '/examples ' ,
4127        '/example ' ,
@@ -45,27 +31,27 @@ class ArchiveExcludeInstaller implements InstallerInterface
4531        '/tests ' ,
4632    ];
4733
34+     private  readonly  string  $ destination ;
35+     private  readonly  array  $ defaults ;
36+ 
4837    /** 
4938     * Constructor. 
5039     * 
5140     * @param MappingResolver $resolver 
52-      * @param IOInterface      $io 
53-      * @param JsonFile|null   $file  
54-      * @param string |null     $destination 
55-      * @param array|null       $defaults 
41+      * @param IOInterface $io 
42+      * @param ComposerJsonWriter $composerJsonWriter  
43+      * @param string |null $destination 
44+      * @param array|null $defaults 
5645     */ 
5746    public  function  __construct (
58-         MappingResolver   $ resolver ,
59-         IOInterface   $ io ,
60-         ? JsonFile   $ file  =  null ,
47+         private   readonly   MappingResolver   $ resolver ,
48+         private   readonly   IOInterface   $ io ,
49+         private   readonly   ComposerJsonWriter   $ composerJsonWriter  ,
6150        ?string  $ destination  = null ,
6251        ?array  $ defaults  = null ,
6352    ) {
64-         $ this  ->resolver     = $ resolver ;
65-         $ this  ->io           = $ io ;
66-         $ this  ->file         = $ file  ?? new  JsonFile (Factory::getComposerFile ());
6753        $ this  ->destination  = $ destination  ?? getcwd ();
68-         $ this  ->defaults      = $ defaults  ?? $ this -> defaults ;
54+         $ this  ->defaults  = $ defaults  ?? self :: DEFAULTS ;
6955    }
7056
7157    /** 
@@ -76,16 +62,16 @@ public function __construct(
7662     */ 
7763    public  function  install (): void 
7864    {
79-         $ definition  = $ this  ->file -> read ();
80-         $ excluded     = $ definition[ ' archive ' ][ ' exclude ' ]  ?? [];
65+         $ definition  = $ this  ->composerJsonWriter -> getContents ();
66+         $ excluded  = $ definition-> archive -> exclude  ?? [];
8167
8268        $ excluded  = array_map (
8369            function  (string  $ exclude ): string  {
8470                return  substr ($ exclude , 0 , 1 ) !== '/ ' 
8571                    ? '/ '  . $ exclude
8672                    : $ exclude ;
8773            },
88-             $ excluded
74+             $ excluded, 
8975        );
9076
9177        $ files  = array_merge (
@@ -95,27 +81,37 @@ function (FileMappingInterface $mapping): string {
9581                    return  '/ '  . $ mapping ->getRelativeDestination ();
9682                },
9783                iterator_to_array (
98-                     $ this  ->resolver ->resolve ()
84+                     $ this  ->resolver ->resolve (), 
9985                ),
100-             )
86+             ), 
10187        );
10288
89+         $ hasChanges  = false ;
10390        foreach  ($ files  as  $ file ) {
10491            if  (
10592                !in_array ($ file , $ excluded )
10693                && file_exists ($ this  ->destination  . $ file )
10794            ) {
10895                $ excluded [] = $ file ;
96+                 $ hasChanges  = true ;
10997                $ this  ->io ->write (
11098                    sprintf (
11199                        '<info>Added:</info> %s to archive exclude in composer.json ' ,
112100                        $ file ,
113-                     )
101+                     ), 
114102                );
115103            }
116104        }
117105
118-         $ definition ['archive ' ]['exclude ' ] = $ excluded ;
119-         $ this  ->file ->write ($ definition );
106+         if  (!$ hasChanges ) {
107+             return ;
108+         }
109+ 
110+         if  (!isset ($ definition ->archive )) {
111+             $ definition ->archive  = (object ) [];
112+         }
113+         $ definition ->archive ->exclude  = $ excluded ;
114+ 
115+         $ this  ->composerJsonWriter ->setContents ($ definition );
120116    }
121117}
0 commit comments