Example #1
0
 public function save($alias, array $data = [], array $segments = [])
 {
     // page data
     $content = '---' . PHP_EOL;
     $content .= Yaml::dump($data);
     $content .= '---' . PHP_EOL;
     // page segments
     if (array_key_exists(0, $segments)) {
         $content .= $segments[0];
         $content .= PHP_EOL;
         unset($segments[0]);
     }
     if (array_key_exists('', $segments)) {
         $content .= $segments[''];
         $content .= PHP_EOL;
         unset($segments['']);
     }
     foreach ($segments as $key => $value) {
         $content .= '--- ' . $key . ' ---' . PHP_EOL;
         $content .= $value;
         $content .= PHP_EOL;
     }
     $path = $this->alias->get($alias);
     return file_put_contents($path, $content);
 }
Example #2
0
 public static function updateData($filepath, array $data)
 {
     $content = file_get_contents($filepath);
     $matches = self::pregMatch($content);
     if (count($matches) == 3) {
         $newContent = '';
         $newContent .= '---' . PHP_EOL;
         $newContent .= Yaml::dump($data);
         $newContent .= '---' . PHP_EOL;
         $newContent .= $matches[2];
         return file_put_contents($filepath, $newContent);
     }
     return false;
 }
 public function editAction($request)
 {
     $path = $request->getQuery('path', null);
     $absPath = $this->alias->get($path);
     // Config
     $name = pathinfo($absPath, PATHINFO_FILENAME);
     $config = $this->config->get('plugins.config.adminpanel.data.' . $name . '.config');
     if (is_null($config)) {
         return $this->editAsString($request);
     }
     $saved = false;
     if ($this->request->getMethod() == 'POST') {
         $data = $request->getPost('data', []);
         $content = Yaml::dump(array_values($data));
         $saved = file_put_contents($absPath, $content);
     }
     return $this->render('data/edit.twig', ['config' => $config, 'data' => Yaml::parseFile($absPath), 'saved' => $saved]);
 }
 public function reformatFileAction($request)
 {
     $name = $request->getPost('name');
     $files = $this->getYamlFiles();
     if (empty($name) || !array_key_exists($name, $files)) {
         $this->sendErrorHeader($this->t('Invalid parameter!'));
     }
     if (!is_file($files[$name]['path'])) {
         $this->sendErrorHeader($this->t('{name} does not exist.', ['{name}' => $files[$name]['label']]));
     }
     if (!FilesystemHelper::createBackupFile($files[$name]['path'])) {
         $this->sendErrorHeader($this->t('Backup file can not be created.'));
     }
     $parsed = Yaml::parse($files[$name]['path']);
     $content = Yaml::dump($parsed);
     if (!file_put_contents($files[$name]['path'], $content)) {
         $this->sendErrorHeader($this->t('File can not be created.'));
     }
     echo $this->t('File was formatted and saved.');
     exit;
 }