Renders a given file as a downloadable content with application/octet-stream
content type. If no filename is given, it will automatically use the actual
file basename.

@param string $filePath
@param null|string $filename
@return Response
181846
protected function download(string $filePath, ?string $filename = null): Response
    {
        return ResponseFactory::getInstance()->buildDownload($filePath, $filename);
    }
114064
private static function parseFile($filePath = '')
    {
        $supportFileType = array();
        $supportFileType = array_merge($supportFileType, self::supportFilePHP());
        $supportFileType = array_merge($supportFileType, self::supportFileYAML());
        foreach ($supportFileType as $value) {
            $file = sprintf('%s.%s', $filePath, $value);
            if (is_file($file)) {
                if (in_array($value, self::supportFilePHP())) {
                    return require($file);
                }
                if (in_array($value, self::supportFileYAML())) {
                    return Yaml::parse(file_get_contents($file));
                }
            }
        }
        return null;
    }
343519
public static function loadFromFile($filepath)
  {
    $info = Yaml::parseFile($filepath);
    $name = str_replace('.profile.yml', '', pathinfo($filepath, PATHINFO_BASENAME));

    $profile = new static();
    $profile->setTitle($info['title'])
            ->setName($name)
            ->setFilepath($filepath);

    if (isset($info['description'])) {
      $profile->setDescription($info['description']);
    }

    if (isset($info['policies'])) {
      $v21_keys = ['parameters', 'severity'];
      foreach ($info['policies'] as $name => $metadata) {
        // Check for v2.0.x style profiles.
        if (!empty($metadata) && !count(array_intersect($v21_keys, array_keys($metadata)))) {
          throw new \Exception("{$info['title']} is a v2.0.x profile. Please upgrade $filepath to v2.2.x schema.");
        }
        $weight = array_search($name, array_keys($info['policies']));
        $profile->addPolicyDefinition(PolicyDefinition::createFromProfile($name, $weight, $metadata));
      }
    }

    if (isset($info['excluded_policies']) && is_array($info['excluded_policies'])) {
      $profile->addExcludedPolicies($info['excluded_policies']);
    }

    if (isset($info['include'])) {
      foreach ($info['include'] as $name) {
        $include = ProfileSource::loadProfileByName($name);
        $profile->addInclude($include);
      }
    }

    if (isset($info['format'])) {
      foreach ($info['format'] as $format => $options) {
        $profile->addFormatOptions(Format::create($format, $options));
      }
    }

    return $profile;
  }
460699
public function attachFile(string $filePath, string $name = null)
    {
        $this->mailer->addAttachment($filePath, $name ?? '');
        return $this;
    }
1280018
public function download($filename)
    {
        return $this->response->withFile($this->getPath($filename, false), ['download' => true]);
    }
570770
protected function parseFile(string $filePath)
    {
        $lexer = new Lexer([
            'usedAttributes' => [
                'comments', 'startLine', 'endLine',
            ],
        ]);

        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, $lexer);

        try {
            $statements = $parser->parse(file_get_contents($filePath));
        } catch (Error $e) {
            return null;
        }

        return $statements;
    }
465928
public function setFilepath($filepath)
    {
        $this->_path = pathinfo($filepath);

        if (!isset($this->_path['extension']))
            $this->_path['extension'] = null;

        return $this;
    }
1069100
public static function openFile($filepath)
    {
        $contents = null;

        if ($realPath = realpath($filepath)) {
            $handle = fopen($realPath, 'rb');
            $contents = fread($handle, filesize($realPath));
            fclose($handle);
        }

        return $contents;
    }
877780
private function save($fileName, $filePath, $extension)
    {
        $fullPath = rtrim($filePath, '/').DIRECTORY_SEPARATOR.$fileName.$extension;

        $template = $this->renderTemplate($fullPath);

        $tempExecutable = tmpfile();
        fwrite($tempExecutable, $template);
        $tempFileName = stream_get_meta_data($tempExecutable)['uri'];
        $cmd = escapeshellcmd("{$this->binPath} --ssl-protocol=any --ignore-ssl-errors=true ".$tempFileName);
        shell_exec($cmd);
        fclose($tempExecutable);

        if (!file_exists($fullPath))
        {
            throw new TimeoutException('Page load timeout.');
        }

        return $fullPath;
    }
5920
private function renderTemplate($filePath)
    {
        return $this->render(array(
            'url' => $this->url,
            'width' => $this->width,
            'height' => $this->height,
            'fullPage' => $this->fullPage,
            'timeout' => $this->timeout * 1000, // convert into milliseconds
            'waitForImages' => $this->waitForImages,
            'imagesLoadingTimeout' => $this->imagesLoadingTimeout * 1000, // convert into milliseconds
        ), $filePath);
    }
766496
private function getFile($filenameWithPath)
    {
        $explodedString = explode('\\', $filenameWithPath);
        header('Content-Type: application/pdf');
        header('Content-Disposition: inline; filename="' . $explodedString[count($explodedString) - 1] . '"');
        readfile(ROOT . $filenameWithPath);
        exit; // $this->autoRender = false; is not enough!
    }
697501
public function uploadShopImage($filePath, $fileName = null)
    {
        $fileName === null && $fileName = pathinfo($filePath, PATHINFO_BASENAME);
        $result = $this->httpRaw(self::WECHAT_SHOP_IMAGE_UPLOAD_URL .
            'access_token=' . $this->getAccessToken() . '&filename=' . $fileName, file_get_contents($filePath));
        return isset($result['errmsg']) && $result['errmsg'] == 'success' ? $result['image_url'] : false;
    }
22443
public function uploadAsset($filePath)
    {
        $params = array( 'file' => '@'.$filePath );
        $params = $this->validator->validate($params);
        $this->validator->validateAssetFile($params['file']);

        try {
            return $this->client->sendRequest('assets', $params, 'POST');
        } catch (\Exception $ex) {
            throw $ex;
        }
    }
747705
public function setFile($filePath, $type = self::CONTENT_APP_STREAM, $suggestedFileName = '', $disposition = 'attachment', $ignore_user_abort = false)
    {
        // Set response headers
        $this->setHeaders($type, $suggestedFileName, $disposition, $ignore_user_abort);

        // Set response content
        $this->content = file_get_contents($filePath);

        return $this;
    }
1163654
public function get($filename, $includePath = false, $streamContext = null)
    {
        return file_get_contents($filename, $includePath, $streamContext);
    }
1362256
private function restoreFile($filePath)
    {
        try {
            for ($i = 0; $i < $this->_config->getHostRetryLimit(); ++$i) {
                /* We try an exclusive creation of the file. This is an atomic operation, it avoid locking mechanism */
                @fopen($filePath, 'x');

                if (false === $fileData = file_get_contents($filePath)) {
                    return null;
                }
                return $fileData;
            }
        } catch (\Exception $e) {
            return null;
        }

        return null;
    }
734592
public static function createEmptyByFilePath(string $filepath) : Repository
    {
        $filename = basename($filepath);
        list($domain, $language, $extension) = explode('.', $filename, 3);

        return new self(
            TranslationCollection::create([]),
            $filepath,
            $language,
            $domain
        );
    }