Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom vcl snippets #179

Merged
merged 10 commits into from
Jun 30, 2018
74 changes: 74 additions & 0 deletions Block/System/Config/Form/Field/CustomSnippetBtn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Fastly\Cdn\Block\System\Config\Form\Field;

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

/**
* Class CustomSnippetBtn
*
* @package Fastly\Cdn\Block\System\Config\Form\Field
*/
class CustomSnippetBtn extends Field
{
protected function _construct() // @codingStandardsIgnoreLine - required by parent class
{
$this->_template = 'Fastly_Cdn::system/config/form/field/customSnippetBtn.phtml';

parent::_construct();
}

/**
* Remove scope label
*
* @param AbstractElement $element
* @return string
*/
public function render(AbstractElement $element)
{
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
return parent::render($element);
}

/**
* Return element html
*
* @param AbstractElement $element
* @return string
*/
protected function _getElementHtml(AbstractElement $element) // @codingStandardsIgnoreLine - required by parent class
{
return $this->_toHtml();
}

/**
* Return ajax url for collect button
*
* @return string
*/
public function getAjaxUrl()
{
return $this->getUrl('adminhtml/fastlyCdn/vcl/serviceinfo');
}

/**
* Generate upload button html
*
* @return mixed
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getButtonHtml()
{
$button = $this->getLayout()->createBlock(
'Magento\Backend\Block\Widget\Button'
)->setData(
[
'id' => 'fastly_custom_vcl_button',
'label' => __('Create'),
]
);

return $button->toHtml();
}
}
115 changes: 115 additions & 0 deletions Block/System/Config/Form/Field/CustomSnippets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* Fastly CDN for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Fastly CDN for Magento End User License Agreement
* that is bundled with this package in the file LICENSE_FASTLY_CDN.txt.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Fastly CDN to newer
* versions in the future. If you wish to customize this module for your
* needs please refer to http://www.magento.com for more information.
*
* @category Fastly
* @package Fastly_Cdn
* @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com)
* @license BSD, see LICENSE_FASTLY_CDN.txt
*/
namespace Fastly\Cdn\Block\System\Config\Form\Field;

use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\Data\Form\Element\Factory;

/**
* Backend system config array field renderer
*/
class CustomSnippets extends AbstractFieldArray
{
/**
* @var Factory
*/
private $elementFactory;

/**
* Backend constructor.
*
* @param Context $context
* @param Factory $elementFactory
* @param array $data
*/
public function __construct(
Context $context,
Factory $elementFactory,
array $data = []
) {
$this->elementFactory = $elementFactory;

parent::__construct($context, $data);
}

/**
* Initialise form fields
*
* @return void
*/
protected function _construct() // @codingStandardsIgnoreLine - required by parent class
{
$this->addColumn('snippet_name', ['label' => __('Name')]);
$this->_addAfter = false;
$this->_template = 'Fastly_Cdn::system/config/form/field/customSnippets.phtml';

parent::_construct();
}

/**
* Render array cell for prototypeJS template
*
* @param string $columnName
* @return mixed|string
* @throws \Exception
*/
public function renderCellTemplate($columnName)
{
if ($columnName == 'store_id' && isset($this->_columns[$columnName])) {
$options = $this->getOptions(__('-- Select Store --'));
$element = $this->elementFactory->create('select');
$element->setForm(
$this->getForm()
)->setName(
$this->_getCellInputElementName($columnName)
)->setHtmlId(
$this->_getCellInputElementId('<%- _id %>', $columnName)
)->setValues(
$options
);
return str_replace("\n", '', $element->getElementHtml());
}

return parent::renderCellTemplate($columnName);
}

/**
* Get list of store views.
*
* @param bool|false $label
* @return array
*/
protected function getOptions($label = false) // @codingStandardsIgnoreLine - required by parent class
{
$options = [];
foreach ($this->_storeManager->getStores() as $store) {
$options[] = ['value' => $store->getId(), 'label' => $store->getName()];
}

if ($label) {
array_unshift($options, ['value' => '', 'label' => $label]);
}

return $options;
}
}
117 changes: 117 additions & 0 deletions Controller/Adminhtml/FastlyCdn/Vcl/CreateCustomSnippet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Vcl;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\Directory\WriteFactory;
use Magento\Framework\Controller\Result\JsonFactory;

/**
* Class CreateCustomSnippet
*
* @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Vcl
*/
class CreateCustomSnippet extends Action
{
/**
* @var RawFactory
*/
private $resultRawFactory;
/**
* @var FileFactory
*/
private $fileFactory;
/**
* @var DirectoryList
*/
private $directoryList;
/**
* @var WriteFactory
*/
private $writeFactory;
/**
* @var JsonFactory
*/
private $resultJson;

public function __construct(
Context $context,
RawFactory $resultRawFactory,
FileFactory $fileFactory,
DirectoryList $directoryList,
WriteFactory $writeFactory,
JsonFactory $resultJsonFactory
) {
$this->resultRawFactory = $resultRawFactory;
$this->fileFactory = $fileFactory;
$this->directoryList = $directoryList;
$this->writeFactory = $writeFactory;
$this->resultJson = $resultJsonFactory;

parent::__construct($context);
}

/**
* @return $this|\Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$result = $this->resultJson->create();
try {
$name = $this->getRequest()->getParam('name');
$type = $this->getRequest()->getParam('type');
$priority = $this->getRequest()->getParam('priority');
$vcl = $this->getRequest()->getParam('vcl');

$fileDirectory = DirectoryList::VAR_DIR;
$snippetName = $this->validateCustomSnippet($name, $type, $priority);
$fileName = $type . '_' . $priority . '_' . $snippetName . '.vcl';

$write = $this->writeFactory->create($fileDirectory . '/vcl_snippets_custom/');
$write->writeFile($fileName, $vcl);

return $result->setData([
'status' => true
]);
} catch (\Exception $e) {
return $result->setData([
'status' => false,
'msg' => $e->getMessage()
]);
}
}

/**
* @param $name
* @param $type
* @param $priority
* @return mixed
* @throws LocalizedException
*/
private function validateCustomSnippet($name, $type, $priority)
{
$snippetName = str_replace(' ', '', $name);
$types = ['init', 'recv', 'hit', 'miss', 'pass', 'fetch', 'error', 'deliver', 'log', 'none'];

$inArray = in_array($type, $types);
$isNumeric = is_numeric($priority);
$isAlphanumeric = preg_match('/^[\w]+$/', $snippetName);

if (!$inArray) {
throw new LocalizedException(__('Type value is not recognised.'));
}
if (!$isNumeric) {
throw new LocalizedException(__('Please make sure that the priority value is a number.'));
}
if (!$isAlphanumeric) {
throw new LocalizedException(__('Please make sure that the name value contains only
alphanumeric characters.'));
}
return $snippetName;
}
}
82 changes: 82 additions & 0 deletions Controller/Adminhtml/FastlyCdn/Vcl/DeleteCustomSnippet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Vcl;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Filesystem\Driver\File;
use Fastly\Cdn\Model\Config;
use Fastly\Cdn\Model\Config\Backend\CustomSnippetUpload;

class DeleteCustomSnippet extends Action
{
/**
* @var Http
*/
private $request;

/**
* @var JsonFactory
*/
private $resultJson;

/**
* @var Config
*/
private $config;

private $customSnippetUpload;

private $file;

/**
* DeleteCustomSnippet constructor.
*
* @param Context $context
* @param Http $request
* @param JsonFactory $resultJsonFactory
* @param Config $config
* @param CustomSnippetUpload $customSnippetUpload
* @param File $file
*/
public function __construct(
Context $context,
Http $request,
JsonFactory $resultJsonFactory,
Config $config,
CustomSnippetUpload $customSnippetUpload,
File $file
) {
$this->request = $request;
$this->resultJson = $resultJsonFactory;
$this->config = $config;
$this->customSnippetUpload = $customSnippetUpload;
$this->file = $file;

parent::__construct($context);
}

public function execute()
{
$result = $this->resultJson->create();

try {
$snippet = $this->getRequest()->getParam('snippet_id');
$customSnippetPath = $this->customSnippetUpload->getUploadDirPath('vcl_snippets_custom');

if ($this->file->isExists($customSnippetPath . '/' . $snippet)) {
$this->file->deleteFile($customSnippetPath . '/' . $snippet);
}
return $result->setData([
'status' => true
]);
} catch (\Exception $e) {
return $result->setData([
'status' => false,
'msg' => $e->getMessage()
]);
}
}
}
Loading