/**
  * Method to get the list of files for the field options.
  * Specify the target directory with a directory attribute
  * Attributes allow an exclude mask and stripping of extensions from file name.
  * Default attribute may optionally be set to null (no file) or -1 (use a default).
  *
  * @return  array  The field option objects.
  *
  * @since   1.0
  */
 protected function getOptions()
 {
     $options = array();
     // Initialize some field attributes.
     $filter = (string) $this->element['filter'];
     $exclude = (string) $this->element['exclude'];
     $stripExt = (string) $this->element['stripext'];
     $hideNone = (string) $this->element['hide_none'];
     $hideDefault = (string) $this->element['hide_default'];
     // Get the path in which to search for file options.
     $path = (string) $this->element['directory'];
     if (!is_dir($path)) {
         $path = JPATH_ROOT . '/' . $path;
     }
     // Prepend some default options based on field attributes.
     if (!$hideNone) {
         $options[] = HtmlSelect::option('-1', Text::alt('JOPTION_DO_NOT_USE', preg_replace('/[^a-zA-Z0-9_\\-]/', '_', $this->fieldname)));
     }
     if (!$hideDefault) {
         $options[] = HtmlSelect::option('', Text::alt('JOPTION_USE_DEFAULT', preg_replace('/[^a-zA-Z0-9_\\-]/', '_', $this->fieldname)));
     }
     // Get a list of files in the search path with the given filter.
     $files = Folder::files($path, $filter);
     // Build the options list from the list of files.
     if (is_array($files)) {
         foreach ($files as $file) {
             // Check to see if the file is in the exclude mask.
             if ($exclude) {
                 if (preg_match(chr(1) . $exclude . chr(1), $file)) {
                     continue;
                 }
             }
             // If the extension is to be stripped, do it.
             if ($stripExt) {
                 $file = File::stripExt($file);
             }
             $options[] = HtmlSelect::option($file, $file);
         }
     }
     // Merge any additional options in the XML definition.
     $options = array_merge(parent::getOptions(), $options);
     return $options;
 }
 /**
  * Execute the command
  *
  * @return  void
  *
  * @since   1.0
  */
 public function execute()
 {
     // Check if caching is enabled
     $twigCache = $this->app->get('template.cache', false);
     if ($twigCache === false) {
         $this->app->out('Twig caching is disabled.');
         return;
     }
     // Display status
     $this->app->out('Resetting Twig Cache.');
     // First remove the existing cache files
     if (is_dir(JPATH_ROOT . '/' . $twigCache)) {
         foreach (Folder::folders(JPATH_ROOT . '/' . $twigCache) as $folder) {
             Folder::delete(JPATH_ROOT . '/' . $twigCache . '/' . $folder);
         }
     }
     // Now get a list of all the templates
     $files = Folder::files(JPATH_TEMPLATES, '.twig', true, true);
     // Load each template now
     /** @var \Joomla\Renderer\TwigRenderer $twigRenderer */
     $twigRenderer = $this->app->getContainer()->get('renderer');
     $engine = $twigRenderer->getRenderer();
     $errorFiles = [];
     foreach ($files as $file) {
         $template = str_replace(JPATH_TEMPLATES . '/', '', $file);
         try {
             $engine->loadTemplate($template);
         } catch (\Twig_Error $e) {
             $errorFiles[] = $file;
         }
     }
     if (count($errorFiles)) {
         $msg = 'The following Twig resources failed to cache: ' . implode(', ', $errorFiles);
     } else {
         $msg = 'The cached Twig resources were successfully created.';
     }
     $this->app->out($msg);
 }
 /**
  * Tests the Folder::files method.
  *
  * @return  void
  *
  * @covers             Joomla\Filesystem\Folder::files
  * @expectedException  \UnexpectedValueException
  */
 public function testFilesException()
 {
     Folder::files('/this/is/not/a/path');
 }
 /**
  * Tests the Folder::files method.
  *
  * @return  void
  *
  * @since   1.0
  *
  * @covers  Joomla\Filesystem\Folder::files
  * @covers  Joomla\Filesystem\Folder::_items
  */
 public function testFiles()
 {
     // Make sure previous test files are cleaned up
     $this->_cleanupTestFiles();
     // Make some test files and folders
     mkdir(Path::clean(__DIR__ . '/tmp/test'), 0777, true);
     file_put_contents(Path::clean(__DIR__ . '/tmp/test/index.html'), 'test');
     file_put_contents(Path::clean(__DIR__ . '/tmp/test/index.txt'), 'test');
     mkdir(Path::clean(__DIR__ . '/tmp/test/test'), 0777, true);
     file_put_contents(Path::clean(__DIR__ . '/tmp/test/test/index.html'), 'test');
     file_put_contents(Path::clean(__DIR__ . '/tmp/test/test/index.txt'), 'test');
     // Use of realpath to ensure test works for on all platforms
     $result = Folder::files(Path::clean(__DIR__ . '/tmp/test'), 'index.*', true, true, array('index.html'));
     $result[0] = realpath($result[0]);
     $result[1] = realpath($result[1]);
     $this->assertEquals(array(Path::clean(__DIR__ . '/tmp/test/index.txt'), Path::clean(__DIR__ . '/tmp/test/test/index.txt')), $result, 'Line: ' . __LINE__ . ' Should exclude index.html files');
     // Use of realpath to ensure test works for on all platforms
     $result = Folder::files(Path::clean(__DIR__ . '/tmp/test'), 'index.html', true, true);
     $result[0] = realpath($result[0]);
     $result[1] = realpath($result[1]);
     $this->assertEquals(array(Path::clean(__DIR__ . '/tmp/test/index.html'), Path::clean(__DIR__ . '/tmp/test/test/index.html')), $result, 'Line: ' . __LINE__ . ' Should include full path of both index.html files');
     $this->assertEquals(array(Path::clean('index.html'), Path::clean('index.html')), Folder::files(Path::clean(__DIR__ . '/tmp/test'), 'index.html', true, false), 'Line: ' . __LINE__ . ' Should include only file names of both index.html files');
     // Use of realpath to ensure test works for on all platforms
     $result = Folder::files(Path::clean(__DIR__ . '/tmp/test'), 'index.html', false, true);
     $result[0] = realpath($result[0]);
     $this->assertEquals(array(Path::clean(__DIR__ . '/tmp/test/index.html')), $result, 'Line: ' . __LINE__ . ' Non-recursive should only return top folder file full path');
     $this->assertEquals(array(Path::clean('index.html')), Folder::files(Path::clean(__DIR__ . '/tmp/test'), 'index.html', false, false), 'Line: ' . __LINE__ . ' non-recursive should return only file name of top folder file');
     $this->assertFalse(Folder::files('/this/is/not/a/path'), 'Line: ' . __LINE__ . ' Non-existent path should return false');
     $this->assertEquals(array(), Folder::files(Path::clean(__DIR__ . '/tmp/test'), 'nothing.here', true, true, array(), array()), 'Line: ' . __LINE__ . ' When nothing matches the filter, should return empty array');
     // Clean up our files
     $this->_cleanupTestFiles();
 }