* php ../docs/examples/applications/Lister/list.php * * This will list all the files and directories in the current directories, * into as many columns as it fits, using colors, and it will not show the * hidden files. * * * php ../docs/examples/applications/Lister/list.php -d * * Same as the previous command, but it will only show the directories in the * current directory. * * * php ../docs/examples/applications/Lister/list.php -l -a -o -- Base * * This will show all the files (including hidden ones) in the Base directory, in a list * format, putting files last. * * @package Applications * @version //autogen// */ class ezcLister { /** * Used for output to the console. * * @var ezcConsoleOutput */ private $output; /** * Used for input from the console. * * @var ezcConsoleInput */ private $input; /** * The color options for console output. * Can be "auto", "always" or "never". Default is "auto". * It is set in the {@link processInput()} function. * * @var string */ private $optionColor; /** * The list option for console output. * It is set in the {@link processInput()} function. * * @var bool */ private $optionList; /** * The directory option for console output. * It is set in the {@link processInput()} function. * * @var bool */ private $optionDir; /** * The all option for console output. * (false = do not show hidden files, true = show them). * It is set in the {@link processInput()} function. * * @var bool */ private $optionAll; /** * The order option for console output. * (false = files & directories mixed, true = files last). * It is set in the {@link processInput()} function. * * @var bool */ private $optionOrder; /** * The directory which the user specifies in the command line. * It is set in the {@link processInput()} function. * If the user does not specify a directory, it is set to "." (current directory). * * @var string */ private $dir; /** * Entry point for this application. */ public function run() { $this->init(); $this->output->outputLine( 'ezcLister version 0.1devel', 'info' ); $this->output->outputLine(); if ( $this->input->getOption( 'h' )->value === true ) { $this->printHelp(); exit( 0 ); } $fileList = $this->createFileList( $this->optionDir, $this->optionAll, $this->optionOrder ); $this->printFileList( $fileList, $this->optionList ); } /** * Configures the input and output for the console. */ private function init() { // Output configuration $this->output = new ezcConsoleOutput(); $this->output->formats->error->color = 'red'; $this->output->formats->error->style = array( 'bold' ); $this->output->formats->info->color = 'blue'; $this->output->formats->info->style = array( 'bold' ); $this->output->formats->log->color = 'gray'; $this->output->formats->log->style = array( 'bold' ); // Option configuration $this->input = new ezcConsoleInput(); $optionHelpOption = $this->input->registerOption( new ezcConsoleOption( 'h', 'help' ) ); $optionHelpOption->shorthelp = 'Get help information.'; $optionAllOption = $this->input->registerOption( new ezcConsoleOption( 'a', 'all' ) ); $optionAllOption->shorthelp = 'Do not hide entries starting with .'; $optionColorOption = $this->input->registerOption( new ezcConsoleOption( 'c', 'color', ezcConsoleInput::TYPE_STRING ) ); $optionColorOption->shorthelp = 'Control whether color is used to distinguish file types. WHEN may be never, always, or auto.'; $optionDirOption = $this->input->registerOption( new ezcConsoleOption( 'd', 'directory' ) ); $optionDirOption->shorthelp = 'List directory entries instead of contents, and do not dereference symbolic links.'; $optionListOption = $this->input->registerOption( new ezcConsoleOption( 'l', 'list' ) ); $optionListOption->shorthelp = 'Use a long listing format.'; $optionDirOption = $this->input->registerOption( new ezcConsoleOption( 'o', 'order' ) ); $optionDirOption->shorthelp = 'Put files last.'; $this->processInput(); // Configure the colors for display, by creating the output formats // 'directory' and 'file'. if ( $this->optionColor == 'auto' || $this->optionColor == 'always' ) { $this->output->formats->directory->color = 'blue'; $this->output->formats->directory->style = array ( 'bold' ); $this->output->formats->file->color = 'magenta'; $this->output->formats->file->style = array ( 'bold' ); } if ( $this->optionColor == 'never' ) { $this->output->formats->directory->color = 'default'; $this->output->formats->directory->style = array ( 'normal' ); $this->output->formats->file->color = 'default'; $this->output->formats->file->style = array ( 'normal' ); } } /** * Outputs the error message and then terminates the script. * * @param string $message */ private function raiseError( $message ) { $this->output->outputLine( $message, 'error' ); $this->output->outputLine(); die( 255 ); } /** * Processes the input from the console. * * If something is wrong it raises an error. * Sets the class variables $optionColor, $optionList, $optionDir, $optionAll, $optionOrder. * Also sets $dir, which holds the directory specified in the command-line. * If there was no directory specified, it sets $dir to "." (current directory). * If the directory specified does not exist, it displays an error message and quits. * * @return void */ private function processInput() { try { $this->input->process(); } catch ( ezcConsoleOptionException $e ) { $this->raiseError( $e->getMessage() ); } $this->optionColor = $this->input->getOption( 'color' )->value; if ( $this->optionColor === false ) { $this->optionColor = 'auto'; } $colorOptions = array( 'auto', 'always', 'never' ); if ( !in_array( $this->optionColor, $colorOptions ) ) { $this->raiseError( "The value for option is wrong (should be auto, always, or never)." ); } $this->optionList = $this->input->getOption( 'list' )->value; $this->optionDir = $this->input->getOption( 'directory' )->value; $this->optionAll = $this->input->getOption( 'all' )->value; $this->optionOrder = $this->input->getOption( 'order' )->value; // Get the directory name argument. If none is specified, it assumes "." (current directory). $this->dir = $this->input->getArguments(); if ( $this->dir != null ) { $this->dir = $this->dir[0]; } else { $this->dir = "."; } if ( !is_dir( $this->dir ) ) { $this->raiseError( 'The directory you specified does not exist.' ); } } /** * Prints the help and options for this application using an ezcConsoleTable object. */ private function printHelp() { $table = new ezcConsoleTable( $this->output, 78 ); $table->options->widthType = ezcConsoleTable::WIDTH_FIXED; foreach ( $this->input->getOptions() as $id => $option ) { $table[$id][]->content = '-' . $option->short . '/--' . $option->long; $table[$id][]->content = $option->shorthelp; } $this->output->outputLine( 'Commandline help.' ); $this->output->outputLine( $this->input->getSynopsis() ); $table->outputTable(); $this->output->outputLine(); } /** * Creates and returns the file list in the specified directory. * The generated list will be based on the values passed as parameters. * The returned array is bidimensional, like: * * $fileList[5][0] = "foo.txt"; * $fileList[5][1] = 0; * * For the second column, 0 means file, 1 means directory * * @param bool $optionDir Display only the directories or not. * @param bool $optionAll Display also the hidden files or not. * @param bool $optionOrder Display files last or not. * @return array(string) Bidimensional array */ private function createFileList( $optionDir = false, $optionAll = false, $optionOrder = false ) { $fileList = array(); $dirHandle = opendir( $this->dir ); $index = 0; while ( false !== ( $file = readdir( $dirHandle ) ) ) { $isDir = is_dir( "{$this->dir}/{$file}" ); $isHidden = ( substr( $file, 0, 1 ) === '.' ); if ( ( !$isHidden ) || ( $isHidden && $optionAll ) ) { if ( ( !$isDir && !$optionDir ) || ( $isDir ) ) { $fileList[$index] = array(); $fileList[$index][0] = $file; $fileList[$index][1] = $isDir; $index++; } } } closedir( $dirHandle ); if ( !empty( $fileList ) ) { foreach ( $fileList as $key => $row ) { $files[$key] = $row[0]; $types[$key] = $row[1]; } $files = array_map( 'strtolower', $files ); // Sorts $fileList based on the $optionOrder parameter. // (files last or files together with directories). if ( $optionOrder ) { array_multisort( $types, SORT_DESC, $files, SORT_ASC, SORT_STRING, $fileList ); } else { array_multisort( $files, SORT_ASC, SORT_STRING, $types, SORT_DESC, $fileList ); } } return $fileList; } /** * Outputs the file list on the console. * * The display is based on the $optionList parameter (table if $optionList is false). * If $fileList is empty then the output to the console will be: * * The 'dir' is empty. * * where dir is the directory specified in the command-line. * * @param array $fileList Bidimensional array. See {@link ezcLister::createFileList()}. * @param bool $optionList Display the results in a list or in a table. */ private function printFileList( $fileList, $optionList = false ) { if ( empty( $fileList ) ) { $this->output->outputLine( "The '{$this->dir}' directory is empty", 'info' ); return; } if ( $optionList ) { for ( $i = 0; $i < count( $fileList ); $i++ ) { $this->output->outputLine( $fileList[$i][0], ( $fileList[$i][1] ? 'directory' : 'file' ) ); } } else { $table = new ezcConsoleTable( $this->output, 78 ); $columnWidth = strlen( $fileList[$this->longestElement( $fileList )][0] ); $columns = floor( 78 / $columnWidth ); $rows = count( $fileList ) / $columns; $table->options->widthType = ezcConsoleTable::WIDTH_FIXED; $table->options->colPadding = ' '; $table->options->lineVertical = ''; $table->options->lineHorizontal = ''; $table->options->corner = ''; for ( $i = 0; $i < $rows; $i++ ) { $table[$i]->options->colPadding = ' '; for ( $j = 0; $j < $columns; $j++ ) { $table[$i][$j]->content = $fileList[$i * $columns + $j][0]; $table[$i][$j]->format = ( $fileList[$i * $columns + $j][1] ? 'directory' : 'file' ); } } $table->outputTable(); } $this->output->outputLine(); } /** * Finds out the index of the longest element in the bi-dimensional array $fileList. * (used for column display). * * @param array(string) $fileList Bidimensional array. See {@link ezcLister::createFileList()}. * @return int The index of the longest element in the array. */ private function longestElement( $fileList ) { $result = 0; for ( $i = 0; $i < count( $fileList ); $i++ ) { if ( strlen( $fileList[$i][0] ) > strlen( $fileList[$result][0] ) ) { $result = $i; } } return $result; } } // Instantiates the ezcLister class and executes its run() method. $optionLister = new ezcLister(); $optionLister->run(); ?>