Skip to content

Commit

Permalink
Merge pull request #242 from t3solution/5.2.0
Browse files Browse the repository at this point in the history
New release v5.2.0
  • Loading branch information
t3solution authored Aug 14, 2022
2 parents f070f64 + 39eb302 commit 9d04f9f
Show file tree
Hide file tree
Showing 54 changed files with 783 additions and 302 deletions.
79 changes: 77 additions & 2 deletions Classes/Command/CdnToLocal.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
class CdnToLocal extends Command
class CdnToLocal extends CommandBase
{

protected $configurationManager;
const localZipFile = 'googlefont.zip';
const localZipPath = 'fileadmin/T3SB/Resources/Public/CSS/googlefonts/';
const zipFilePath = 'https://google-webfonts-helper.herokuapp.com/api/fonts/';
const localGoogleFile = 'fileadmin/T3SB/Resources/Public/CSS/googlefonts.css';

protected $configurationManager;

public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager)
{
Expand Down Expand Up @@ -73,6 +78,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
$settings['cdn']['fontawesome'] = '5.15.4';
}

if ( !empty($settings['cdn']['googlefonts']) && empty($settings['cdn']['noZip']) ) {
self::getGoogleFonts($settings['cdn']['googlefonts']);
} else {
$localZipPath = GeneralUtility::getFileAbsFileName(self::localZipPath);
if ( is_dir($localZipPath) ) {
parent::rmDir($localZipPath);
}
$cssFile = GeneralUtility::getFileAbsFileName(self::localGoogleFile);
if (file_exists($cssFile)) unlink($cssFile);
}

foreach ($settings['cdn'] as $key=>$version) {

if ($key == 'jquery') {
Expand Down Expand Up @@ -264,6 +280,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}


private function writeCustomFile($customPath, $customFileName, $cdnPath, $extend=false ) {
$customFile = $customPath.$customFileName;
$customContent = GeneralUtility::getURL($cdnPath);
Expand All @@ -285,4 +302,62 @@ private function writeCustomFile($customPath, $customFileName, $cdnPath, $extend
}


private function getGoogleFonts($googleFonts) {
$localZipPath = GeneralUtility::getFileAbsFileName(self::localZipPath);
if ( is_dir($localZipPath) ) {
parent::rmDir($localZipPath);
}
mkdir($localZipPath, 0777, true);
$localZipFile = GeneralUtility::getFileAbsFileName(self::localZipPath.self::localZipFile);
$googleFontsArr = explode(',', $googleFonts);
$cFF = '';
foreach ($googleFontsArr as $key=>$font) {
$fontFamily = trim($font);
$font = str_replace(' ', '-', trim($font));
$zipFilename = strtolower($font).'?download=zip&subsets=latin&variants=regular';

$zipContent = GeneralUtility::getURL(self::zipFilePath . $zipFilename);

if ($zipContent) {
GeneralUtility::writeFile($localZipFile, $zipContent);
$extractTo = $localZipPath;
$zip = new \ZipArchive;
if ($zip->open($localZipFile) === TRUE) {
$zip->extractTo($extractTo);
$zip->close();
} else {
throw new \InvalidArgumentException('Sorry ZIP creation failed at this time!', 1655291667);
}
$zipFile = GeneralUtility::getFileAbsFileName($localZipFile);
if (file_exists($zipFile)) unlink($zipFile);
$googleFiles = scandir($localZipPath);
$css = '';
foreach ($googleFiles as $googleFile) {
if ( str_ends_with($googleFile, 'woff') && $cFF != $fontFamily ) {
$googleFileArr[$key] = $googleFile;
$cFF = $fontFamily;
}
}
} else {
throw new \InvalidArgumentException('Check the spelling of the google fonts!', 1657464667);
}
}

foreach ($googleFileArr as $key=>$googleFile) {
$css .= "@font-face {".LF."
font-family: '".trim($googleFontsArr[$key])."';".LF."
font-style: normal;".LF."
font-weight: 400;".LF."
src: local(''),".LF."
url('googlefonts/".$googleFile."') format('woff2'),".LF."
url('googlefonts/".$googleFile."') format('woff');".LF."
}".LF.LF;
}
$cssFile = GeneralUtility::getFileAbsFileName(self::localGoogleFile);
if (file_exists($cssFile)) unlink($cssFile);
GeneralUtility::writeFile($cssFile, $css);
}



}
70 changes: 70 additions & 0 deletions Classes/Command/CommandBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);

namespace T3SBS\T3sbootstrap\Command;

use Symfony\Component\Console\Command\Command;

/*
* This file is part of the TYPO3 extension t3sbootstrap.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
class CommandBase extends Command
{

/**
* remove dirs
*/
public function rmDir(string $path) : int
{
if (!is_dir ($path)) {
return -1;
}
$dir = @opendir ($path);
if (!$dir) {
return -2;
}
while ($entry = @readdir($dir)) {
if ($entry == '.' || $entry == '..') continue;
if (is_dir ($path.'/'.$entry)) {
$res = self::rmDir ($path.'/'.$entry);
if ($res == -1) {
@closedir ($dir);
return -2;
} else if ($res == -2) {
@closedir ($dir);
return -2;
} else if ($res == -3) {
@closedir ($dir);
return -3;
} else if ($res != 0) {
@closedir ($dir);
return -2;
}
} else if (is_file ($path.'/'.$entry) || is_link ($path.'/'.$entry)) {
$res = @unlink ($path.'/'.$entry);
if (!$res) {
@closedir ($dir);
return -2;
}
} else {
@closedir ($dir);
return -3;
}
}

@closedir ($dir);
$res = @rmdir ($path);

if (!$res) {
return -2;
}

return 0;
}



}
Loading

0 comments on commit 9d04f9f

Please sign in to comment.