Commit 891a1104 authored by Sigrid Suski's avatar Sigrid Suski

(+) Recursive file moving/copying added to the DirectoryIterator

    version 1.2
parent 1edce681
......@@ -17,7 +17,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* @created Thu, Dec 1, 2016 11:53:09 by Radek Suski
* @modified 30 April 2020 by Sigrid Suski
* @modified 07 September 2020 by Sigrid Suski
*/
namespace Sobi;
......@@ -44,5 +44,5 @@ abstract class C
const NO = 0;
const ROOT = JPATH_ROOT;
const DS = DIRECTORY_SEPARATOR;
const VERSION = '1.1.1';
const VERSION = '1.2';
}
......@@ -31,7 +31,9 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
(!) Change
### 1.1.1 (September 2020)
### 1.2 (September 2020)
(+) Recursive file moving/copying added to the DirectoryIterator
(#) Fixes for latest MySQL version
......
<?php
/**
* @package: Sobi Framework
*
* @author
* Name: Sigrid Suski & Radek Suski, Sigsiu.NET GmbH
* Email: sobi[at]sigsiu.net
* Url: https://www.Sigsiu.NET
* @copyright Copyright (C) 2006 - 2016 Sigsiu.NET GmbH (https://www.sigsiu.net). All rights reserved.
*
* @copyright Copyright (C) 2006 - 2020 Sigsiu.NET GmbH (https://www.sigsiu.net). All rights reserved.
* @license GNU/LGPL Version 3
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3
* as published by the Free Software Foundation, and under the additional terms according section 7 of GPL v3.
* See http://www.gnu.org/licenses/lgpl.html and https://www.sigsiu.net/licenses.
* See https://www.gnu.org/licenses/lgpl.html and https://www.sigsiu.net/licenses.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* @created
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* @created 03 December 2016 by Radek Suski
* @modified 07 September 2020 by Sigrid Suski
*/
namespace Sobi\FileSystem;
......@@ -23,6 +27,10 @@ defined( 'SOBI' ) || exit( 'Restricted access' );
use Sobi\FileSystem\DirectoryIterator;
use Sobi\FileSystem\FileSystem;
/**
* Class Directory
* @package Sobi\FileSystem
*/
class Directory extends File
{
/*** @var DirectoryIterator */
......@@ -32,6 +40,7 @@ class Directory extends File
* @param string | array $string - part or full name of the file to search for
* @param bool $exact - search for exact string or the file nam can contain this string
* @param $recLevel - recursion level
*
* @return array
*/
public function searchFile( $string, $exact = true, $recLevel = 1 )
......@@ -44,6 +53,7 @@ class Directory extends File
foreach ( $string as $search ) {
$this->searchRecursive( $this->_dirIterator, $search, $exact, $recLevel, $results );
}
return $results;
}
......@@ -55,39 +65,44 @@ class Directory extends File
if ( !$this->_dirIterator ) {
$this->_dirIterator = new DirectoryIterator( $this->_filename );
}
return $this->_dirIterator;
}
/**
* Move files from directory to given path
* Moves files from directory to given path.
*
* @param string $target - target path
* @param bool $force
* @param false $force
* @param false $recursive
*
* @return array
*/
public function moveFiles( $target, $force = false )
public function moveFiles( $target, $force = false, $recursive = false )
{
$this->iterator();
$log = [];
foreach ( $this->_dirIterator as $child ) {
if ( !( $child->isDot() ) ) {
if ( ( !( FileSystem::Exists( FileSystem::Clean( $target . '/' . $child->getFileName() ) ) ) ) ) {
if ( FileSystem::Move( $child->getPathname(), FileSystem::Clean( $target . '/' . $child->getFileName() ) ) ) {
$log[] = FileSystem::Clean( $target . '/' . $child->getFileName() );
if ( ( !( FileSystem::Exists( FileSystem::Clean( $target . '/' . $child->getFileName() ) ) ) ) || $force ) {
if ( is_dir( $child->getPathname() ) && $recursive ) {
$fr = new Directory( $child->getPathname() );
$fr->moveFiles( $target . $child->getFilename() . '/', $force, true );
}
}
elseif ( $force && !( is_dir( $child->getPathname() ) ) ) {
if ( FileSystem::Move( $child->getPathname(), FileSystem::Clean( $target . '/' . $child->getFileName() ) ) ) {
elseif ( FileSystem::Move( $child->getPathname(), FileSystem::Clean( $target . '/' . $child->getFileName() ) ) ) {
$log[] = FileSystem::Clean( $target . '/' . $child->getFileName() );
}
}
}
}
return $log;
}
/**
* Remove all files in directory
* @return void
* Removes all files in directory.
*
* @throws \Sobi\Error\Exception
*/
public function deleteFiles()
{
......@@ -106,6 +121,7 @@ class Directory extends File
* @param $recLevel
* @param $results
* @param $level
*
* @return void
*/
private function searchRecursive( DirectoryIterator $dir, $string, $exact, $recLevel, &$results, $level = 0 )
......
<?php
/**
* @package: Sobi Framework
*
* @author
* Name: Sigrid Suski & Radek Suski, Sigsiu.NET GmbH
* Email: sobi[at]sigsiu.net
* Url: https://www.Sigsiu.NET
* @copyright Copyright (C) 2006 - 2016 Sigsiu.NET GmbH (https://www.sigsiu.net). All rights reserved.
*
* @copyright Copyright (C) 2006 - 2020 Sigsiu.NET GmbH (https://www.sigsiu.net). All rights reserved.
* @license GNU/LGPL Version 3
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3
* as published by the Free Software Foundation, and under the additional terms according section 7 of GPL v3.
* See http://www.gnu.org/licenses/lgpl.html and https://www.sigsiu.net/licenses.
* See https://www.gnu.org/licenses/lgpl.html and https://www.sigsiu.net/licenses.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* @created Sat, Dec 3, 2016 15:45:55
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* @created Sat, Dec 3, 2016 by Radek Suski
* @modified 07 September 2020 by Sigrid Suski
*/
namespace Sobi\FileSystem;
......@@ -24,22 +28,42 @@ use Sobi\FileSystem\FileSystem;
defined( 'SOBI' ) || exit( 'Restricted access' );
/**
* Class DirectoryIterator
* @package Sobi\FileSystem
*/
class DirectoryIterator extends \ArrayObject
{
/**
* @var string
*/
private $_dir = null;
/** @noinspection PhpInconsistentReturnPointsInspection */
// protected function scanDirectory( $dir )
// {
// $res = scandir( $dir );
// foreach ( $res as $file ) {
// if ( $file != '.' && $file != '..' ) {
// if ( is_dir( $dir . '/' . $file ) ) {
// $this->scanDirectory( $dir . '/' . $file );
// }
// else {
// $this->append( new File( FileSystem::Clean( $dir . '/' . $file ) ) );
// }
// }
// }
// }
/**
* @param string $dir - path
*
* @return DirectoryIterator
*/
public function __construct( $dir )
{
$Dir = scandir( $dir );
$this->_dir = new ArrayObject();
// $this->scanDirectory( $dir );
foreach ( $Dir as $file ) {
$this->append( new File( FileSystem::Clean( $dir . '/' . $file ) ) );
}
......@@ -54,12 +78,14 @@ class DirectoryIterator extends \ArrayObject
else {
return ( $from->isDir() && !( $from->isDot() ) ) ? -1 : 1;
}
} );
}
);
}
/**
* @param string $string - part or full name of the file to search for
* @param bool $exact - search for exact string or the file nam can contain this string
*
* @return array
*/
public function searchFile( $string, $exact = true )
......@@ -74,12 +100,11 @@ class DirectoryIterator extends \ArrayObject
$results[ $item->getPathname() ] = $item;
}
}
else {
if ( strstr( $item->getFileName(), $string ) ) {
$results[ $item->getPathname() ] = $item;
}
elseif ( strstr( $item->getFileName(), $string ) ) {
$results[ $item->getPathname() ] = $item;
}
}
return $results;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment