Loads a package from a given directory.

This function returns the string of the primary package class.

@throws InvalidPathException Thrown when the package directory
does not exist.
@throws NewUpException
@param  $directory
@return string
19978
public function loadPackage($directory)
    {
        if (!$this->files->exists($directory)) {
            throw new InvalidPathException("The directory {$directory} does not exist.");
        }

        if (!$this->files->exists($directory . '/composer.json')) {
            throw new InvalidPathException("There is no composer.json file in {$directory}");
        }

        if (!$this->files->exists($directory . '/_newup')) {
            throw new InvalidPathException("There is no _newup directory in {$directory}");
        }

        if (!$this->files->exists($directory . '/_newup/Package.php')) {
            throw new InvalidPathException("A Package.php file must be present in in \"_newup\" directory.");
        }

        $package = Package::fromFile($directory . '/composer.json',
            user_config('configuration.strictComposerValues', true));
        $namespace = package_vendor_namespace($package->getVendor(), $package->getPackage(), true);

        add_psr4($namespace, $directory . '/_newup');


        if (!class_exists($namespace . 'Package', true)) {
            throw new NewUpException("A valid reachable class named 'Package' must be defined.");
        }

        if (!is_subclass_of($namespace . 'Package', BasePackageTemplate::class)) {
            throw new NewUpException("The 'Package' class must extend ".BasePackageTemplate::class);
        }

        return $namespace . 'Package';
    }
625813
public function read($directory) {
		$directory = realpath($directory);
		$filename = $this->getExtensionConfigurationFilename($directory);
		$configuration = $this->readExtensionConfigurationFile($filename);
		return array($configuration[self::PARAMETER_VERSION], $configuration[self::PARAMETER_STABILITY]);
	}
504963
protected function _makeDirectory( $directory )
    {
        try
        {
            if ( !is_dir( $directory ) && !is_file( $directory ) && !is_link( $directory ) )
            {
                mkdir( $directory, 0755, true );
            }
        }
        catch ( \Exception $e )
        {
            $this->error( $e->getMessage() );
            die;
        }
    }
688090
public function setDirectory($directory)
	{
		// trim trailling slash
		$directory = rtrim($directory,"/");
		$dir       = opendir($directory);
		if ($dir === false)
		{
			throw new UnableToSetException("Could not set '$directory'. It is not a directory!");
		}


		while (($file = readdir($dir)) !== false)
		{
			$path      = $directory.'/'.$file;
			$pathInfo  = pathinfo($path);
			$extension = strtolower($pathInfo['extension']);
			$filename  = $pathInfo['filename'];
			if ($extension == 'php')
			{
				$strings = include $path;
				if (is_array($strings))
				{
					$this->collection->setArray($strings, $filename);
				}
			}
		}
		return $this;
	}
1182299
public function rmdir($directory) {
        if (false === @ftp_rmdir($this->getConnection(), $directory)) {
            throw $this->newFTPException(sprintf("rmdir %s failed", $directory));
        }
        return $this;
    }
208196
public function addDirectory($directory)
    {
        if (!file_exists($directory) || !is_dir($directory)) {
            throw new \Exception('The configuration directory does not exist.');
        } else {
            if (substr($directory, -1) != DIRECTORY_SEPARATOR) {
                $directory .= DIRECTORY_SEPARATOR;
            }

            foreach (glob($directory . '*.yml') as $file) {
                self::addFile($file);
            }
        }

        return self::getInstance();
    }
1300321
protected static function listAssets($directory) {

        if (false === is_dir($directory)) {
            throw new InvalidArgumentException(sprintf("\"%s\" is not a directory", $directory));
        }

        $assets = [];

        foreach (new DirectoryIterator(realpath($directory)) as $current) {

            // Check the filename and his extension.
            if (true === in_array($current->getFilename(), [".", ".."]) || 0 === preg_match("/\.zip$/", $current->getFilename())) {
                continue;
            }

            $assets[] = $current->getPathname();
        }

        sort($assets);

        return $assets;
    }
511294
public function findClasses($bundles, $directory = '', $type = '')
    {
        $finder = Finder::create()->files()->name('*.php')->sortByName()->ignoreUnreadableDirs();
        $found  = false;

        foreach ($bundles as $bundle) {
            $path = rtrim($bundle->getPath(), DIRECTORY_SEPARATOR);

            if (true === empty($directory)) {
                $found = true;
                $finder->in($path);
                continue;
            }

            if (false === is_array($directory)) {
                $directory = [$directory];
            }

            foreach ($directory as $dir) {
                $pathname = sprintf('%s%s%s', $path, DIRECTORY_SEPARATOR, trim($dir, DIRECTORY_SEPARATOR));
                if (true === is_dir($pathname)) {
                    $found = true;
                    $finder->in($pathname);
                }
            }
        }

        if (false === $found) {
            return [];
        }

        $files = [];
        foreach ($finder as $file) {
            $files[] = $file->getPathname();
        }

        $classes = [];
        foreach ($files as $file) {
            foreach ($bundles as $bundle) {
                if (null !== $class = $this->getClassFromFileAndBundle($file, $bundle)) {
                    $classes[] = $class;
                    continue;
                }
            }
        }

        $classes = array_filter($classes, function ($e) {
            return class_exists($e);
        });

        if (false === empty($type)) {
            if (false === (class_exists($type) || interface_exists($type))) {
                throw new \InvalidArgumentException(sprintf('Class or interface "%s" unknown', $type));
            }
            $classes = array_filter($classes, function ($e) use ($type) {
                return is_subclass_of($e, $type);
            });
        }

        return $classes;
    }
1305834
static function contents($directory) {
        if (!self::exists($directory)) return null;

        $dircontents = [];

        $contents = scandir($directory);

        foreach ($contents as $content) {
            if ($content != '.' && $content != '..') {
                array_push($dircontents, $content);
            }
        }

        return $dircontents;
    }
371236
public function getPlugin(string $directory)
    {
        $files = $this->files->files($this->app->mupluginsPath($directory));

        foreach ($files as $file) {
            $headers = $this->headers($file->getRealPath(), $this->headers);

            if (! empty($headers['name'])) {
                return array_merge(['root' => $file->getFilename()], $headers);
                break;
            }
        }
    }
454650
public function isIn($directory, $cwd = null)
	{
		$dir = Path::info($directory);
		if ($dir->isEmpty()) {
			throw new \InvalidArgumentException('Directory is empty.');
		}
		$dir = $dir->normalize()->assumeDirectory();
		$self = $this->normalize();
		
		if (! $dir->isAbsolute() && is_null($cwd)) {
			throw new \InvalidArgumentException(sprintf('Directory "%s" is not absolute, you have to provide a working directory.', $directory));
		}
		if (! $self->isAbsolute() && is_null($cwd)) {
			throw new \InvalidArgumentException(sprintf('Path "%s" is not absolute, you have to provide a working directory.', $this));
		}
		if (! is_null($cwd)) {
			$cwd = Path::info($cwd)->normalize()->assumeDirectory();
			if (! $cwd->isAbsolute()) {
				throw new \InvalidArgumentException(sprintf('Working directory "%s" must be absolute.', $cwd));
			}
			if ($cwd->isEmpty()) {
				throw new \InvalidArgumentException('Working directory is empty.');
			}
			$dir = $dir->abs($cwd);
			$self = $self->abs($cwd);
		}
		
		if (strpos($self->__toString(), $dir->__toString()) === 0) {
			return true;
		}
		return false;
	}
1214360
protected function getConfigPackagePath(string $dir, PackageInterface $package): ?string
    {
        $dir = preg_replace(
            '#^(' . preg_quote(getcwd() . DIRECTORY_SEPARATOR, '#') . ')#',
            '.' . DIRECTORY_SEPARATOR,
            $dir
        );

        $path = $this->getConfigPath($dir);
        if (file_exists($path))
        {
            $this->getLogger()->write(
                sprintf(
                    '  - Found in <info>%s</info>',
                    $package->getPrettyName()
                )
            );

            return $path;
        }

        return null;
    }
843758
public function moveToDirectory($directory)
    {
        $targetPath = rtrim($directory, '/') . '/' . $this->getClientFilename();

        return $this->moveTo($targetPath);
    }
574619
private function ensureDirectoryIsSafe($directory)
    {
        if (
            $directory === '..'
            || strpos($directory, '../') !== false
            || strpos($directory, '/..') !== false
        ) {
            throw new \InvalidArgumentException("File path must not contain ..");
        }
    }
392406
public function getUrl(string $path, ?string $packageName = null): string
    {
        if ($this->status == self::NOT_LOADED) {
            $namedPackages = $this->getNamedPackages();
            if (empty($namedPackages)) {
                $this->package = $this->getDefaultPackage();
                $this->status = self::ONE_PACKAGE;
            } else {
                $defaultPackage = $this->getDefaultPackage();
                $this->packages = new Packages($defaultPackage, $namedPackages);
                $this->status = self::MUTI_PACKAGES;
            }
        }
        if ($this->status == self::ONE_PACKAGE) {
            return $this->package->getUrl($path);
        } else {
            return $this->packages->getUrl($path, $packageName);
        }
    }
1356432
private function getFileMap(PackageInterface $package, $directory)
    {
        $files = [];
        $root  = $this->getInstallPath($package);

        if (!file_exists($root . '/' . $directory)) {
            return [];
        }

        $iterator = new RecursiveDirectoryIterator($root . '/' . $directory, RecursiveDirectoryIterator::SKIP_DOTS);

        /** @var SplFileInfo $file */
        foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
            if ($file->isFile()) {
                $path = str_replace($root . '/' . $directory . '/', '', $file->getPathname());

                $files[$directory . '/' . $path] = $path;
            }
        }

        return $files;
    }
1263182
public function setTemplateDirectory($directory)
    {
        // The directory will be null if a template is being loaded from storage.
        if ($directory == null) {
            $this->templateDirectory = realpath(find_tse_template($this->templateName));
            $this->loadPackageTemplate($this->templateDirectory);
            return;
        }

        $this->templateDirectory = realpath($directory);
        $this->loadPackageTemplate(realpath($directory));
    }