diff options
author | Nathan Guse <nathaniel.guse@gmail.com> | 2012-07-29 20:08:30 -0500 |
---|---|---|
committer | Unknown Bliss <m@michaelcullum.com> | 2012-09-01 15:05:49 +0100 |
commit | 36465c9a205c356b0662e45b4fded79c4b476547 (patch) | |
tree | 5be0d398e3e29fe2b701fb8c9f79c4a959aa485b | |
parent | 500879520c40a71f0b83799ab3e59c86c12a801a (diff) | |
download | forums-36465c9a205c356b0662e45b4fded79c4b476547.tar forums-36465c9a205c356b0662e45b4fded79c4b476547.tar.gz forums-36465c9a205c356b0662e45b4fded79c4b476547.tar.bz2 forums-36465c9a205c356b0662e45b4fded79c4b476547.tar.xz forums-36465c9a205c356b0662e45b4fded79c4b476547.zip |
[ticket/10631] Functional acp_extensions test, cleanup
PHPBB3-10631
-rw-r--r-- | phpBB/adm/style/acp_ext_list.html | 4 | ||||
-rw-r--r-- | phpBB/includes/acp/acp_extensions.php | 38 | ||||
-rw-r--r-- | tests/extension/acp.php | 226 | ||||
-rw-r--r-- | tests/extension/ext/foo/composer.json | 2 | ||||
-rw-r--r-- | tests/extension/ext/vendor/moo/composer.json | 22 | ||||
-rw-r--r-- | tests/extension/metadata_manager_test.php | 99 | ||||
-rw-r--r-- | tests/test_framework/phpbb_functional_test_case.php | 54 | ||||
-rw-r--r-- | tests/test_framework/phpbb_test_case_helpers.php | 108 |
8 files changed, 481 insertions, 72 deletions
diff --git a/phpBB/adm/style/acp_ext_list.html b/phpBB/adm/style/acp_ext_list.html index 65051cbae0..b654a80caa 100644 --- a/phpBB/adm/style/acp_ext_list.html +++ b/phpBB/adm/style/acp_ext_list.html @@ -23,7 +23,7 @@ </td> </tr> <!-- BEGIN enabled --> - <tr> + <tr class="ext_enabled"> <td><strong>{enabled.EXT_NAME}</strong></a></td> <td style="text-align: center;"><a href="{enabled.U_DETAILS}">{L_DETAILS}</a></td> <td style="text-align: center;"> @@ -41,7 +41,7 @@ <td class="row3" colspan="3"><strong>{L_DISABLED} {L_EXTENSIONS}</strong></td> </tr> <!-- BEGIN disabled --> - <tr> + <tr class="ext_disabled"> <td><strong>{disabled.EXT_NAME}</strong></a></td> <td style="text-align: center;"> <!-- IF disabled.U_DETAILS --><a href="{disabled.U_DETAILS}">{L_DETAILS}</a><!-- ENDIF --> diff --git a/phpBB/includes/acp/acp_extensions.php b/phpBB/includes/acp/acp_extensions.php index 4f59ea309b..c4d9f0c0e0 100644 --- a/phpBB/includes/acp/acp_extensions.php +++ b/phpBB/includes/acp/acp_extensions.php @@ -72,7 +72,7 @@ class acp_extensions break; case 'enable_pre': - if (!$md_manager->validate_enable()) + if (!$md_manager->validate_enable() || $phpbb_extension_manager->enabled($ext_name)) { trigger_error('EXTENSION_NOT_AVAILABLE'); } @@ -81,7 +81,7 @@ class acp_extensions $template->assign_vars(array( 'PRE' => true, - 'U_ENABLE' => $this->u_action . '&action=enable&ext_name=' . $ext_name, + 'U_ENABLE' => $this->u_action . '&action=enable&ext_name=' . urlencode($ext_name), )); break; @@ -95,7 +95,7 @@ class acp_extensions { $template->assign_var('S_NEXT_STEP', true); - meta_refresh(0, $this->u_action . '&action=enable&ext_name=' . $ext_name); + meta_refresh(0, $this->u_action . '&action=enable&ext_name=' . urlencode($ext_name)); } $this->tpl_name = 'acp_ext_enable'; @@ -106,11 +106,16 @@ class acp_extensions break; case 'disable_pre': + if (!$phpbb_extension_manager->enabled($ext_name)) + { + trigger_error('EXTENSION_NOT_AVAILABLE'); + } + $this->tpl_name = 'acp_ext_disable'; $template->assign_vars(array( 'PRE' => true, - 'U_DISABLE' => $this->u_action . '&action=disable&ext_name=' . $ext_name, + 'U_DISABLE' => $this->u_action . '&action=disable&ext_name=' . urlencode($ext_name), )); break; @@ -119,7 +124,7 @@ class acp_extensions { $template->assign_var('S_NEXT_STEP', true); - meta_refresh(0, $this->u_action . '&action=disable&ext_name=' . $ext_name); + meta_refresh(0, $this->u_action . '&action=disable&ext_name=' . urlencode($ext_name)); } $this->tpl_name = 'acp_ext_disable'; @@ -134,7 +139,7 @@ class acp_extensions $template->assign_vars(array( 'PRE' => true, - 'U_PURGE' => $this->u_action . '&action=purge&ext_name=' . $ext_name, + 'U_PURGE' => $this->u_action . '&action=purge&ext_name=' . urlencode($ext_name), )); break; @@ -143,7 +148,7 @@ class acp_extensions { $template->assign_var('S_NEXT_STEP', true); - meta_refresh(0, $this->u_action . '&action=purge&ext_name=' . $ext_name); + meta_refresh(0, $this->u_action . '&action=purge&ext_name=' . urlencode($ext_name)); } $this->tpl_name = 'acp_ext_purge'; @@ -166,7 +171,6 @@ class acp_extensions * Lists all the enabled extensions and dumps to the template * * @param $phpbb_extension_manager An instance of the extension manager - * @param $template An instance of the template engine * @return null */ public function list_enabled_exts(phpbb_extension_manager $phpbb_extension_manager) @@ -180,12 +184,12 @@ class acp_extensions $this->template->assign_block_vars('enabled', array( 'EXT_NAME' => $md_manager->get_metadata('display-name'), - 'U_DETAILS' => $this->u_action . '&action=details&ext_name=' . $name, + 'U_DETAILS' => $this->u_action . '&action=details&ext_name=' . urlencode($name), )); $this->output_actions('enabled', array( - 'DISABLE' => $this->u_action . '&action=disable_pre&ext_name=' . $name, - 'PURGE' => $this->u_action . '&action=purge_pre&ext_name=' . $name, + 'DISABLE' => $this->u_action . '&action=disable_pre&ext_name=' . urlencode($name), + 'PURGE' => $this->u_action . '&action=purge_pre&ext_name=' . urlencode($name), )); } catch(phpbb_extension_exception $e) @@ -201,7 +205,6 @@ class acp_extensions * Lists all the disabled extensions and dumps to the template * * @param $phpbb_extension_manager An instance of the extension manager - * @param $template An instance of the template engine * @return null */ public function list_disabled_exts(phpbb_extension_manager $phpbb_extension_manager) @@ -215,12 +218,12 @@ class acp_extensions $this->template->assign_block_vars('disabled', array( 'EXT_NAME' => $md_manager->get_metadata('display-name'), - 'U_DETAILS' => $this->u_action . '&action=details&ext_name=' . $name, + 'U_DETAILS' => $this->u_action . '&action=details&ext_name=' . urlencode($name), )); $this->output_actions('disabled', array( - 'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . $name, - 'PURGE' => $this->u_action . '&action=purge_pre&ext_name=' . $name, + 'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . urlencode($name), + 'PURGE' => $this->u_action . '&action=purge_pre&ext_name=' . urlencode($name), )); } catch(phpbb_extension_exception $e) @@ -236,7 +239,6 @@ class acp_extensions * Lists all the available extensions and dumps to the template * * @param $phpbb_extension_manager An instance of the extension manager - * @param $template An instance of the template engine * @return null */ public function list_available_exts(phpbb_extension_manager $phpbb_extension_manager) @@ -252,11 +254,11 @@ class acp_extensions $this->template->assign_block_vars('disabled', array( 'EXT_NAME' => $md_manager->get_metadata('display-name'), - 'U_DETAILS' => $this->u_action . '&action=details&ext_name=' . $name, + 'U_DETAILS' => $this->u_action . '&action=details&ext_name=' . urlencode($name), )); $this->output_actions('disabled', array( - 'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . $name, + 'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . urlencode($name), )); } catch(phpbb_extension_exception $e) diff --git a/tests/extension/acp.php b/tests/extension/acp.php new file mode 100644 index 0000000000..c078a3f7b4 --- /dev/null +++ b/tests/extension/acp.php @@ -0,0 +1,226 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class acp_test extends phpbb_functional_test_case +{ + static private $copied_files = array(); + static private $helper; + + /** + * This should only be called once before the tests are run. + * This is used to copy the extensions to the phpBB install + */ + static public function setUpBeforeClass() + { + global $phpbb_root_path; + + parent::setUpBeforeClass(); + + self::$helper = new phpbb_test_case_helpers(self); + + // First, move any extensions setup on the board to a temp directory + self::$copied_files = self::$helper->copy_dir($phpbb_root_path . 'ext/', $phpbb_root_path . 'store/temp_ext/'); + + // Then empty the ext/ directory on the board (for accurate test cases) + self::$helper->empty_dir($phpbb_root_path . 'ext/'); + + // Copy our ext/ files from the test case to the board + self::$copied_files = array_merge(self::$copied_files, self::$helper->copy_dir(dirname(__FILE__) . '/ext/', $phpbb_root_path . 'ext/')); + } + + public function setUp() + { + parent::setUp(); + + $this->get_db(); + + // Clear the phpbb_ext table + $this->db->sql_query('DELETE FROM phpbb_ext'); + + // Insert our base data + $insert_rows = array( + array( + 'ext_name' => 'foo', + 'ext_active' => true, + 'ext_state' => 'b:0;', + ), + array( + 'ext_name' => 'vendor/moo', + 'ext_active' => false, + 'ext_state' => 'b:0;', + ), + + // do not exist + array( + 'ext_name' => 'test2', + 'ext_active' => true, + 'ext_state' => 'b:0;', + ), + array( + 'ext_name' => 'test3', + 'ext_active' => false, + 'ext_state' => 'b:0;', + ), + ); + $this->db->sql_multi_insert('phpbb_ext', $insert_rows); + + $this->login(); + $this->admin_login(); + + $this->add_lang('acp/extensions'); + } + + /** + * This should only be called once after the tests are run. + * This is used to remove the files copied to the phpBB install + */ + static public function tearDownAfterClass() + { + global $phpbb_root_path; + + // Copy back the board installed extensions from the temp directory + self::$helper->copy_dir($phpbb_root_path . 'store/temp_ext/', $phpbb_root_path . 'ext/'); + + self::$copied_files[] = $phpbb_root_path . 'store/temp_ext/'; + + // Remove all of the files we copied around (from board ext -> temp_ext, from test ext -> board ext) + self::$helper->remove_files(self::$copied_files); + } + + public function test_list() + { + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&sid=' . $this->sid); + + $this->assertCount(1, $crawler->filter('.ext_enabled')); + $this->assertCount(4, $crawler->filter('.ext_disabled')); + + $this->assertContains('phpBB Foo Extension', $crawler->filter('.ext_enabled')->eq(0)->text()); + $this->assertContainsLang('PURGE', $crawler->filter('.ext_enabled')->eq(0)->text()); + + $this->assertContains('The "test2" extension is not valid.', $crawler->filter('.ext_disabled')->eq(0)->text()); + + $this->assertContains('The "test3" extension is not valid.', $crawler->filter('.ext_disabled')->eq(1)->text()); + + $this->assertContains('phpBB Moo Extension', $crawler->filter('.ext_disabled')->eq(2)->text()); + $this->assertContainsLang('DETAILS', $crawler->filter('.ext_disabled')->eq(2)->text()); + $this->assertContainsLang('ENABLE', $crawler->filter('.ext_disabled')->eq(2)->text()); + $this->assertContainsLang('PURGE', $crawler->filter('.ext_disabled')->eq(2)->text()); + + $this->assertContains('The "bar" extension is not valid.', $crawler->filter('.ext_disabled')->eq(3)->text()); + } + + public function test_details() + { + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=details&ext_name=foo&sid=' . $this->sid); + + for ($i = 0; $i < $crawler->filter('dl')->count(); $i++) + { + $text = $crawler->filter('dl')->eq($i)->text(); + + switch (true) + { + case (strpos($text, $this->lang('DISPLAY_NAME')) === 0): + $this->assertContains('phpBB Foo Extension', $text); + break; + + case (strpos($text, $this->lang('CLEAN_NAME')) === 0): + $this->assertContains('foo/example', $text); + break; + + case (strpos($text, $this->lang('DESCRIPTION')) === 0): + $this->assertContains('An example/sample extension to be used for testing purposes in phpBB Development.', $text); + break; + + case (strpos($text, $this->lang('VERSION')) === 0): + $this->assertContains('1.0.0', $text); + break; + + case (strpos($text, $this->lang('TIME')) === 0): + $this->assertContains('2012-02-15 01:01:01', $text); + break; + + case (strpos($text, $this->lang('LICENCE')) === 0): + $this->assertContains('GNU GPL v2', $text); + break; + + case (strpos($text, $this->lang('PHPBB_VERSION')) === 0): + $this->assertContains('3.1.0-dev', $text); + break; + + case (strpos($text, $this->lang('PHP_VERSION')) === 0): + $this->assertContains('>=5.3', $text); + break; + + case (strpos($text, $this->lang('AUTHOR_NAME')) === 0): + $this->assertContains('Nathan Guse', $text); + break; + + case (strpos($text, $this->lang('AUTHOR_EMAIL')) === 0): + $this->assertContains('email@phpbb.com', $text); + break; + + case (strpos($text, $this->lang('AUTHOR_HOMEPAGE')) === 0): + $this->assertContains('http://lithiumstudios.org', $text); + break; + + case (strpos($text, $this->lang('AUTHOR_ROLE')) === 0): + $this->assertContains('N/A', $text); + break; + } + } + } + + public function test_enable_pre() + { + // Foo is already enabled (error) + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=enable_pre&ext_name=foo&sid=' . $this->sid); + $this->assertContainsLang('EXTENSION_NOT_AVAILABLE', $crawler->filter('html')->text()); + + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=enable_pre&ext_name=vendor%2Fmoo&sid=' . $this->sid); + $this->assertContainsLang('ENABLE_CONFIRM', $crawler->filter('html')->text()); + } + + public function test_disable_pre() + { + // Moo is not enabled (error) + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=disable_pre&ext_name=vendor%2Fmoo&sid=' . $this->sid); + $this->assertContainsLang('EXTENSION_NOT_AVAILABLE', $crawler->filter('html')->text()); + + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=disable_pre&ext_name=foo&sid=' . $this->sid); + $this->assertContainsLang('DISABLE_CONFIRM', $crawler->filter('html')->text()); + } + + public function test_purge_pre() + { + // test2 is not available (error) + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=purge_pre&ext_name=test2&sid=' . $this->sid); + $this->assertContains('The required file does not exist', $crawler->filter('html')->text()); + + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=purge_pre&ext_name=foo&sid=' . $this->sid); + $this->assertContainsLang('PURGE_CONFIRM', $crawler->filter('html')->text()); + } + + public function test_enable() + { + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=enable&ext_name=vendor%2Fmoo&sid=' . $this->sid); + $this->assertContainsLang('ENABLE_SUCCESS', $crawler->filter('html')->text()); + } + + public function test_disable() + { + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=disable&ext_name=vendor%2Fmoo&sid=' . $this->sid); + $this->assertContainsLang('DISABLE_SUCCESS', $crawler->filter('html')->text()); + } + + public function test_purge() + { + $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=purge&ext_name=vendor%2Fmoo&sid=' . $this->sid); + $this->assertContainsLang('PURGE_SUCCESS', $crawler->filter('html')->text()); + } +}
\ No newline at end of file diff --git a/tests/extension/ext/foo/composer.json b/tests/extension/ext/foo/composer.json index 14af677dac..4b5150461f 100644 --- a/tests/extension/ext/foo/composer.json +++ b/tests/extension/ext/foo/composer.json @@ -8,7 +8,7 @@ "authors": [{ "name": "Nathan Guse", "username": "EXreaction", - "email": "nathaniel.guse@gmail.com", + "email": "email@phpbb.com", "homepage": "http://lithiumstudios.org", "role": "N/A" }], diff --git a/tests/extension/ext/vendor/moo/composer.json b/tests/extension/ext/vendor/moo/composer.json new file mode 100644 index 0000000000..c91a5e027b --- /dev/null +++ b/tests/extension/ext/vendor/moo/composer.json @@ -0,0 +1,22 @@ +{ + "name": "moo/example", + "type": "phpbb3-extension", + "description": "An example/sample extension to be used for testing purposes in phpBB Development.", + "version": "1.0.0", + "time": "2012-02-15 01:01:01", + "licence": "GNU GPL v2", + "authors": [{ + "name": "Nathan Guse", + "username": "EXreaction", + "email": "email@phpbb.com", + "homepage": "http://lithiumstudios.org", + "role": "N/A" + }], + "require": { + "php": ">=5.3", + "phpbb": "3.1.0-dev" + }, + "extra": { + "display-name": "phpBB Moo Extension" + } +} diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index 67630e9f36..d1e60ad268 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -11,7 +11,14 @@ class metadata_manager_test extends phpbb_database_test_case { protected $class_loader; protected $extension_manager; + + protected $cache; + protected $config; + protected $db; protected $phpbb_root_path; + protected $phpEx; + protected $template; + protected $user; public function getDataSet() { @@ -22,15 +29,30 @@ class metadata_manager_test extends phpbb_database_test_case { parent::setUp(); + $this->cache = new phpbb_mock_cache(); + $this->config = new phpbb_config(array( + 'version' => '3.1.0', + )); + $this->db = $this->new_dbal(); $this->phpbb_root_path = dirname(__FILE__) . '/'; + $this->phpEx = '.php'; + $this->user = new phpbb_user(); + + $this->template = new phpbb_template( + $this->phpbb_root_path, + $this->phpEx, + $this->config, + $this->user, + new phpbb_style_resource_locator() + ); $this->extension_manager = new phpbb_extension_manager( - $this->new_dbal(), - new phpbb_config(array()), + $this->db(), + $this->config, 'phpbb_ext', $this->phpbb_root_path, - '.php', - new phpbb_mock_cache + $this->phpEx, + $this->cache ); } @@ -39,21 +61,7 @@ class metadata_manager_test extends phpbb_database_test_case { $ext_name = 'bar'; - $manager = new phpbb_extension_metadata_manager_test( - $ext_name, - $this->new_dbal(), - $this->extension_manager, - $this->phpbb_root_path, - '.php', - new phpbb_template( - $this->phpbb_root_path, - '.php', - new phpbb_config(array()), - new phpbb_user(), - new phpbb_style_resource_locator() - ), - new phpbb_config(array()) - ); + $manager = $this->get_metadata_manager($ext_name); try { @@ -69,21 +77,7 @@ class metadata_manager_test extends phpbb_database_test_case { $ext_name = 'foo'; - $manager = new phpbb_extension_metadata_manager_test( - $ext_name, - $this->new_dbal(), - $this->extension_manager, - $this->phpbb_root_path, - '.php', - new phpbb_template( - $this->phpbb_root_path, - '.php', - new phpbb_config(array()), - new phpbb_user(), - new phpbb_style_resource_locator() - ), - new phpbb_config(array()) - ); + $manager = $this->get_metadata_manager($ext_name); try { @@ -103,23 +97,7 @@ class metadata_manager_test extends phpbb_database_test_case { $ext_name = 'validator'; - $manager = new phpbb_extension_metadata_manager_test( - $ext_name, - $this->new_dbal(), - $this->extension_manager, - $this->phpbb_root_path, - '.php', - new phpbb_template( - $this->phpbb_root_path, - '.php', - new phpbb_config(array()), - new phpbb_user(), - new phpbb_style_resource_locator() - ), - new phpbb_config(array( - 'version' => '3.1.0', - )) - ); + $manager = $this->get_metadata_manager($ext_name); // Non-existant data try @@ -358,6 +336,25 @@ class metadata_manager_test extends phpbb_database_test_case $this->fail($e); } } + + /** + * Get an instance of the metadata manager + * + * @param string $ext_name + * @return phpbb_extension_metadata_manager_test + */ + private function get_metadata_manager($ext_name) + { + return new phpbb_extension_metadata_manager_test( + $ext_name, + $this->new_dbal(), + $this->extension_manager, + $this->phpbb_root_path, + $this->phpEx, + $this->template, + $this->config + ); + } } class phpbb_extension_metadata_manager_test extends phpbb_extension_metadata_manager diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index a1deeb2b63..6b4c0b6883 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -252,6 +252,48 @@ class phpbb_functional_test_case extends phpbb_test_case } } + /** + * Login to the ACP + * You must run login() before calling this. + */ + protected function admin_login() + { + $this->add_lang('acp/common'); + + // Requires login first! + if (empty($this->sid)) + { + $this->fail('$this->sid is empty. Make sure you call login() before admin_login()'); + return; + } + + $crawler = $this->request('GET', 'adm/index.php?sid=' . $this->sid); + $this->assertContains($this->lang('LOGIN_ADMIN_CONFIRM'), $crawler->filter('html')->text()); + + $form = $crawler->selectButton($this->lang('LOGIN'))->form(); + + foreach ($form->getValues() as $field => $value) + { + if (strpos($field, 'password_') === 0) + { + $login = $this->client->submit($form, array('username' => 'admin', $field => 'admin')); + + $cookies = $this->cookieJar->all(); + + // The session id is stored in a cookie that ends with _sid - we assume there is only one such cookie + foreach ($cookies as $cookie); + { + if (substr($cookie->getName(), -4) == '_sid') + { + $this->sid = $cookie->getValue(); + } + } + + break; + } + } + } + protected function add_lang($lang_file) { if (is_array($lang_file)) @@ -288,4 +330,16 @@ class phpbb_functional_test_case extends phpbb_test_case return call_user_func_array('sprintf', $args); } + + /** + * assertContains for language strings + * + * @param string $needle Search string + * @param string $haystack Search this + * @param string $message Optional failure message + */ + public function assertContainsLang($needle, $haystack, $message = null) + { + $this->assertContains(html_entity_decode($this->lang($needle), ENT_QUOTES), $haystack, $message); + } } diff --git a/tests/test_framework/phpbb_test_case_helpers.php b/tests/test_framework/phpbb_test_case_helpers.php index 46feef550a..d10645a732 100644 --- a/tests/test_framework/phpbb_test_case_helpers.php +++ b/tests/test_framework/phpbb_test_case_helpers.php @@ -115,4 +115,112 @@ class phpbb_test_case_helpers return $config; } + + /** + * Recursive directory copying function + * + * @param string $source + * @param string $dest + * @return array list of files copied + */ + public function copy_dir($source, $dest) + { + $source = (substr($source, -1) == '/') ? $source : $source . '/'; + $dest = (substr($dest, -1) == '/') ? $dest : $dest . '/'; + + $copied_files = array(); + + if (!is_dir($dest)) + { + $this->makedirs($dest); + } + + $files = scandir($source); + foreach ($files as $file) + { + if ($file == '.' || $file == '..') + { + continue; + } + + if (is_dir($source . $file)) + { + $created_dir = false; + if (!is_dir($dest . $file)) + { + $created_dir = true; + $this->makedirs($dest . $file); + } + + $copied_files = array_merge($copied_files, self::copy_dir($source . $file, $dest . $file)); + + if ($created_dir) + { + $copied_files[] = $dest . $file; + } + } + else + { + if (!file_exists($dest . $file)) + { + copy($source . $file, $dest . $file); + + $copied_files[] = $dest . $file; + } + } + } + + return $copied_files; + } + + /** + * Remove files/directories that are listed in an array + * Designed for use with $this->copy_dir() + * + * @param array $file_list + */ + public function remove_files($file_list) + { + foreach ($file_list as $file) + { + if (is_dir($file)) + { + rmdir($file); + } + else + { + unlink($file); + } + } + } + + /** + * Empty directory (remove any subdirectories/files below) + * + * @param array $file_list + */ + public function empty_dir($path) + { + $path = (substr($path, -1) == '/') ? $path : $path . '/'; + + $files = scandir($path); + foreach ($files as $file) + { + if ($file == '.' || $file == '..') + { + continue; + } + + if (is_dir($path . $file)) + { + $this->empty_dir($path . $file); + + rmdir($path . $file); + } + else + { + unlink($path . $file); + } + } + } } |