Generates an export, download or local file.

@param null|string $path
@return mixed
@throws Exception
511412
protected function generateExport($path = null)
    {
        $temporary = $this->getTemporaryFilePath();

        if ( ! app()->bound('excel')) {
            throw new RuntimeException("Excel exporter strategy expected 'excel' to be bound for IoC");
        }

        /** @var Excel $excel */
        $excel = app('excel');

        $columns    = $this->exportInfo->columns;
        $strategies = $this->getColumnStrategyInstances();

        $excel
            ->create(pathinfo($temporary, PATHINFO_FILENAME), function ($excel) use ($columns, $strategies) {
                /** @var LaravelExcelWriter $excel */

                $excel->setTitle($this->getTitle());

                $excel->sheet($this->getWorksheetTitle(), function($sheet) use ($columns, $strategies) {
                    /** @var LaravelExcelWorksheet $sheet */

                    // Generate header row, if not disabled
                    if ($this->shouldHaveHeaderRow()) {

                        $headers = array_map(
                            function (ModelExportColumnDataInterface $column) {
                                return $column->header();
                            },
                            $columns
                        );

                        $sheet->appendRow($headers);
                        $sheet->freezeFirstRow();

                        // todo: style first row according to options
                    }

                    // For each query chunk: get a model instance,
                    // and for each column, generate the column content using the strategies
                    $this->query->chunk(
                        static::CHUNK_SIZE,
                        function ($records) use ($sheet, $columns, $strategies) {
                            /** @var Collection|Model[] $records */

                            foreach ($records as $record) {

                                $fields = [];

                                foreach ($strategies as $key => $strategy) {
                                    $fields[] = $strategy->render($record, $columns[ $key ]->source);
                                }

                                $sheet->appendRow($fields);

                                // todo: style row according to options
                            }
                        }
                    );
                });
            })
            ->store($this->extension(), pathinfo($temporary, PATHINFO_DIRNAME));

        return $temporary;
    }
339499
public function get($path)
    {
        // Make sure we have a file
        if (empty($path)) {
            return null;
        }

        // Generate file URL
        $url = $this->urlProvider->getUrlFromPath($path);

        // Validate file URL
        if (! filter_var($url, FILTER_VALIDATE_URL)) {
            return null;
        }

        return $url;
    }
907515
public static function getViewContent($path) {
        if ($path === false) {
            throw new \Exception('The view file is not specified. Probably the correct path to the file was not found. Make sure that all paths are specified correctly, the files exists and is available.');
        }

        ob_start();

        require($path);

        $result = ob_get_contents();

        ob_end_clean();

        return $result;
    }
925251
public function setPath($path)
    {
        $this->path = $path;
        $fileFullPath = "{$this->rootPath}/{$this->path}";
        // readable ?
        if (file_exists($fileFullPath)) {
            $this->file = $fileFullPath;
            return $this;
        }
        // absolute path ?
        if (file_exists($path) && strpos($path, $this->rootPath) === 0) {
            $this->file = $path;
            $this->rootPath = '/';
        } else {
            error_log("[BEAR.Ace] 404: {$fileFullPath}");
            throw new Exception('Not found ' . $fileFullPath, 404);
        }

        return $this;
    }
542796
public function create($path = null)
    {
        $media = new $this->class();

        if ($path !== null) {
            $media->setFile(new \Symfony\Component\HttpFoundation\File\File($path));
        }

        return $media;
    }
1269973
public function fetch($path) {
        $result = $this->result;
        $keys = explode(".", $path);
        foreach ($keys as $key) {
            if (!array_key_exists($key, $result)) {
                return null;
            }
            $result = $result[$key];
        }
        return $result;
    }
975399
public static function at($path)
    {
        $file = new static($path);

        if (! $file->exists()) {
            throw new NonExistentFileException("File does not exist at $path");
        }

        if (! $file->isReadable()) {
            throw new FilePermissionsException("File not readable at $path");
        }

        return $file;
    }
891348
public function download(string $path)
    {
        $arguments = [
            'path' => $this->normalizePath($path),
        ];

        $response = $this->contentEndpointRequest('files/download', $arguments);

        return StreamWrapper::getResource($response->getBody());
    }
631871
public function download($path)
    {
        $this->setupRequest([
            'path' => $this->normalizePath($path),
        ]);

        $this->apiEndpoint = 'files/download';

        $this->content = null;

        $response = $this->doDropboxApiContentRequest();

        return StreamWrapper::getResource($response->getBody());
    }
341040
public function createZipFile($path = null)
    {
        if ($path === null) {
            $path = tempnam(sys_get_temp_dir(), 'temp-zip-');
        }
        $zip = new \ZipArchive();
        $zip->open($path, \ZipArchive::CREATE);
        $this->addYmlFiles($zip);
        $zip->close();

        return $path;
    }
1224962
public function execute($path = null)
    {
        $content = '';

        /*
         * Let's first take out strings, comments and regular expressions.
         * All of these can contain JS code-like characters, and we should make
         * sure any further magic ignores anything inside of these.
         *
         * Consider this example, where we should not strip any whitespace:
         * var str = "a   test";
         *
         * Comments will be removed altogether, strings and regular expressions
         * will be replaced by placeholder text, which we'll restore later.
         */
        $this->extractStrings('\'"`');
        $this->stripComments();
        $this->extractRegex();

        // loop files
        foreach ($this->data as $source => $js) {
            // take out strings, comments & regex (for which we've registered
            // the regexes just a few lines earlier)
            $js = $this->replace($js);

            $js = $this->propertyNotation($js);
            $js = $this->shortenBools($js);
            $js = $this->stripWhitespace($js);

            // combine js: separating the scripts by a ;
            $content .= $js.";";
        }

        // clean up leftover `;`s from the combination of multiple scripts
        $content = ltrim($content, ';');
        $content = (string) substr($content, 0, -1);

        /*
         * Earlier, we extracted strings & regular expressions and replaced them
         * with placeholder text. This will restore them.
         */
        $content = $this->restoreExtractedData($content);

        return $content;
    }
1369037
public function reads( $path )
	{
		try {
			$content = $this->fs->get( $path );
		} catch( \Exception $e ) {
			throw new Exception( $e->getMessage(), 0, $e );
		}

		if( ( $stream = tmpfile() ) === false ) {
			throw new Exception( 'Couldn\'t create temporary file' );
		}

		if( fwrite( $stream, $content ) === false ) {
			throw new Exception( 'Couldn\'t write to temporary file' );
		}

		if( rewind( $stream ) === false ) {
			throw new Exception( 'Couldn\'t rewind temporary file' );
		}

		return $stream;
	}
881383
public function compile($path = null)
    {
        if ($path) {
            $this->setPath($path);
        }

        if (!is_null($this->cachePath)) {
            if (!is_file($path)) {
                throw new \RuntimeException("File does not exist at path {$path}");
            }

            file_put_contents(
                $this->getCompiledPath($path),
                $this->compileString(file_get_contents($path))
            );
        }
    }
312767
protected function createDir($path) {
        if (!is_dir($path)) {
            $this->log->info('Create directory %s', $path);

            $status = mkdir($path, 0775, true);

            if ($status === false) {
                throw new Exception(sprintf(
                    'Unable to create directory %s',
                    $path
                ), ExitApp::RUNTIME_ERROR);
            }
        }

        return $this;
    }
551899
public function filesystem(string $path = null)
    {
        if (!$this->filesystem instanceof Filesystem) {
            $local = new Local($path);

            $this->filesystem = new Filesystem($local);
        }

        return $this->filesystem;
    }
460768
public function loadFile($path)
    {
        if (!is_string($path)) {
            throw new InvalidArgumentException(
                'File must be a string'
            );
        }

        if (!file_exists($path)) {
            throw new InvalidArgumentException(
                sprintf('File "%s" does not exist', $path)
            );
        }

        $ext = pathinfo($path, PATHINFO_EXTENSION);
        switch ($ext) {
            case 'php':
                return $this->loadPhpFile($path);

            case 'json':
                return $this->loadJsonFile($path);

            case 'ini':
                return $this->loadIniFile($path);

            case 'yml':
            case 'yaml':
                return $this->loadYamlFile($path);
        }

        $validConfigExts = [ 'ini', 'json', 'php', 'yml' ];
        throw new InvalidArgumentException(sprintf(
            'Unsupported file format for "%s"; must be one of "%s"',
            $path,
            implode('", "', $validConfigExts)
        ));
    }
488613
public function mkdir(string $path = ""): File {
		if (!empty($path)) {
			$dir = new File(Paths::appendPath($this->getUrl(), $path));
			$dir->mkdir();

			return $dir;
		} else {
			if (!$this->isDirectory() && !$this->isFile()) {
				$this->manager->createDir($this->getUrl());
			}

			return $this;
		}
	}