Skip to content
Snippets Groups Projects
Commit 2b467101 authored by damjan's avatar damjan
Browse files

[FEATURE] Making TsrefRestService and injecting it to the controller

parent cf456f36
No related branches found
No related tags found
No related merge requests found
......@@ -7,61 +7,35 @@ namespace SGalinski\TypoScriptReferenceFrontend\Controller;
* *
* */
use SGalinski\TypoScriptReferenceFrontend\Service\TsrefRestService;
use TYPO3\Flow\Annotations as Flow;
class TsrefController extends \TYPO3\Flow\Mvc\Controller\ActionController {
/**
* @var TsrefRestService
*/
protected $tsrefRestService;
/**
* The constructor is used for dependency injection.
*
* @param TsrefRestService $service
*/
public function __construct(TsrefRestService $service) {
$this->tsrefRestService = $service;
}
/**
* The action which fetches initial data for typoScript reference page, and displays the page.
*
* @return void
*/
public function indexAction() {
$jsonAttributes = $this->curl_download('http://127.0.0.1:8000/api/attributes');
$jsonAttributes = $this->tsrefRestService->getAttributesJson(TRUE);
$this->view->assign(
'foos', [
'bar', $jsonAttributes
]
'bar', $jsonAttributes
]
);
}
function curl_download($Url) {
// is cURL installed yet?
if (!function_exists('curl_init')) {
die('Sorry cURL is not installed!');
}
// OK cool - then let's create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
// curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// // User agent
// curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
// curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'istype: 1'
));
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
}
\ No newline at end of file
<?php
namespace SGalinski\TypoScriptReferenceFrontend\Service;
use TYPO3\Flow\Annotations as Flow;
/**
* The Class TsrefRestService contains methods for communication with TypoScript REST API.
*
* @package SGalinski\TypoScriptReferenceFrontend\Service
* @Flow\Scope("singleton")
*/
class TsrefRestService {
/**
* URL of REST API resource.
*/
const REST_API_BASE_URL = '127.0.0.1:8000/api/attributes';
/**
* Constant used in $typo3Group field.
*/
const NORMAL_GROUP = 1;
/**
* Constant used in $typo3Group field.
*/
const PAGE_GROUP = 2;
/**
* Constant used in $typo3Group field.
*/
const USER_GROUP = 3;
/**
* Default typo3 version of typoScript attributes
*/
const TYPO3_DEFAULT_VERSION = '6.2';
/**
* @param boolean|null $isType
* @param string $typo3Version
* @param int $typo3Group
* @return mixed
*/
public function getAttributesJson(
$isType = NULL, $typo3Version = self::TYPO3_DEFAULT_VERSION, $typo3Group = self::NORMAL_GROUP
) {
$curlHandle = $this->initialiseCurl();
$headerProperties = [
'version: ' . $typo3Version,
'typo3group: ' . $typo3Group
];
if ($isType !== NULL) {
$headerProperties[] = 'istype: ' . ($isType ? '1' : '0');
}
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headerProperties);
// Download the given URL, and return output
$output = curl_exec($curlHandle);
// Close the cURL resource, and free system resources
curl_close($curlHandle);
return $output;
}
/**
* @return resource
*/
protected function initialiseCurl() {
// is cURL installed yet?
if (!function_exists('curl_init')) {
throw new \RuntimeException('Sorry cURL is not installed. Install cURL.');
}
$curlHandle = curl_init();
// Set URL to download
curl_setopt($curlHandle, CURLOPT_URL, self::REST_API_BASE_URL);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, TRUE);
// Timeout in seconds
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
return $curlHandle;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment