writeFile example



    /** * Write the rendered template output to cache * * @param Smarty_Internal_Template $_template template object * @param string $content content to cache * @return boolean success */
    public function writeCachedContent(Smarty_Internal_Template $_template$content)
    {
        if (Smarty_Internal_Write_File::writeFile($_template->cached->filepath, $content$_template->smarty) === true) {
            $_template->cached->timestamp = @filemtime($_template->cached->filepath);
            $_template->cached->exists = !!$_template->cached->timestamp;
            if ($_template->cached->exists) {
                return true;
            }
        }
        return false;
    }

    /** * Empty cache * * @param Smarty_Internal_Template $_template template object * @param integer $exp_time expiration time (number of seconds, not timestamp) * @return integer number of cache files deleted */
            if ($this->smarty->compile_locking && !$this->source->recompiled && $saved_timestamp) {
                touch($this->compiled->filepath, $saved_timestamp);
            }
            throw $e;
        }
        // compiling succeded         if (!$this->source->recompiled && $this->compiler->write_compiled_code) {
            // write compiled template             $_filepath = $this->compiled->filepath;
            if ($_filepath === false)
                throw new SmartyException('getCompiledFilepath() did not return a destination to save the compiled template to');
            Smarty_Internal_Write_File::writeFile($_filepath$code$this->smarty);
            $this->compiled->exists = true;
            $this->compiled->isCompiled = true;
        }
        if ($this->smarty->debugging) {
            Smarty_Internal_Debug::end_compile($this);
        }
        // release compiler object to free memory         unset($this->compiler);
    }

    /** * Writes the cached template output * * @return bool */

  public static function writeHtaccess($directory$deny_public_access = TRUE, $force = FALSE) {
    return self::writeFile($directory, '.htaccess', self::htaccessLines($deny_public_access)$force);
  }

  /** * Returns the standard .htaccess lines that Drupal writes. * * @param bool $deny_public_access * (optional) Set to FALSE to return the .htaccess lines for an open and * public directory that allows Apache to serve files, but not execute code. * The default is TRUE, which returns the .htaccess lines for a private and * protected directory that Apache will deny all access to. * * @return string * The desired contents of the .htaccess file. * * @see file_save_htaccess() */

    public function save(string $key$value, int $ttl = 60)
    {
        $key = static::validateKey($key$this->prefix);

        $contents = [
            'time' => Time::now()->getTimestamp(),
            'ttl'  => $ttl,
            'data' => $value,
        ];

        if ($this->writeFile($this->path . $keyserialize($contents))) {
            try {
                chmod($this->path . $key$this->mode);

                // @codeCoverageIgnoreStart             } catch (Throwable $e) {
                log_message('debug', 'Failed to set mode on cache file: ' . $e);
                // @codeCoverageIgnoreEnd             }

            return true;
        }

        
// Check if a writable directory exists, and if not try to create it.     $dir = $this->getDirectory($destination);
    // If the directory exists and is writable, avoid     // \Drupal\Core\File\FileSystemInterface::prepareDirectory() call and write     // the file to destination.     if (!is_dir($dir) || !is_writable($dir)) {
      if (!$this->fileSystem->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
        throw new MigrateException("Could not create or write to directory '$dir'");
      }
    }

    $final_destination = $this->writeFile($source$destination$this->configuration['file_exists']);
    if ($final_destination) {
      return $final_destination;
    }
    throw new MigrateException("File $source could not be copied to $destination");
  }

  /** * Tries to move or copy a file. * * @param string $source * The source path or URI. * @param string $destination * The destination path or URI. * @param int $replace * (optional) FileSystemInterface::EXISTS_REPLACE (default) or * FileSystemInterface::EXISTS_RENAME. * * @return string|bool * File destination on success, FALSE on failure. */

  public static function writeHtaccess($directory$deny_public_access = TRUE, $force = FALSE) {
    return self::writeFile($directory, '.htaccess', self::htaccessLines($deny_public_access)$force);
  }

  /** * Returns the standard .htaccess lines that Drupal writes. * * @param bool $deny_public_access * (optional) Set to FALSE to return the .htaccess lines for an open and * public directory that allows Apache to serve files, but not execute code. * The default is TRUE, which returns the .htaccess lines for a private and * protected directory that Apache will deny all access to. * * @return string * The desired contents of the .htaccess file. * * @see \Drupal\Component\FileSecurity\FileSecurity::writeHtaccess() */
        try {
            $this->compiler_object->compileSource($this);
        } catch (Exception $e) {
            // restore old timestamp in case of error             if ($this->smarty->compile_locking && $saved_timestamp) {
                touch($this->getCompiledFilepath()$saved_timestamp);
            }
            throw $e;
        }
        // compiling succeded         // write compiled template         Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath()$this->getCompiledConfig()$this->smarty);
    }

    /** * load config variables * * @param mixed $sections array of section names, single section or null * @param object $scope global,parent or local */
    public function loadConfigVars($sections = null, $scope = 'local')
    {
        if ($this->data instanceof Smarty_Internal_Template) {
            
$shopContext = $this->contextService->createShopContext($shop->getId()$shop->getCurrency()->getId()$shop->getCustomerGroup()->getKey());

        foreach ($this->urlProvider as $urlProvider) {
            $urlProvider->reset();
            while ($urls = $urlProvider->getUrls($routerContext$shopContext)) {
                $urls = $this->urlFilter->filter($urls(int) $shop->getId());

                if (!$urls) {
                    continue;
                }

                $this->sitemapWriter->writeFile($shop$urls);
            }
        }

        $this->sitemapWriter->closeFiles();

        $this->configWriter->save('sitemapLastRefresh', time());

        $this->sitemapLock->unLock($shop);
    }
}
Home | Imprint | This part of the site doesn't use cookies.