/** * @param string $file * @return string */ protected function loadAndPrepareTypoScriptFromFile($file) { $content = Files::getFileContents($file) . chr(10); $content = preg_replace_callback('/include:[ ]?(.*)/', function ($match) use($file) { if (substr($match[1], 0, 11) === 'resource://') { return $match[0]; } return 'include: ' . dirname($file) . '/' . $match[1]; }, $content); return $content; }
/** * @param $simulationFilePath * @return Simulation */ protected function buildSimulation($simulationFilePath) { $simulation = new Simulation(); $simulation->setFilePath($simulationFilePath); $simulationCode = Files::getFileContents($simulationFilePath); preg_match("/package (.*)/", $simulationCode, $package); $simulation->setPackage($package[1]); preg_match("/class (.*) extends Simulation/", $simulationCode, $className); $simulation->setClassName($className[1]); $simulation->setDataPath('Data'); $simulation->setSimulationPath('Simulations'); $simulation->setReportRootPath($this->reportRootPath); return $simulation; }
/** * @param $templateIdentifier * @param \GIB\GradingTool\Domain\Model\Project $project * @param \GIB\GradingTool\Domain\Model\ProjectManager $projectManager * @param string $recipientName * @param string $recipientEmail * @param string $additionalContent * @param array $attachements * @return bool|void */ public function sendNotificationMail($templateIdentifier, \GIB\GradingTool\Domain\Model\Project $project, \GIB\GradingTool\Domain\Model\ProjectManager $projectManager = NULL, $recipientName = '', $recipientEmail = '', $additionalContent = '', $attachements = array()) { if ($this->settings['email']['activateNotifications'] === FALSE) { return TRUE; } /** @var \GIB\GradingTool\Domain\Model\Template $template */ $template = $this->templateRepository->findOneByTemplateIdentifier($templateIdentifier); $templateContentArray = unserialize($template->getContent()); // some kind of wrapper of all e-mail templates containing the HTML structure and styles $beforeContent = Files::getFileContents($this->settings['email']['beforeContentTemplate']); $afterContent = Files::getFileContents($this->settings['email']['afterContentTemplate']); /** @var \TYPO3\Fluid\View\StandaloneView $emailView */ $emailView = new \TYPO3\Fluid\View\StandaloneView(); $emailView->setTemplateSource('<f:format.raw>' . $beforeContent . $templateContentArray['content'] . $afterContent . '</f:format.raw>'); $emailView->setPartialRootPath(FLOW_PATH_PACKAGES . 'Application/GIB.GradingTool/Resources/Private/Partials'); $emailView->setFormat('html'); $emailView->assignMultiple(array('beforeContent' => $beforeContent, 'afterContent' => $afterContent, 'project' => $project, 'projectManager' => $projectManager, 'dataSheetContent' => $project->getDataSheetContentArray(), 'additionalContent' => $additionalContent)); $emailBody = $emailView->render(); /** @var \TYPO3\SwiftMailer\Message $email */ $email = new Message(); $email->setFrom(array($templateContentArray['senderEmail'] => $templateContentArray['senderName'])); // the recipient e-mail can be overridden by method arguments if (!empty($recipientEmail)) { $email->setTo(array((string) $recipientEmail => (string) $recipientName)); // in this case, send a bcc to the GIB team $email->setBcc(array($templateContentArray['senderEmail'] => $templateContentArray['senderName'])); } else { $email->setTo(array($templateContentArray['recipientEmail'] => $templateContentArray['recipientName'])); } if (!empty($attachements)) { foreach ($attachements as $attachement) { $email->attach(\Swift_Attachment::fromPath($attachement['source'])->setFilename($attachement['fileName'])); } } $email->setSubject($templateContentArray['subject']); $email->setBody($emailBody, 'text/html'); $email->send(); }
/** * Render the given template file with the given variables * * @param string $templatePathAndFilename * @param array $contextVariables * @return string * @throws \TYPO3\Fluid\Core\Exception */ protected function renderTemplate($templatePathAndFilename, array $contextVariables) { $templateSource = \TYPO3\Flow\Utility\Files::getFileContents($templatePathAndFilename, FILE_TEXT); if ($templateSource === false) { throw new \TYPO3\Fluid\Core\Exception('The template file "' . $templatePathAndFilename . '" could not be loaded.', 1225709595); } $parsedTemplate = $this->templateParser->parse($templateSource); $renderingContext = $this->buildRenderingContext($contextVariables); return $parsedTemplate->render($renderingContext); }
/** * Reads the TypoScript file from the given path and filename. * If it doesn't exist, this function will just return an empty string. * * @param string $pathAndFilename Path and filename of the TypoScript file * @return string The content of the .ts2 file, plus one chr(10) at the end */ protected function readExternalTypoScriptFile($pathAndFilename) { return is_file($pathAndFilename) ? Files::getFileContents($pathAndFilename) . chr(10) : ''; }
/** * Returns a key by its name * * @param string $name * @return boolean * @throws \TYPO3\Flow\Security\Exception */ public function getKey($name) { if (strlen($name) === 0) { throw new \TYPO3\Flow\Security\Exception('Required name argument was empty', 1334215378); } $keyPathAndFilename = $this->getKeyPathAndFilename($name); if (!file_exists($keyPathAndFilename)) { throw new \TYPO3\Flow\Security\Exception(sprintf('The key "%s" does not exist.', $keyPathAndFilename), 1305812921); } $key = Files::getFileContents($keyPathAndFilename); if ($key === false) { throw new \TYPO3\Flow\Security\Exception(sprintf('The key "%s" could not be read.', $keyPathAndFilename), 1334483163); } if (strlen($key) === 0) { throw new \TYPO3\Flow\Security\Exception(sprintf('The key "%s" is empty.', $keyPathAndFilename), 1334483165); } return $key; }
/** * @param array $paths * @param string $templateNamePrefix * @param string $suffix * @return string */ public function render(array $paths, $templateNamePrefix = NULL, $suffix = '.hbs') { foreach ($paths as $path) { $templates = \TYPO3\Flow\Utility\Files::readDirectoryRecursively($path, $suffix); foreach ($templates as $template) { if (substr($template, 0, strlen($path) + 10) === $path . '/Resources') { $templateName = str_replace(array($path . '/Resources/', $suffix), '', $template); $this->handlebarTemplates[$this->getPrefixedResourceTemplateName($templateName, $templateNamePrefix)] = $template; } elseif (substr($template, 0, strlen($path) + 9) === $path . '/Partials') { $templateName = str_replace(array($path . '/Partials/', $suffix), '', $template); $this->handlebarTemplates['_' . $this->getPrefixedResourceTemplateName($templateName, $templateNamePrefix)] = $template; } else { $templateName = str_replace(array($path . '/', $suffix, '/'), array('', '', '_'), $template); $this->handlebarTemplates[$this->getPrefixedTemplateName($templateName, $templateNamePrefix)] = $template; } } } foreach ($this->handlebarTemplates as $templateName => $template) { $handlebarView = new \TYPO3\Fluid\View\StandaloneView(); $handlebarView->setFormat('html'); $handlebarView->setTemplateSource(\TYPO3\Flow\Utility\Files::getFileContents($template)); $assignHelpers = \TYPO3\Flow\Reflection\ObjectAccess::getPropertyPath($this->settings, 'handlebar.view.assignHelpers.' . $templateName); if (is_array($assignHelpers)) { foreach ($assignHelpers as $variable => $helper) { if (!isset($helper['class']) || !isset($helper['method'])) { continue; } $helperInstance = $this->objectManager->get($helper['class']); $value = call_user_func_array(array($helperInstance, $helper['method']), isset($helper['arguments']) ? $helper['arguments'] : array()); $handlebarView->assign($variable, $value); } } $this->handlebarTemplates[$templateName] = sprintf('<script type="text/x-handlebars" data-template-name="%s">%s</script>', $templateName, $handlebarView->render()); } return implode('', $this->handlebarTemplates); }
/** * Figures out which partial to use. * * @param string $partialName The name of the partial * @return string contents of the partial template * @throws Exception\InvalidTemplateResourceException */ protected function getPartialSource($partialName) { $partialPathAndFilename = $this->getPartialPathAndFilename($partialName); $partialSource = Files::getFileContents($partialPathAndFilename, FILE_TEXT); if ($partialSource === FALSE) { throw new Exception\InvalidTemplateResourceException('"' . $partialPathAndFilename . '" is not a valid template resource URI.', 1257246929); } return $partialSource; }
/** * Convert an object from $source to an entity or a value object. * * @param mixed $source * @param string $targetType * @param array $convertedChildProperties * @param \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration * @return object the target type * @throws \TYPO3\Flow\Property\Exception\InvalidTargetException * @throws \InvalidArgumentException */ public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration = NULL) { if (is_array($source)) { $object = $this->handleArrayData($source, $targetType, $convertedChildProperties, $configuration); } elseif (is_string($source)) { if ($source === '') { return NULL; } $object = $this->fetchObjectFromPersistence($source, $targetType); } else { throw new \InvalidArgumentException('Only strings and arrays are accepted.', 1305630314); } foreach ($convertedChildProperties as $propertyName => $propertyValue) { if ($this->reflectionService->isPropertyAnnotatedWith($targetType, $propertyName, 'Doctrine\\ODM\\CouchDB\\Mapping\\Annotations\\Attachments')) { $attachments = array(); foreach ($propertyValue as $version => $value) { $safeFileName = preg_replace('/[^a-zA-Z-_0-9\\.]*/', '', $value['name']); $attachments[$safeFileName] = \Doctrine\CouchDB\Attachment::createFromBinaryData(\TYPO3\Flow\Utility\Files::getFileContents($value['tmp_name'])); } $propertyValue = $attachments; } $result = \TYPO3\Flow\Reflection\ObjectAccess::setProperty($object, $propertyName, $propertyValue); if ($result === FALSE) { $exceptionMessage = sprintf('Property "%s" having a value of type "%s" could not be set in target object of type "%s". Make sure that the property is accessible properly, for example via an appropriate setter method.', $propertyName, is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue), $targetType); throw new \TYPO3\Flow\Property\Exception\InvalidTargetException($exceptionMessage, 1297935345); } } return $object; }
/** * Load options by provider name or by a settings file (first has precedence) * * @param string $providerName Name of the authentication provider to use * @param string $settingsFile Path to a yaml file containing the settings to use for testing purposes * @return array|mixed * @throws \TYPO3\Flow\Mvc\Exception\StopActionException */ protected function getOptions($providerName = null, $settingsFile = null) { if ($providerName !== null && array_key_exists($providerName, $this->authenticationProvidersConfiguration)) { $this->options = $this->authenticationProvidersConfiguration[$providerName]['providerOptions']; return $this->options; } if ($settingsFile !== null) { if (!file_exists($settingsFile)) { $this->outputLine('Could not find settings file on path %s', [$settingsFile]); $this->quit(1); } $this->options = Yaml::parse(Files::getFileContents($settingsFile)); return $this->options; } $this->outputLine('Neither providerName or settingsFile is passed as argument. You need to pass one of those.'); $this->quit(1); }
/** * @param string $snippetId * @return string * @throws \RuntimeException */ private function loadDefaultSourceFromTemplate($snippetId) { if (!is_dir($this->templateRootPath)) { throw new \RuntimeException(sprintf('Template root path "%s" does not exist.', $this->templateRootPath), 1456827589); } $snippetFileName = str_replace('.', DIRECTORY_SEPARATOR, $snippetId); $possibleTemplatePaths = []; $possibleTemplatePaths[] = Files::concatenatePaths([$this->templateRootPath, $snippetFileName . '.html']); $possibleTemplatePaths[] = Files::concatenatePaths([$this->templateRootPath, $snippetFileName]); foreach ($possibleTemplatePaths as $templatePathAndFilename) { if (is_file($templatePathAndFilename)) { return Files::getFileContents($templatePathAndFilename); } } return ''; }