This repository was archived by the owner on Aug 14, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +75
-2
lines changed Expand file tree Collapse file tree 6 files changed +75
-2
lines changed Original file line number Diff line number Diff line change 1414* Added ` File/_config.yml ` file.
1515* Added ` File/.travis.yml ` file.
1616
17+ * Added ` Josantonius\File\Test\FileTest::CopyDirRecursively() ` method.
18+
1719* Deleted ` Josantonius\File\Tests\FileTest::testSearchString() ` method.
1820
1921* Deleted ` Josantonius\File\Tests\FileTest ` class.
Original file line number Diff line number Diff line change @@ -60,6 +60,7 @@ File::exists();
6060File::delete();
6161File::createDir();
6262File::deleteEmptyDir();
63+ File::CopyDirRecursively();
6364File::deleteDirRecursively();
6465File::getFilesFromDir();
6566```
Original file line number Diff line number Diff line change @@ -60,6 +60,7 @@ File::exists();
6060File::delete();
6161File::createDir();
6262File::deleteEmptyDir();
63+ File::CopyDirRecursively();
6364File::deleteDirRecursively();
6465File::getFilesFromDir();
6566```
Original file line number Diff line number Diff line change @@ -87,6 +87,40 @@ public static function createDir($path) {
8787 return (!is_dir ($ path ) && @mkdir ($ path , 0777 , true ));
8888 }
8989
90+ /**
91+ * Copy directory recursively.
92+ *
93+ * @since 1.1.4
94+ *
95+ * @param string $fromPath → path from copy
96+ * @param string $toPath → path to copy
97+ *
98+ * @return boolean
99+ */
100+ public static function CopyDirRecursively ($ from , $ to ) {
101+
102+ if (!$ path = self ::getFilesFromDir ($ from )) { return false ; }
103+
104+ self ::createDir ($ to = rtrim ($ to , '/ ' ) . '/ ' );
105+
106+ foreach ($ path as $ file ) {
107+
108+ if ($ file ->isFile ()) {
109+
110+ if (!copy ($ file ->getRealPath (), $ to .$ file ->getFilename ())) {
111+
112+ return false ;
113+ }
114+
115+ } else if (!$ file ->isDot () && $ file ->isDir ()) {
116+
117+ self ::CopyDirRecursively ($ file ->getRealPath (), $ to . $ path );
118+ }
119+ }
120+
121+ return true ;
122+ }
123+
90124 /**
91125 * Delete empty directory.
92126 *
Original file line number Diff line number Diff line change @@ -174,22 +174,57 @@ public function testDeleteEmptyDirError() {
174174 }
175175
176176 /**
177- * Test delete directory recursively.
177+ * Test copy directory recursively.
178178 *
179179 * @since 1.1.4
180180 *
181181 * @return void
182182 */
183- public function testDeleteDirRecursively () {
183+ public function testCopyDirRecursively () {
184184
185185 File::createDir (__DIR__ . '/test/test/test/ ' );
186186
187187 touch (__DIR__ . '/test/test/test/test.txt ' );
188188
189+ $ this ->assertTrue (
190+
191+ File::copyDirRecursively (__DIR__ . '/test/ ' , __DIR__ . '/copy/ ' )
192+ );
193+ }
194+
195+ /**
196+ * Test copy missing directory recursively.
197+ *
198+ * @since 1.1.4
199+ *
200+ * @return void
201+ */
202+ public function testCopyMissingDirRecursively () {
203+
204+ $ this ->assertFalse (
205+
206+ File::deleteDirRecursively (__DIR__ . '/unknown/ ' )
207+ );
208+ }
209+
210+ /**
211+ * Test delete directory recursively.
212+ *
213+ * @since 1.1.4
214+ *
215+ * @return void
216+ */
217+ public function testDeleteDirRecursively () {
218+
189219 $ this ->assertTrue (
190220
191221 File::deleteDirRecursively (__DIR__ . '/test/ ' )
192222 );
223+
224+ $ this ->assertTrue (
225+
226+ File::deleteDirRecursively (__DIR__ . '/copy/ ' )
227+ );
193228 }
194229
195230 /**
You can’t perform that action at this time.
0 commit comments