diff --git a/src/installer/extension b/src/installer/extension index a8ae695..bbdcffa 100644 --- a/src/installer/extension +++ b/src/installer/extension @@ -4,7 +4,11 @@ use ncc\Exceptions\ConstantReadonlyException; use ncc\Exceptions\ImportException; use ncc\Exceptions\InvalidConstantNameException; - use ncc\ncc; +use ncc\Exceptions\InvalidPackageNameException; +use ncc\Exceptions\InvalidScopeException; +use ncc\Exceptions\PackageLockException; +use ncc\Exceptions\PackageNotFoundException; +use ncc\ncc; use ncc\Runtime; if(!defined('NCC_INIT')) @@ -112,4 +116,22 @@ Runtime\Constants::delete($package, $name); } } + + if(!function_exists('get_data_path')) + { + /** + * Returns the data path of the package + * + * @param string $package + * @return string + * @throws InvalidPackageNameException + * @throws InvalidScopeException + * @throws PackageLockException + * @throws PackageNotFoundException + */ + function get_data_path(string $package): string + { + return Runtime::getDataPath($package); + } + } } \ No newline at end of file diff --git a/src/ncc/Objects/PackageLock.php b/src/ncc/Objects/PackageLock.php index 5415d8e..989164c 100644 --- a/src/ncc/Objects/PackageLock.php +++ b/src/ncc/Objects/PackageLock.php @@ -65,6 +65,7 @@ $package_entry->addVersion($package, $install_path, true); $package_entry->Name = $package->Assembly->Package; $package_entry->UpdateSource = $package->Header->UpdateSource; + $package_entry->getDataPath(); $this->Packages[$package->Assembly->Package] = $package_entry; $this->update(); @@ -73,6 +74,7 @@ $this->Packages[$package->Assembly->Package]->UpdateSource = $package->Header->UpdateSource; $this->Packages[$package->Assembly->Package]->addVersion($package, true); + $this->Packages[$package->Assembly->Package]->getDataPath(); $this->update(); } diff --git a/src/ncc/Objects/PackageLock/PackageEntry.php b/src/ncc/Objects/PackageLock/PackageEntry.php index c2f95f0..2c58990 100644 --- a/src/ncc/Objects/PackageLock/PackageEntry.php +++ b/src/ncc/Objects/PackageLock/PackageEntry.php @@ -4,11 +4,17 @@ namespace ncc\Objects\PackageLock; + use ncc\Abstracts\Scopes; + use ncc\Exceptions\InvalidPackageNameException; + use ncc\Exceptions\InvalidScopeException; use ncc\Exceptions\VersionNotFoundException; use ncc\Objects\Package; use ncc\Objects\ProjectConfiguration\UpdateSource; use ncc\ThirdParty\jelix\Version\VersionComparator; + use ncc\ThirdParty\Symfony\Filesystem\Filesystem; use ncc\Utilities\Functions; + use ncc\Utilities\PathFinder; + use ncc\Utilities\Resolver; class PackageEntry { @@ -194,6 +200,24 @@ return $r; } + /** + * @return string + * @throws InvalidPackageNameException + * @throws InvalidScopeException + */ + public function getDataPath(): string + { + $path = PathFinder::getPackageDataPath($this->Name); + + if(!file_exists($path) && Resolver::resolveScope() == Scopes::System) + { + $filesystem = new Filesystem(); + $filesystem->mkdir($path, 0777); + } + + return $path; + } + /** * Returns an array representation of the object * diff --git a/src/ncc/Runtime.php b/src/ncc/Runtime.php index 1e00e53..6bb63ba 100644 --- a/src/ncc/Runtime.php +++ b/src/ncc/Runtime.php @@ -10,6 +10,7 @@ use ncc\Classes\PhpExtension\PhpRuntime; use ncc\Exceptions\ImportException; use ncc\Exceptions\PackageLockException; + use ncc\Exceptions\PackageNotFoundException; use ncc\Exceptions\VersionNotFoundException; use ncc\Managers\PackageManager; use ncc\Objects\PackageLock\VersionEntry; @@ -137,6 +138,26 @@ self::addImport($package, $version); } + /** + * Returns the data path of the package + * + * @param string $package + * @return string + * @throws Exceptions\InvalidPackageNameException + * @throws Exceptions\InvalidScopeException + * @throws PackageLockException + * @throws PackageNotFoundException + */ + public static function getDataPath(string $package): string + { + $package = self::getPackageManager()->getPackage($package); + + if($package == null) + throw new PackageNotFoundException(sprintf('Package %s not found', $package)); + + return $package->getDataPath(); + } + /** * @return PackageManager */ diff --git a/src/ncc/Utilities/PathFinder.php b/src/ncc/Utilities/PathFinder.php index 8872291..df457fc 100644 --- a/src/ncc/Utilities/PathFinder.php +++ b/src/ncc/Utilities/PathFinder.php @@ -4,6 +4,7 @@ use ncc\Abstracts\Runners; use ncc\Abstracts\Scopes; + use ncc\Exceptions\InvalidPackageNameException; use ncc\Exceptions\InvalidScopeException; use ncc\Exceptions\RunnerExecutionException; use ncc\ThirdParty\Symfony\Process\ExecutableFinder; @@ -204,6 +205,22 @@ return $results; } + /** + * Returns the path where package data is located + * + * @param string $package + * @return string + * @throws InvalidPackageNameException + * @throws InvalidScopeException + */ + public static function getPackageDataPath(string $package): string + { + if(!Validate::packageName($package)) + throw new InvalidPackageNameException($package); + + return self::getDataPath() . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . $package; + } + /** * Returns the file path where files for the given extension is stored *