<?php

namespace Fab\Plugin\Helper;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Directory Helper
 *
 * @package    Fab
 * @subpackage Fab\Plugin\Helper
 */
trait Directory {

    /**
     * Get lists of directories
     *
     * @return  void
     * @var     string  $path   Directory path
     */
    public function getDir( $path, $directories = array() ) {
        foreach ( glob( $path . '/*', GLOB_ONLYDIR ) as $dir ) {
            $directories[] = basename( $dir );
        }
        return $directories;
    }

    /**
     * Get files within directory
     *
     * @return  array
     * @var     string  $dir   plugin hooks directory (Api, Controller)
     */
    public function getDirFiles( $dir, &$results = array() ) {
        $files = scandir( $dir );
        foreach ( $files as $key => $value ) {
            $path = realpath( $dir . DIRECTORY_SEPARATOR . $value );
            if ( ! is_dir( $path ) ) {
                $results[] = $path;
            } elseif ( $value != '.' && $value != '..' ) {
                self::getDirFiles( $path, $results );
            }
        }
        return $results;
    }
}
