AbstractPosition::publishFile

@param $filePath

@return array
765287
protected function publishFile($filePath)
    {
        $publicFilePath = str_replace(PATH_RESOURCES, PATH_PUBLIC, $filePath);
        $publicFileDir = dirname($publicFilePath) . DIRECTORY_SEPARATOR;

        $extension = pathinfo($filePath, PATHINFO_EXTENSION);

        $publicMinifyFilePath = str_replace('.' . $extension, '.min.' . $extension, $publicFilePath);

        $fileContent = file_get_contents($filePath);
        $fileVersion = $this->getVersion($fileContent);
        
        if (is_file($mapFilePath = $publicFilePath . '.map')) {
            $mapMetadata = json_decode(file_get_contents($mapFilePath), true);
            // if the file version is changed delete it first
            if ( ! hash_equals($fileVersion, $mapMetadata[ 'version' ])) {
                unlink($publicFilePath);
                unlink($publicMinifyFilePath);
                unlink($mapFilePath);
            }
        }

        if ( ! is_file($mapFilePath)) {
            if ( ! empty($fileContent)) {
                $mapMetadata = [
                    'version'        => $fileVersion,
                    'sources'        => [
                        $filePath,
                    ],
                    'names'          => [],
                    'mappings'       => [],
                    'file'           => pathinfo($publicMinifyFilePath, PATHINFO_BASENAME),
                    'sourcesContent' => [
                        $fileContent,
                    ],
                    'sourceRoot'     => '',
                ];
                
                if ( ! is_dir($publicFileDir)) {
                    @mkdir($publicFileDir, 0777, true);
                }

                if (is_writable($publicFileDir)) {
                    if ($fileStream = @fopen($publicFilePath, 'ab')) {
                        flock($fileStream, LOCK_EX);
                        fwrite($fileStream, $fileContent);
                        flock($fileStream, LOCK_UN);
                        fclose($fileStream);

                        // File Map
                        if ($fileStream = @fopen($mapFilePath, 'ab')) {
                            flock($fileStream, LOCK_EX);

                            fwrite($fileStream, json_encode($mapMetadata));

                            flock($fileStream, LOCK_UN);
                            fclose($fileStream);
                        }

                        switch ($extension) {
                            case 'min.css':
                            case 'css':
                                $minifyStyleHandler = new CSS($publicFilePath);
                                $minifyStyleHandler->minify($publicMinifyFilePath);
                                break;

                            case 'min.js':
                            case 'js':
                                $minifyJavascriptHandler = new JS($publicFilePath);
                                $minifyJavascriptHandler->minify($publicMinifyFilePath);
                                break;
                        }

                    }
                }
            }
        }

        return [
            'filePath' => $publicFilePath,
            'url'      => $this->getUrl($publicFilePath),
            'minify'   => [
                'filePath' => $publicMinifyFilePath,
                'url'      => $this->getUrl($publicMinifyFilePath),
            ],
            'version'  => $fileVersion,
        ];
    }
125473
public function makeFileContents($filePath, $extraParams = [])
    {
        $this->controller->vars = array_merge($this->controller->vars, $this->vars);
        return $this->controller->makeFileContents($filePath, $extraParams);
    }
619165
public static function parse(string $filePath): FilePathInterface
    {
        if (!self::myFilePathParse(
            DIRECTORY_SEPARATOR,
            $filePath,
            function ($p, $d, &$e) {
                return self::myPartValidator($p, $d, $e);
            },
            null,
            $isAbsolute,
            $aboveBaseLevel,
            $drive,
            $directoryParts,
            $filename,
            $error)
        ) {
            throw new FilePathInvalidArgumentException('File path "' . $filePath . '" is invalid: ' . $error);
        }

        return new self($isAbsolute, $aboveBaseLevel, $drive, $directoryParts, $filename);
    }
1205810
public static function getXmlContentFromFile ($filePath)
    {
        // Detect media type
        $finfo         = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
        $fileMediaType = finfo_file($finfo, $filePath);
        finfo_close($finfo);

        // Act upon detected media type
        switch ($fileMediaType) {
            case 'application/xml':
                $fileContent = file_get_contents($filePath);
                break;
            case 'application/x-gzip':
                $fileContent = self::gunzip($filePath);
                break;
            case 'application/zip':
                $fileContent = self::unzip($filePath);
                break;
            default:
                throw new Exception("Unsupported file media type: $fileMediaType");
        }

        return $fileContent;
    }
1087615
public static function getIncludePath($filepath)
    {
        static $_include_path = null;

        if ($_path_array === null) {
            $_include_path = explode(PATH_SEPARATOR, get_include_path());
        }

        foreach ($_include_path as $_path) {
            if (file_exists($_path . DS . $filepath)) {
                return $_path . DS . $filepath;
            }
        }
        
        return false;
    }
81245
public function setFile(string $filePath): self
    {
        IOExpect::isFile($filePath);
        IOExpect::isReadable($filePath);

        $this->file = $filePath;

        return $this;
    }
37698
public function publishPackage($package, $packagePath = null)
	{
		list($vendor, $name) = explode('/', $package);

		// First we will figure out the source of the package's configuration location
		// which we do by convention. Once we have that we will move the files over
		// to the "main" configuration directory for this particular application.
		$path = $packagePath ?: $this->packagePath;

		$source = $this->getSource($package, $name, $path);

		return $this->publish($package, $source);
	}
697582
protected function getFileFunctions($filePath)
    {
        $source = file_get_contents($filePath);
        $tokens = token_get_all($source);

        $functions = [];
        $nextStringIsFunc = false;
        foreach($tokens as $token) {
            switch($token[0]) {
                case T_FUNCTION:
                    $nextStringIsFunc = true;
                    break;

                case T_STRING:
                    if ($nextStringIsFunc) {
                        $nextStringIsFunc = false;
                        $functions[] = $token[1];
                    }
                    break;
            }
        }

        return $functions;
    }
373836
public function attachFile(string $filePath, string $name = null)
    {
        $this->attachedFiles[] = [
          'path' => $filePath,
          'name' => $name
        ];

        return $this;
    }
1276972
protected function getFileAsset($filePath)
    {
        $file = new \SplFileInfo($filePath);

        if (!$file->isReadable() || $file->isDir()) {
            return null;
        }

        $filePath = $file->getRealPath();

        return new FileAsset($filePath);
    }
432612
public function writeFile($filePath = null, array $options = [])
    {
        $filePath = empty($filePath)
            ? $this->filePath
            : $filePath;

        $sections = (empty($options)
            ? true
            : $options[ 'sections' ]);

        $content = null;

        if ($sections) {
            foreach ($this->getArrayCopy() as $section => $data) {
                if (is_array($data)) {
                    $content .= '[' . $section . ']' . PHP_EOL;

                    foreach ($data as $key => $value) {
                        if (is_array($value)) {
                            foreach ($value as $valueChild) {
                                $content .= $key . '[] = ' . (is_numeric($valueChild)
                                        ? $valueChild
                                        : '"' . $valueChild . '"') . PHP_EOL;
                            }
                        } elseif (strpos($key, ';') !== false) {
                            $content .= '; ' . trim(ucfirst($key), ';') . ' ' . $value . PHP_EOL;
                        } elseif (empty($value)) {
                            $content .= $key . ' = ' . PHP_EOL;
                        } else {
                            $content .= $key . ' = ' . (is_numeric($value)
                                    ? $value
                                    : '"' . $value . '"') . PHP_EOL;
                        }
                    }

                    $content .= PHP_EOL;
                } elseif (strpos($section, ';') !== false) {
                    $content .= '; ' . trim(ucfirst($section), ';') . ' ' . $data . PHP_EOL;
                } elseif (empty($data)) {
                    $content .= $section . ' = ' . PHP_EOL;
                } else {
                    $content .= $section . ' = ' . (is_numeric($data)
                            ? $data
                            : '"' . $data . '"') . PHP_EOL;
                }
            }
        } else {
            foreach ($this->getArrayCopy() as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $valueChild) {
                        $content .= $key . '[] = ' . (is_numeric($valueChild)
                                ? $valueChild
                                : '"' . $valueChild . '"') . PHP_EOL;
                    }
                } elseif (strpos($key, ';') !== false) {
                    $content .= '; ' . trim(ucfirst($key), ';') . ' ' . $value . PHP_EOL;
                } elseif (empty($value)) {
                    $content .= $key . ' = ' . PHP_EOL;
                } else {
                    $content .= $key . ' = ' . (is_numeric($value)
                            ? $value
                            : '"' . $value . '"') . PHP_EOL;
                }
            }
        }

        return (new File())->write($filePath, $content);
    }
408937
public static function subscribers(string $filePath = null)
    {
        if (is_file($filePath)) {
            /** @noinspection PhpIncludeInspection */
            $subscribers = require_once $filePath;

            $subscribers = is_array($subscribers) ? $subscribers : [];

            foreach ($subscribers as $subscriber) {
                Event::register(IOC::resolve($subscriber));
            }

            Event::dispatch('minimal.loaded.subscribers',
                [$filePath, $subscribers]);
        }
    }
603095
protected function interfacePostCopy(
        string $filePath
    ): void {
        $this->findAndReplaceHelper->replaceFieldInterfaceNamespace($this->interfaceNamespace, $filePath);
        $this->replaceDefaultValueInInterface($filePath);
        $this->postCopy($filePath);
    }
712047
public function readFile($filePath = null, array $options = [])
    {
        $filePath = empty($filePath)
            ? $this->filePath
            : $filePath;

        $result = new ArrayIterator();

        if (false !== ($xml = simplexml_load_file($filePath))) {
            $contents = json_decode(json_encode($xml), true); // force to array conversion

            if (count($contents) == 1) {
                $contents = reset($contents);
            }

            if (json_last_error() === JSON_ERROR_NONE) {
                foreach ($contents as $content) {
                    $result[] = new SplArrayObject($content);
                }
            }
        }

        return $result;
    }
1182988
public function fileGetContents($filePath)
    {
        if ($this->fileExists($filePath) == false) {
            throw new FileSystemException('File '.$filePath.' was not found');
        }
        return file_get_contents($filePath);
    }
181487
public function publish(string $packagePath, array $map, bool $isSymlink, bool $isRelative)
    {
        $packagePath = (string)realpath($packagePath);
        \assert('is_dir($packagePath) && is_readable($packagePath)');
        foreach ($map as $from => $to) {
            $origin = realpath(rtrim($packagePath, '/') . '/' . ltrim($from, '/'));
            \assert('is_dir($origin) && is_readable($origin)');
            $this->filesystem->remove($to);
            $this->filesystem->mkdir(\dirname($to));
            if ($isSymlink) {
                $from = $isRelative
                    ? $this->filesystem->makePathRelative($origin, \dirname($to))
                    : $origin;
                $this->publishSymlink($origin, $from, $to);
            } else {
                $this->io->write(sprintf('Installing assets %s as <comment>hard copies</comment>.', $origin));
                $this->filesystem->mirror($origin, $to);
            }
        }
    }
1019639
public static function providers(string $filePath = null)
    {
        if (is_file($filePath)) {
            /** @noinspection PhpIncludeInspection */
            $providers = require_once $filePath;

            $providers = is_array($providers) ? $providers : [];

            IOC::addProviders($providers);

            Event::dispatch('minimal.loaded.providers',
                [$filePath, $providers]);
        }
    }