Showing posts with label FileSystem. Show all posts
Showing posts with label FileSystem. Show all posts

Tuesday, February 28, 2012

Get the dynamically rendered file contents

/**
 * Get the dynamic rendered contents of given file
 * without exposing a bit of char.
 *
 * @author Junaid Atari <mj.atari@gmail.com>
 * @version 1.0
 * @param string $filename Absolute path of the file.
 * @param array $vars List of variables pass to file.
 * @return mixed Rendered output of file.
 */
function getIncludedFileContents $filename, array $vars = array () )
{
    
//** make sure that given path is the real file.
    
if ( is_file ( (string) $filename ) )
    {
        
//** Start Output Buffer
        
ob_start ();
        
ob_implicit_flush false );
     
        
//** Extract named variables and assign thier values.
        
extract $varsEXTR_PREFIX_SAME'vars' );
     
        
//** Include the file and execute the code.
        
require_once ( $filename );
     
        
//** Return the rendered output.
        
return ob_get_clean ();
    }

    
//** If nothing, return.
    
return NULL;
}
/*
 +-----------+
 |  Example  |
 +-----------+
*/
/*

welcome.php
-------------------
Salam <strong><?php echo $fullname; ?></strong>!

*/

/*
# The file contain variable $fullname
# and will replaced with the value.
echo getIncludedFileContents (
    "welcome.php",
    array ( "fullname"=>'Junaid Atari' )
);
*/

/*
 +-----------+
 |  Output   |
 +-----------+
*/
/*
Salam <strong>Junaid Atari</strong>!
*/

Sunday, November 20, 2011

Get the Directories Name by Given Criteria

/*
 * Get the directories name by given criteria.
 *
 * @author    Junaid Atari <mj.atari@gmail.com>
 * @version   1.0
 * @param     string   $dir              Complete path to directory.
 * @param     array    $skipList         Dirs name to be exclude.
 * @param     string   $strictNameRegex  PCRE pattern to filter names
 * @return    array    List of folders name | empty
*/ 
function getDirectryFolders 
   
$dir$skipList = array (), $strictNameRegex '' )
{
    
$listDir = array ();
    
    if ( !
preg_match '@(//|\\\)$@', (string) $dir ) )
        
$dir .= DIRECTORY_SEPARATOR;
    
    
$handler = @opendir $dir );
    
    if ( !
$handler )
        return array ();
    
    
$skipList array_merge ( (array) $skipList, array ( '.''..' ) );
    
    while ( ( 
$sub readdir $handler ) ) !== false )
    { 
        if ( !
in_array $sub$skipList ) && is_dir $dir $sub ) )
        {
            if ( 
trim $strictNameRegex ) )
            {
                if ( (bool) @
preg_match $strictNameRegex$sub ) )
                    
$listDir$sub ] = $dir $sub;
            }
            else
                
$listDir$sub ] = $dir $sub;
        }
    } 
    
    
closedir $handler );
    
    return 
$listDir
}

/*
+---------+
| Example |
+---------+
*/ 
print_r (
    
getDirectryFolders (
        
'c:\htdocs\web',
        array ( 
'includes' ),
        
'/^[a-z0-9_]+$/i'
    
)
);

/*
+--------+
| Output |
+--------+

Array
(
    [backups] => c:\htdocs\web\backups
    [libraries] => c:\htdocs\web\libraries
    [images] => c:\htdocs\web\images
    [logs] => c:\htdocs\web\logs
    [components] => c:\htdocs\web\components
)

*/ 

Get the Files of Directory by Given Criteria

/*
 * Get the files of directory by given criteria.
 *
 * @author  Junaid Atari <mj.atari@gmail.com>
 * @version 1.0
 * @param   
string  $dir              Path of directory.
 * @param   string  $pattern          Pattern of file name & extesion
 * @param   array   $excludeList      Files list to exclude (name only)
 * @param   string  $strictNameRegex  PCRE pattern to filter files name
 * @return  array   List of folder's files | empty
*/
function getFolderFiles (
   $dir$pattern NULL, array $excludeList = array (),
   $strictNameRegex '' )
{
    
$pattern = !is_string $pattern )
                ? 
'*.php'
                
$pattern;
      
    if ( !
preg_match '@(//|\\\)$@'$dir ) )
            
$dir .= DIRECTORY_SEPARATOR;
  
    
$_files glob $dir .DIRECTORY_SEPARATOR $pattern );
  
    if ( !
count $_files ) )
        return array ();
  
    
$filesList = array ();
  
    foreach ( 
$_files as $file )
    {
        
$fName pathinfo $filePATHINFO_FILENAME );
      
        if ( !
in_array $fName, (array) $excludeList ) )
            
$filesList$fName ] = $file;
    }
          
    if ( 
trim $strictNameRegex ) )
    {
        
$fiList = array ();
      
        
$list preg_grep (
            $strictNameRegexarray_keys $filesList )
        );
      
        if ( !
count $list ) )
            return array ();
      
        foreach ( 
$list as $name )
        {
            if ( 
array_key_exists $name$filesList ) )
                
$fiList$name ] = $filesList[$name];
        }
      
        
$filesList $fiList;
    }
  
    return 
$filesList;
}

/*
+---------+
| Example |
+---------+
*/ 

print_r 
(
    
getFolderFiles (
        
'C:/path/to',
        
'CF*.php',
        array ( 
'CTag' ),
        
'/^CF[a-zA-Z0-9_]+$/'
    
)
);

/*
+--------+
| Output |
+--------+

Array
(
    [CF_Comment] => C:/path/to/CF_Comment.php
    [CF_BBTags] => C:/path/to/CF_BBTags.php
    [CF_JavaScript] => C:/path/to/CF_JavaScript.php
)

*/