Actions

Source Code of This Project

/lib/helper/MyUtilHelper.php

<?php
/*
 * This file is part of the pwp package.
 * (c) 2009-2010 Victor Rad' <victor.v.rad[at]gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Scan dir and bouild structute in two mode
 *
 * @param string $dir directory path
 * @param array $mode mode of scan
 * @param array $mode['flat'] scan in flat mode
 * @param array $mode['flat']['fullPath'] return array with full path to files
 * @param array $mode['flat']['recursive'] recursive scan
 * @param array $mode['tree'] scan in tree mode
 * @param array $filters array with preg patterns
 * @return array
 *  e.g. in 'flat' mode
 *  [0]   => /home/web/apps/frontend/templates/layout.php
 *  [1]   => /home/web/apps/frontend/templates/_pager.php
 *  [2]   => /home/web/apps/frontend/templates/_scrollBar.php
 *  [...] => /home/web/config/app.yml
 *  e.g. in 'tree' mode
 *  Array (
 *  [apps]=>Array(
 *    [frontend]=>Array(
 *      [config]=>Array(
 *       [0]=>app.yml
 *       [1]=>cache.yml
 *       [2]=>factories.yml
 *       [3]=>filters.yml
 *      )
 *      [lib]=>myUser.class.php
 *      [modules]=>Array(
 *   ......
 */
function my_scandir($dir, $mode, $filters = array())
{
  /**
   * prepare and check data
   */
  $filesList = array();
  $dirsSort = array();
  $filesSort = array();
  $modeScan = key($mode);
  $fullPath = !empty($mode[$modeScan]['fullPath']) ? true : false;
  $recursive = !empty($mode[$modeScan]['recursive']) ? true : false;
  if (empty($mode[$modeScan]['baseDir']))
  {
    $mode[$modeScan]['baseDir'] = $dir;
    $baseDir = $dir;
  }
  else
    $baseDir = $mode[$modeScan]['baseDir'];
  $relativeDir = str_replace($baseDir, '', $dir);

  /**
   * build structure
   */
  // scan dir and remove . ..
  $files = array_diff(scandir($dir), array('.', '..'));
  // sort: directories first
  foreach($files as $file)
  {
    if(is_dir($dir.'/'.$file))
      $dirsSort[] = $file;
    elseif(is_file($dir.'/'.$file))
      $filesSort[] = $file;
  }
  $files = array_merge($dirsSort, $filesSort);

  // build structure
  foreach($files as $file)
  {
    // filter
    $filterMatch = false;
    foreach ($filters as $filter)
      if (preg_match('%^'.$filter.'$%', $relativeDir.'/'.$file))
        $filterMatch = true;

    // continue if not filtered
    if (false === $filterMatch)
    {
      if('flat' == $modeScan)
      {
        if(true === $recursive and is_dir($dir.'/'.$file))
          $filesList = array_merge($filesList, my_scandir($dir.'/'.$file, $mode, $filters));
        if(is_file($dir.'/'.$file))
          $filesList[] = (true === $fullPath) ? $dir.'/'.$file : $file;
      }
      elseif('tree' == $modeScan)
      {
        if(is_dir($dir.'/'.$file))
          $filesList = array_merge_recursive($filesList, my_scandir($dir.'/'.$file, $mode, $filters));
        if(is_file($dir.'/'.$file))
        {
          $dirInDepthKeys = '';
          foreach (explode('/', $relativeDir) as $dirInDepth)
            if ($dirInDepth)
              $dirInDepthKeys .= '["'.$dirInDepth.'"]';
          eval('$listTemp'.$dirInDepthKeys.'=array("'.$file.'");');
          if (is_array($listTemp))
            $filesList = array_merge_recursive($filesList, $listTemp);
          else
            $filesList[] = $file;
        }
      }
    }
  }

  return $filesList;
}

/**
 * Recursively delete a directory
 *
 * @param string $dir Directory name
 * @param boolean $deleteRootToo Delete specified top-level directory as well
 */
function my_unlink($dir, $deleteRootToo)
{
    if(!$dh = @opendir($dir))
        return;
    while (false !== ($obj = readdir($dh)))
    {
        if($obj == '.' || $obj == '..')
            continue;

        if (!@unlink($dir . '/' . $obj))
            unlinkRecursive($dir.'/'.$obj, true);
    }

    closedir($dh);

    if ($deleteRootToo)
        @rmdir($dir);

    return;
}

/**
 *  Retrieves HTML meta value for the current response
 *
 * @param string $name the name of key
 * @param string $for the top parent value in view.yml
 * @return string
 */
function get_response_meta($name, $for = 'all')
{
  // get from response
  $metas = sfContext::getInstance()->getResponse()->getMetas();
  if (isset($metas[$name]))
  {
    $meta  = $metas[$name];
  }
  // get from config file view.yml
  else
  {
    $configPath = sfConfig::get('sf_app_module_dir').'/'.sfContext::getInstance()->getModuleName().'/config/view.yml';
    $config = sfYaml::load($configPath);
    $meta  = isset($config[$for]['metas'][$name]) ? $config[$for]['metas'][$name] : '';
  }


  return $meta;
}

/**
 * String representation for Boolean variable
 *
 * @param boolean $bool
 * @return string
 */
function bool2str($bool)
{
  return ($bool) ? 'true' : 'false';
}