Generic interface to open files.

@param bool   $toWrite   Whether file should be opened for writing
@param string $extension File extension
@param string $name      Verbose file name to report errors

@return file|false File handle
184727
private function _openFile($toWrite, $extension, $name)
    {
        $shp_name = $this->_getFilename($extension);
        $result = @fopen($shp_name, ($toWrite ? 'wb+' : 'rb'));
        if (! $result) {
            $this->setError(sprintf('It wasn\'t possible to open the %s file "%s"', $name, $shp_name));

            return false;
        }

        return $result;
    }
254726
public function extension_loaded($name)
    {
        $code = new Code();
        $code->addStatement(sprintf(
            "return extension_loaded(%s);",
            var_export($name, true)
        ));

        return $this->adapter->run($code);
    }
420769
private function getExtList($name, PaginationInterface $pagination, array $extList = [])
    {
        $ext = $this->extensionRegistry->getExtension($name);
        $deps = $ext->getDependency();
        foreach ($deps as $dep) {
            $extList = $this->getExtList($dep, $pagination, $extList);
        }
        if ($ext->supportPagination($pagination)) {
            $extList[$name] = $ext;
        }

        return $extList;
    }
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;
    }
92771
public static function update($basepath, $name, $contents = null, $area = null)
	{
		$basepath  = rtrim(static::instance($area)->get_path($basepath), '\\/').DS;
		$new_file  = static::instance($area)->get_path($basepath.$name);

		if ( ! $file = static::open_file(@fopen($new_file, 'w'), true, $area))
		{
			if ( ! is_dir($basepath) or ! is_writable($basepath))
			{
				throw new \InvalidPathException('Invalid basepath: "'.$basepath.'", cannot update a file at this location.');
			}

			throw new \FileAccessException('No write access to: "'.$basepath.'", cannot update a file.');
		}

		fwrite($file, $contents);
		static::close_file($file, $area);

		return true;
	}
806932
public static function createFile($aimUrl, $overWrite = false) {
		if (is_file($aimUrl) && $overWrite == false) {
			return false;
		} elseif (is_file($aimUrl) && $overWrite == true) {
			self::unlinkFile($aimUrl);
		}
		$aimDir = dirname($aimUrl);
		self::createDir($aimDir);
		touch($aimUrl);
		return true;
	}
1164771
public function open($save_path, $name)
    {
        foreach ($this->getHandlers() as $handler) {
            $handler->open($save_path, $name);
        }

        return true;
    }
655920
public function write(
        string $filename,
        string $data,
        int $mode = null,
        bool $ensureDirectory = false,
        bool $append = false
    ): bool {
        $mode = $mode ?? self::DEFAULT_FILE_MODE;

        try {
            if ($ensureDirectory) {
                $this->ensureDirectory(dirname($filename), $mode);
            }

            if ($this->exists($filename)) {
                //Forcing mode for existed file
                $this->setPermissions($filename, $mode);
            }

            $result = file_put_contents(
                $filename,
                $data,
                $append ? FILE_APPEND | LOCK_EX : LOCK_EX
            );

            if ($result !== false) {
                //Forcing mode after file creation
                $this->setPermissions($filename, $mode);
            }
        } catch (\Exception $e) {
            throw new WriteErrorException($e->getMessage(), $e->getCode(), $e);
        }

        return $result;
    }
642340
private function _createFile($dir, $name, $ext)
    {
        if (!empty($this->rootDir)) {
            if (!file_exists($this->_getFileDir($dir))) {
                mkdir($this->_getFileDir($dir), 0777, true);
            }
            if (!file_exists($this->_getFilePath($dir, $name, $ext))) {
                if (file_put_contents($this->_getFilePath($dir, $name, $ext), '') === false) {
                    throw new \Exception("Could not create $ext for {$this->username}.");
                }
            }
        }
    }
1041792
public function Save ($path, $name = FALSE, $extension = FALSE)
	{
		
		// Set path/name variables
		
		if (!$this->setPath ($path))
		{
			return FALSE;
		}
		
		if ($name)
		{
			$this->name	= $name;
		}
		
		if ($extension)
		{
			$this->extension	= $extension;
		}
		
		$this->filename	= $this->name . '.' . $this->extension;
		
		// Get tmp path
		
		$tmpPath	= $this->tmpPath ();
		
		if (!$tmpPath)
		{
			return FALSE;
		}
		
		// Move the file
		
		if (!move_uploaded_file ($tmpPath, $this->path . $this->filename))
		{
			
			// Set error
			
			new \Sonic\Message ('error', 'Cannot save uploaded file!');
			
			// Return FALSE
			
			return FALSE;
			
		}
		
		// Return TRUE
		
		return TRUE;
		
	}
568748
private function getViewFilePath($name, $extension = 'html')
    {
        // Check view file
        $viewFile = $this->getViewFile($name, $extension);
        if (!file_exists($viewFile)) {
            // Check for view folder
            $viewBaseName = $this->getViewFolder($name) . DIRECTORY_SEPARATOR . 'view';

            // Select the view file
            $viewFile = (file_exists($viewBaseName . '.php') ? $viewBaseName . '.php' : $viewBaseName . '.html');
            $viewFile = (file_exists($viewFile) ? $viewFile : null);
        }

        // Check if the file is executable (php)
        if (preg_match('/\.php$/', $viewFile)) {
            $this->executable = true;
        }

        return $viewFile;
    }
1271690
public function extend($basedir = null, $overwrite = false)
    {
        if (null === $basedir) {
            $basedir = $this->basedir;
        }

        $basedir = File::realpath($basedir);

        if (false === $this->loadFromCache($basedir)) {
            $this->loadFromBaseDir($basedir, $overwrite);
            $this->saveToCache($basedir);
        }

        if (
            !empty($this->environment)
            && false === strpos($this->environment, $basedir)
            && is_dir($basedir.DIRECTORY_SEPARATOR.$this->environment)
        ) {
            $this->extend($basedir.DIRECTORY_SEPARATOR.$this->environment, $overwrite);
        }

        return $this;
    }
194767
public function name(?string $name = null, ?bool $extension = false)
  {
    if ($name !== null) {
      $this->info['name'] = $name . ($extension ? '.' . pathinfo($this->info['name'], PATHINFO_EXTENSION) : '');
      return $this;
    }

    return $this->info['name'];
  }
383297
private function getViewFile($name, $extension = 'html')
    {
        return $this->viewsConfiguration->getViewsPath($this->app->getBasePath()) . DIRECTORY_SEPARATOR . $name . '.' . $extension;
    }
60656
public static function fileExtension($name, $realFile = false) {
		if ($realFile) {
			$result = pathinfo($name, PATHINFO_EXTENSION);
			if (empty($result)) {
				return false;
			}
		}

		return substr(strrchr($name, '.'), 1);
	}
1196427
public function createChildFile($name)
    {
        if (
            strpos($name, '\\') !== false
            || strpos($name, '/') !== false
            || strpos($name, '..') !== false
        ) {
            throw new InvalidArgumentException("Wrong file name {$name}");
        }

        $class = $this->fileClass;

        return new $class($this->absolutePath . '/' . $name);
    }
1171364
protected function _load($filename, $extension = 'anahita', $overwrite = true)
	{
		$result	= false;

        if (file_exists($filename)) {

            $strings = parse_ini_file($filename, false, INI_SCANNER_RAW);

            if (is_array($strings)){

                if ($overwrite) {
                    $this->_strings = array_merge($this->_strings, $strings);
                } else {
                    $this->_strings = array_merge($strings, $this->_strings);
                }

                $result = true;
			}
        }

		// Record the result of loading the extension's file.
		if (!isset($this->_paths[$extension])) {
			$this->_paths[$extension] = array();
		}

		$this->_paths[$extension][$filename] = $result;

		return $result;
	}