ensureExtension() public static method

Append a file extension to a path/file if there is no or an empty extension provided, this helper methods is used to make sure the right extension existing on files.
public static ensureExtension ( string $file, string $extension ) : the
$file string The file where extension should be append if not existing
$extension string
return the ensured file/path with extension
Beispiel #1
0
 public function testEnsureExtension()
 {
     $this->assertSame('path/to/image.png', FileHelper::ensureExtension('path/to/image.png', 'gif'));
     $this->assertSame('path/to/image.gif', FileHelper::ensureExtension('path/to/image', 'gif'));
     // twig example as used in element component
     $this->assertSame('file.twig', FileHelper::ensureExtension('file', 'twig'));
     $this->assertSame('path/to/file.twig', FileHelper::ensureExtension('path/to/file', 'twig'));
     $this->assertSame('path/to/file.twig', FileHelper::ensureExtension('path/to/file.', 'twig'));
     $this->assertSame('path/to/file.twig', FileHelper::ensureExtension('path/to/file.twig', 'twig'));
 }
Beispiel #2
0
 /**
  * Method to render twig files with theyr specific arguments, can be used inside the element closure depending
  * on where the closure was registered. Otherwhise the use of the element variable must be provided.
  *
  * @param string $file The name of the file to render.
  * @param array  $args The parameters to pass in the render file.
  *
  * @return string The render value of the view file.
  */
 public function render($file, array $args = [])
 {
     if ($this->renderEngine == 'php') {
         $view = new View();
         return $view->renderPhpFile(rtrim($this->getFolder(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . FileHelper::ensureExtension($file, 'php'), $args);
     } elseif ($this->renderEngine == 'twig') {
         // @deprecated 1.0.0-RC2 Marked as deprecated and will be removed on 1.0.0 release.
         $twig = Yii::$app->twig->env(new Twig_Loader_Filesystem($this->getFolder()));
         return $twig->render(FileHelper::ensureExtension($file, 'twig'), $args);
     }
     throw new Exception('Not supported render engine: ' . $this->renderEngine);
 }