Skip to content

Commit 8950a47

Browse files
authored
Merge pull request #179 from Inchoo/feature/custom-vcl-snippets
Feature/custom vcl snippets
2 parents 92091aa + a08d16a commit 8950a47

File tree

14 files changed

+1042
-4
lines changed

14 files changed

+1042
-4
lines changed

Diff for: Block/System/Config/Form/Field/CustomSnippetBtn.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Fastly\Cdn\Block\System\Config\Form\Field;
4+
5+
use Magento\Config\Block\System\Config\Form\Field;
6+
use Magento\Framework\Data\Form\Element\AbstractElement;
7+
8+
/**
9+
* Class CustomSnippetBtn
10+
*
11+
* @package Fastly\Cdn\Block\System\Config\Form\Field
12+
*/
13+
class CustomSnippetBtn extends Field
14+
{
15+
protected function _construct() // @codingStandardsIgnoreLine - required by parent class
16+
{
17+
$this->_template = 'Fastly_Cdn::system/config/form/field/customSnippetBtn.phtml';
18+
19+
parent::_construct();
20+
}
21+
22+
/**
23+
* Remove scope label
24+
*
25+
* @param AbstractElement $element
26+
* @return string
27+
*/
28+
public function render(AbstractElement $element)
29+
{
30+
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
31+
return parent::render($element);
32+
}
33+
34+
/**
35+
* Return element html
36+
*
37+
* @param AbstractElement $element
38+
* @return string
39+
*/
40+
protected function _getElementHtml(AbstractElement $element) // @codingStandardsIgnoreLine - required by parent class
41+
{
42+
return $this->_toHtml();
43+
}
44+
45+
/**
46+
* Return ajax url for collect button
47+
*
48+
* @return string
49+
*/
50+
public function getAjaxUrl()
51+
{
52+
return $this->getUrl('adminhtml/fastlyCdn/vcl/serviceinfo');
53+
}
54+
55+
/**
56+
* Generate upload button html
57+
*
58+
* @return mixed
59+
* @throws \Magento\Framework\Exception\LocalizedException
60+
*/
61+
public function getButtonHtml()
62+
{
63+
$button = $this->getLayout()->createBlock(
64+
'Magento\Backend\Block\Widget\Button'
65+
)->setData(
66+
[
67+
'id' => 'fastly_custom_vcl_button',
68+
'label' => __('Create'),
69+
]
70+
);
71+
72+
return $button->toHtml();
73+
}
74+
}

Diff for: Block/System/Config/Form/Field/CustomSnippets.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/**
4+
* Fastly CDN for Magento
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Fastly CDN for Magento End User License Agreement
9+
* that is bundled with this package in the file LICENSE_FASTLY_CDN.txt.
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade Fastly CDN to newer
14+
* versions in the future. If you wish to customize this module for your
15+
* needs please refer to http://www.magento.com for more information.
16+
*
17+
* @category Fastly
18+
* @package Fastly_Cdn
19+
* @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com)
20+
* @license BSD, see LICENSE_FASTLY_CDN.txt
21+
*/
22+
namespace Fastly\Cdn\Block\System\Config\Form\Field;
23+
24+
use Magento\Backend\Block\Template\Context;
25+
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
26+
use Magento\Framework\Data\Form\Element\Factory;
27+
28+
/**
29+
* Backend system config array field renderer
30+
*/
31+
class CustomSnippets extends AbstractFieldArray
32+
{
33+
/**
34+
* @var Factory
35+
*/
36+
private $elementFactory;
37+
38+
/**
39+
* Backend constructor.
40+
*
41+
* @param Context $context
42+
* @param Factory $elementFactory
43+
* @param array $data
44+
*/
45+
public function __construct(
46+
Context $context,
47+
Factory $elementFactory,
48+
array $data = []
49+
) {
50+
$this->elementFactory = $elementFactory;
51+
52+
parent::__construct($context, $data);
53+
}
54+
55+
/**
56+
* Initialise form fields
57+
*
58+
* @return void
59+
*/
60+
protected function _construct() // @codingStandardsIgnoreLine - required by parent class
61+
{
62+
$this->addColumn('snippet_name', ['label' => __('Name')]);
63+
$this->_addAfter = false;
64+
$this->_template = 'Fastly_Cdn::system/config/form/field/customSnippets.phtml';
65+
66+
parent::_construct();
67+
}
68+
69+
/**
70+
* Render array cell for prototypeJS template
71+
*
72+
* @param string $columnName
73+
* @return mixed|string
74+
* @throws \Exception
75+
*/
76+
public function renderCellTemplate($columnName)
77+
{
78+
if ($columnName == 'store_id' && isset($this->_columns[$columnName])) {
79+
$options = $this->getOptions(__('-- Select Store --'));
80+
$element = $this->elementFactory->create('select');
81+
$element->setForm(
82+
$this->getForm()
83+
)->setName(
84+
$this->_getCellInputElementName($columnName)
85+
)->setHtmlId(
86+
$this->_getCellInputElementId('<%- _id %>', $columnName)
87+
)->setValues(
88+
$options
89+
);
90+
return str_replace("\n", '', $element->getElementHtml());
91+
}
92+
93+
return parent::renderCellTemplate($columnName);
94+
}
95+
96+
/**
97+
* Get list of store views.
98+
*
99+
* @param bool|false $label
100+
* @return array
101+
*/
102+
protected function getOptions($label = false) // @codingStandardsIgnoreLine - required by parent class
103+
{
104+
$options = [];
105+
foreach ($this->_storeManager->getStores() as $store) {
106+
$options[] = ['value' => $store->getId(), 'label' => $store->getName()];
107+
}
108+
109+
if ($label) {
110+
array_unshift($options, ['value' => '', 'label' => $label]);
111+
}
112+
113+
return $options;
114+
}
115+
}
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Vcl;
4+
5+
use Magento\Framework\App\Action\Action;
6+
use Magento\Framework\App\Action\Context;
7+
use Magento\Framework\Controller\Result\RawFactory;
8+
use Magento\Framework\App\Response\Http\FileFactory;
9+
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Filesystem\Directory\WriteFactory;
12+
use Magento\Framework\Controller\Result\JsonFactory;
13+
14+
/**
15+
* Class CreateCustomSnippet
16+
*
17+
* @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Vcl
18+
*/
19+
class CreateCustomSnippet extends Action
20+
{
21+
/**
22+
* @var RawFactory
23+
*/
24+
private $resultRawFactory;
25+
/**
26+
* @var FileFactory
27+
*/
28+
private $fileFactory;
29+
/**
30+
* @var DirectoryList
31+
*/
32+
private $directoryList;
33+
/**
34+
* @var WriteFactory
35+
*/
36+
private $writeFactory;
37+
/**
38+
* @var JsonFactory
39+
*/
40+
private $resultJson;
41+
42+
public function __construct(
43+
Context $context,
44+
RawFactory $resultRawFactory,
45+
FileFactory $fileFactory,
46+
DirectoryList $directoryList,
47+
WriteFactory $writeFactory,
48+
JsonFactory $resultJsonFactory
49+
) {
50+
$this->resultRawFactory = $resultRawFactory;
51+
$this->fileFactory = $fileFactory;
52+
$this->directoryList = $directoryList;
53+
$this->writeFactory = $writeFactory;
54+
$this->resultJson = $resultJsonFactory;
55+
56+
parent::__construct($context);
57+
}
58+
59+
/**
60+
* @return $this|\Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
61+
*/
62+
public function execute()
63+
{
64+
$result = $this->resultJson->create();
65+
try {
66+
$name = $this->getRequest()->getParam('name');
67+
$type = $this->getRequest()->getParam('type');
68+
$priority = $this->getRequest()->getParam('priority');
69+
$vcl = $this->getRequest()->getParam('vcl');
70+
71+
$fileDirectory = DirectoryList::VAR_DIR;
72+
$snippetName = $this->validateCustomSnippet($name, $type, $priority);
73+
$fileName = $type . '_' . $priority . '_' . $snippetName . '.vcl';
74+
75+
$write = $this->writeFactory->create($fileDirectory . '/vcl_snippets_custom/');
76+
$write->writeFile($fileName, $vcl);
77+
78+
return $result->setData([
79+
'status' => true
80+
]);
81+
} catch (\Exception $e) {
82+
return $result->setData([
83+
'status' => false,
84+
'msg' => $e->getMessage()
85+
]);
86+
}
87+
}
88+
89+
/**
90+
* @param $name
91+
* @param $type
92+
* @param $priority
93+
* @return mixed
94+
* @throws LocalizedException
95+
*/
96+
private function validateCustomSnippet($name, $type, $priority)
97+
{
98+
$snippetName = str_replace(' ', '', $name);
99+
$types = ['init', 'recv', 'hit', 'miss', 'pass', 'fetch', 'error', 'deliver', 'log', 'none'];
100+
101+
$inArray = in_array($type, $types);
102+
$isNumeric = is_numeric($priority);
103+
$isAlphanumeric = preg_match('/^[\w]+$/', $snippetName);
104+
105+
if (!$inArray) {
106+
throw new LocalizedException(__('Type value is not recognised.'));
107+
}
108+
if (!$isNumeric) {
109+
throw new LocalizedException(__('Please make sure that the priority value is a number.'));
110+
}
111+
if (!$isAlphanumeric) {
112+
throw new LocalizedException(__('Please make sure that the name value contains only
113+
alphanumeric characters.'));
114+
}
115+
return $snippetName;
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Vcl;
4+
5+
use Magento\Backend\App\Action;
6+
use Magento\Backend\App\Action\Context;
7+
use Magento\Framework\App\Request\Http;
8+
use Magento\Framework\Controller\Result\JsonFactory;
9+
use Magento\Framework\Filesystem\Driver\File;
10+
use Fastly\Cdn\Model\Config;
11+
use Fastly\Cdn\Model\Config\Backend\CustomSnippetUpload;
12+
13+
class DeleteCustomSnippet extends Action
14+
{
15+
/**
16+
* @var Http
17+
*/
18+
private $request;
19+
20+
/**
21+
* @var JsonFactory
22+
*/
23+
private $resultJson;
24+
25+
/**
26+
* @var Config
27+
*/
28+
private $config;
29+
30+
private $customSnippetUpload;
31+
32+
private $file;
33+
34+
/**
35+
* DeleteCustomSnippet constructor.
36+
*
37+
* @param Context $context
38+
* @param Http $request
39+
* @param JsonFactory $resultJsonFactory
40+
* @param Config $config
41+
* @param CustomSnippetUpload $customSnippetUpload
42+
* @param File $file
43+
*/
44+
public function __construct(
45+
Context $context,
46+
Http $request,
47+
JsonFactory $resultJsonFactory,
48+
Config $config,
49+
CustomSnippetUpload $customSnippetUpload,
50+
File $file
51+
) {
52+
$this->request = $request;
53+
$this->resultJson = $resultJsonFactory;
54+
$this->config = $config;
55+
$this->customSnippetUpload = $customSnippetUpload;
56+
$this->file = $file;
57+
58+
parent::__construct($context);
59+
}
60+
61+
public function execute()
62+
{
63+
$result = $this->resultJson->create();
64+
65+
try {
66+
$snippet = $this->getRequest()->getParam('snippet_id');
67+
$customSnippetPath = $this->customSnippetUpload->getUploadDirPath('vcl_snippets_custom');
68+
69+
if ($this->file->isExists($customSnippetPath . '/' . $snippet)) {
70+
$this->file->deleteFile($customSnippetPath . '/' . $snippet);
71+
}
72+
return $result->setData([
73+
'status' => true
74+
]);
75+
} catch (\Exception $e) {
76+
return $result->setData([
77+
'status' => false,
78+
'msg' => $e->getMessage()
79+
]);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)