1+ <?php
2+
3+
4+ namespace DevCoding \Mac \Command ;
5+
6+
7+ use DevCoding \Command \Base \AbstractMacConsole ;
8+ use DevCoding \Mac \Objects \CreativeCloudApp ;
9+ use Symfony \Component \Console \Input \InputInterface ;
10+ use Symfony \Component \Console \Input \InputOption ;
11+ use Symfony \Component \Console \Output \OutputInterface ;
12+
13+ class AbstractAdobeConsole extends AbstractMacConsole
14+ {
15+ public function configure ()
16+ {
17+ $ this ->addOption ('user ' , 'u ' , InputOption::VALUE_REQUIRED , 'The user to run the command for. Defaults to current user. ' );
18+ }
19+
20+ public function interact (InputInterface $ input , OutputInterface $ output )
21+ {
22+ if ($ input ->hasOption ('user ' ))
23+ {
24+ if ($ user = $ input ->getOption ('user ' ))
25+ {
26+ $ this ->setUser ($ user );
27+ }
28+ }
29+ }
30+
31+ /**
32+ * @param $app
33+ * @param null $year
34+ *
35+ * @return CreativeCloudApp
36+ */
37+ protected function getCreativeCloudApp ($ app , $ year = null )
38+ {
39+ return $ year ? new CreativeCloudApp ($ app , $ year ) : new CreativeCloudApp ($ app );
40+ }
41+
42+ protected function getBackupPath ($ app , $ year = null )
43+ {
44+ if ($ year )
45+ {
46+ return sprintf ("%s/Preferences/Prefer/CC/%s/%s " , $ this ->getUserLibraryDir (), $ app , $ year );
47+ }
48+ else
49+ {
50+ return sprintf ("%s/Preferences/Prefer/CC/%s " , $ this ->getUserLibraryDir (), $ app );
51+ }
52+ }
53+
54+ /**
55+ * @param CreativeCloudApp $ccApp
56+ * @param $dest
57+ *
58+ * @return bool
59+ */
60+ protected function doPreferenceBackup (CreativeCloudApp $ ccApp , $ dest )
61+ {
62+ $ dest = $ dest . '/tmp ' ;
63+ if (!is_dir ($ dest ))
64+ {
65+ if (!file_exists ($ dest ))
66+ {
67+ @mkdir ($ dest , 0777 , true );
68+ }
69+ else
70+ {
71+ throw new \Exception ('Could not create backup destination directory ' );
72+ }
73+ }
74+
75+ $ userDir = $ this ->getUserDir ();
76+ foreach ($ ccApp ->getPreferences () as $ preference )
77+ {
78+ $ path = sprintf ("%s/%s " , $ userDir , $ preference );
79+ if (file_exists ($ path ))
80+ {
81+ $ cmd = sprintf ('rsync -aP --ignore-times "%s" "%s/" ' ,$ path ,$ dest );
82+ exec ($ cmd ,$ output ,$ retval );
83+ if ($ retval !== 0 )
84+ {
85+ throw new \Exception ('An error was encountered backing up preferences: ' );
86+ }
87+ }
88+ }
89+
90+ $ date = date ( 'Ymd-Hi ' );
91+ $ zipFile = sprintf ( 'backup-%s.zip ' , $ date );
92+ $ zipPath = dirname ($ dest ) . '/ ' . $ zipFile ;
93+ $ cmd = sprintf ( 'cd "%s" && zip -r "%s" ./* && cd - ' , $ dest , $ zipPath );
94+ exec ( $ cmd ,$ output , $ retval );
95+ if ( $ retval || !file_exists ( $ zipPath ) )
96+ {
97+ throw new \Exception ( "Could not compress the backup after copying. " );
98+ }
99+ else
100+ {
101+ $ this ->rrmdir ($ dest );
102+ }
103+
104+ return true ;
105+ }
106+
107+ private function rrmdir ($ src )
108+ {
109+ if (is_dir ($ src ))
110+ {
111+ $ inTmp = (strpos ($ src , '/tmp ' ) === 0 && $ src !== '/tmp ' );
112+ $ inUser = (strpos ($ src , $ this ->getUserDir ()) === 0 && $ src !== $ this ->getUserDir ());
113+
114+ if (!$ inTmp && !$ inUser )
115+ {
116+ throw new \Exception (sprintf ('The directory "%s" cannot be deleted with this application. ' ,$ src ));
117+ }
118+
119+ $ dir = opendir ($ src );
120+ while (false !== ( $ file = readdir ($ dir )) ) {
121+ if (( $ file != '. ' ) && ( $ file != '.. ' )) {
122+ $ full = $ src . '/ ' . $ file ;
123+ if ( is_dir ($ full ) ) {
124+ $ this ->rrmdir ($ full );
125+ }
126+ else {
127+ unlink ($ full );
128+ }
129+ }
130+ }
131+ closedir ($ dir );
132+ rmdir ($ src );
133+ }
134+ }
135+
136+ private function rcopy ( $ src , $ dst )
137+ {
138+ if (!is_dir ($ dst ))
139+ {
140+ @mkdir ( $ dst );
141+ }
142+
143+ if (is_dir ($ src ))
144+ {
145+ $ dir = opendir ( $ src );
146+ while ( false !== ( $ file = readdir ( $ dir ) ) )
147+ {
148+ if ( ( $ file != '. ' ) && ( $ file != '.. ' ) )
149+ {
150+ if ( is_dir ( $ src . '/ ' . $ file ) )
151+ {
152+ $ this ->rcopy ( $ src . '/ ' . $ file , $ dst . '/ ' . $ file );
153+ }
154+ else
155+ {
156+ if ( !copy ( $ src . '/ ' . $ file , $ dst . '/ ' . $ file ) )
157+ {
158+ throw new \Exception ( "Could not copy \n" . $ src . "\nto \n" . $ dst );
159+ }
160+ }
161+ }
162+ }
163+ closedir ( $ dir );
164+ }
165+ elseif (is_file ($ src ))
166+ {
167+ copy ($ src , $ dst .'/ ' );
168+ }
169+ }
170+ }
0 commit comments