Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Nov 30, 2020
0 parents commit b32b398
Show file tree
Hide file tree
Showing 27 changed files with 1,422 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Block/Adminhtml/System/Config/Gd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);

namespace Yireo\NextGenImages\Block\Adminhtml\System\Config;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

class Gd extends Field
{
/**
* Override to set a different PHTML template
*
* @return $this
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
$this->setTemplate('config/gd.phtml');

return $this;
}

/**
* Override to render the template instead of the regular output
*
* @param AbstractElement $element
* @return string
*/
protected function _getElementHtml(AbstractElement $element)
{
return $this->toHtml();
}

/**
* Check if GD supports WebP
*
* @return bool
*/
public function hasGdSupport(): bool
{
if (!function_exists('gd_info')) {
return false;
}

if (!function_exists('imagecreatefromwebp')) {
return false;
}

$gdInfo = gd_info();
$webpMatch = false;
foreach ($gdInfo as $gdInfoLine => $gdInfoSupport) {
if (stristr($gdInfoLine, 'webp')) {
$webpMatch = true;
break;
}
}

if ($webpMatch === false) {
return false;
}

return true;
}
}
149 changes: 149 additions & 0 deletions Block/Adminhtml/System/Config/ModuleVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

declare(strict_types=1);

namespace Yireo\NextGenImages\Block\Adminhtml\System\Config;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Filesystem\Directory\ReadFactory as DirectoryReadFactory;
use Magento\Framework\Filesystem\File\ReadFactory as FileReadFactory;
use Magento\Framework\Module\ModuleListInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Backend\Block\Template\Context;

class ModuleVersion extends Field
{
/**
* @var ComponentRegistrar
*/
private $componentRegistrar;

/**
* @var ModuleListInterface
*/
private $moduleList;

/**
* @var FileReadFactory
*/
private $fileReadFactory;

/**
* @var SerializerInterface
*/
private $serializer;

/**
* @var DirectoryReadFactory
*/
private $directoryReadFactory;
/**
* @var string
*/
private $moduleName;

/**
* ModuleVersion constructor.
* @param ComponentRegistrar $componentRegistrar
* @param ModuleListInterface $moduleList
* @param FileReadFactory $fileReadFactory
* @param SerializerInterface $serializer
* @param DirectoryReadFactory $directoryReadFactory
* @param Context $context
* @param array $data
* @param string $moduleName
*/
public function __construct(
ComponentRegistrar $componentRegistrar,
ModuleListInterface $moduleList,
FileReadFactory $fileReadFactory,
SerializerInterface $serializer,
DirectoryReadFactory $directoryReadFactory,
Context $context,
array $data = [],
string $moduleName = 'Yireo_NextGenImages'
) {
parent::__construct($context, $data);
$this->componentRegistrar = $componentRegistrar;
$this->moduleList = $moduleList;
$this->fileReadFactory = $fileReadFactory;
$this->serializer = $serializer;
$this->directoryReadFactory = $directoryReadFactory;
$this->moduleName = $moduleName;
}

/**
* Override to set a different PHTML template
*
* @return $this
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
$this->setTemplate('config/module_version.phtml');

return $this;
}

/**
* Override to render the template instead of the regular output
*
* @param AbstractElement $element
* @return string
*/
protected function _getElementHtml(AbstractElement $element)
{
return $this->toHtml();
}

/**
* Check if GD supports WebP
*
* @return string
*/
public function getModuleVersion(): string
{
$moduleVersion = $this->getModuleVersionFromComposer();
if ($moduleVersion) {
return $moduleVersion;
}

return $this->getModuleVersionFromModuleXml();
}

/**
* @return string
*/
private function getModuleVersionFromComposer(): string
{
$modulePath = $this->componentRegistrar->getPath('module', $this->moduleName);
$composerJsonFile = $modulePath . '/composer.json';

$directoryRead = $this->directoryReadFactory->create($modulePath);
if (!$directoryRead->isExist($composerJsonFile)) {
return '';
}

$fileRead = $this->fileReadFactory->create($composerJsonFile, 'file');
$jsonContents = (string)$fileRead->readAll();
$data = $this->serializer->unserialize($jsonContents);
if (isset($data['version'])) {
return (string)$data['version'];
}
}

/**
* @return string
*/
private function getModuleVersionFromModuleXml(): string
{
$module = $this->moduleList->getOne($this->moduleName);
if (isset($module['setup_version'])) {
return (string)$module['setup_version'];
}

return '';
}
}
44 changes: 44 additions & 0 deletions Block/Adminhtml/System/Config/PhpVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace Yireo\NextGenImages\Block\Adminhtml\System\Config;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

class PhpVersion extends Field
{
/**
* Override to set a different PHTML template
*
* @return $this
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
$this->setTemplate('config/php_version.phtml');

return $this;
}

/**
* Override to render the template instead of the regular output
*
* @param AbstractElement $element
* @return string
*/
protected function _getElementHtml(AbstractElement $element)
{
return $this->toHtml();
}

/**
* Check if GD supports WebP
*
* @return string
*/
public function getPhpVersion(): string
{
return (string)phpversion();
}
}
Loading

0 comments on commit b32b398

Please sign in to comment.