Skip to content

Cache combined puli.json files of all installed packages #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 243 additions & 0 deletions src/Api/Cache/CacheFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
<?php

/*
* This file is part of the puli/manager package.
*
* (c) Bernhard Schussek <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Puli\Manager\Api\Cache;

use InvalidArgumentException;
use Puli\Manager\Api\Module\InstallInfo;
use Puli\Manager\Api\Module\ModuleFile;
use Puli\Manager\Assert\Assert;

/**
* Stores combined Modules configuration.
*
* @since 1.0
*
* @author Mateusz Sojda <[email protected]>
*/
class CacheFile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can add a small description about the class (and a author tag for you if you want :) ). In other classes as well.

{
/**
* @var string|null
*/
private $path;

/**
* @var ModuleFile[]
*/
private $moduleFiles = array();

/**
* @var InstallInfo[]
*/
private $installInfos = array();

/**
* Creates new CacheFile.
*
* @param string|null $path The path where the cache file is stored.
*/
public function __construct($path = null)
{
Assert::nullOrAbsoluteSystemPath($path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we allow the path to be null? For in-memory caching?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if you create a cache file that is not persisted anywhere. I think this is fine, we're doing the same in PackageFile.


$this->path = $path;
}

/**
* Returns the path to the cache file.
*
* @return string|null The path or `null` if this file is not stored on the
* file system.
*/
public function getPath()
{
return $this->path;
}

/**
* Sets the module files.
*
* @param ModuleFile[] $moduleFiles The module files.
*/
public function setModuleFiles(array $moduleFiles)
{
$this->moduleFiles = array();

foreach ($moduleFiles as $moduleFile) {
$this->addModuleFile($moduleFile);
}
}

/**
* Adds a module to the cache file.
*
* @param ModuleFile $moduleFile The added module file.
*/
public function addModuleFile(ModuleFile $moduleFile)
{
$this->moduleFiles[$moduleFile->getModuleName()] = $moduleFile;

ksort($this->moduleFiles);
}

/**
* Removes a module file from the cache file.
*
* @param string $moduleName The module name.
*/
public function removeModuleFile($moduleName)
{
unset($this->moduleFiles[$moduleName]);
}

/**
* Removes all module files from the cache file.
*/
public function clearModuleFiles()
{
$this->moduleFiles = array();
}

/**
* Returns the module file with the given name.
*
* @param string $moduleName The module name.
*
* @return ModuleFile The module with the passed name.
*
* @throws InvalidArgumentException If the module was not found.
*/
public function getModuleFile($moduleName)
{
Assert::moduleName($moduleName);

if (!isset($this->moduleFiles[$moduleName])) {
throw new InvalidArgumentException(sprintf('Could not find a module named %s.', $moduleName));
}

return $this->moduleFiles[$moduleName];
}

/**
* Returns the module files in the cache file.
*
* @return ModuleFile[] The module files in the cache file.
*/
public function getModuleFiles()
{
return $this->moduleFiles;
}

/**
* Returns whether a module with the given name exists.
*
* @param string $moduleName The module name.
*
* @return bool Whether a module with this name exists.
*/
public function hasModuleFile($moduleName)
{
return isset($this->moduleFiles[$moduleName]);
}

/**
* Returns whether a cache file contains any module files.
*
* @return bool Whether a cache file contains any module files.
*/
public function hasModuleFiles()
{
return count($this->moduleFiles) > 0;
}

/**
* Adds install info to the cache file.
*
* @param InstallInfo $installInfo The module install info.
*/
public function addInstallInfo(InstallInfo $installInfo)
{
$this->installInfos[$installInfo->getModuleName()] = $installInfo;

ksort($this->installInfos);
}

/**
* Removes install info file from the cache file.
*
* @param string $moduleName The module name.
*/
public function removeInstallInfo($moduleName)
{
unset($this->installInfos[$moduleName]);
}

/**
* Removes all module install info from the cache file.
*/
public function clearInstallInfo()
{
$this->installInfos = array();
}

/**
* Returns the install info with the given module name.
*
* @param string $moduleName The module name.
*
* @return InstallInfo The module install info with the passed name.
*
* @throws InvalidArgumentException If the install info was not found.
*/
public function getInstallInfo($moduleName)
{
Assert::moduleName($moduleName);

if (!isset($this->installInfos[$moduleName])) {
throw new InvalidArgumentException(sprintf('Could not find a module named %s.', $moduleName));
}

return $this->installInfos[$moduleName];
}

/**
* Returns the install info for all Modules in the cache file.
*
* @return InstallInfo[] The install info for all Modules in the cache file.
*/
public function getInstallInfos()
{
return $this->installInfos;
}

/**
* Returns whether install info for a module with the given name exists.
*
* @param string $moduleName The module name.
*
* @return bool Whether install info for a module with this name exists.
*/
public function hasInstallInfo($moduleName)
{
return isset($this->installInfos[$moduleName]);
}

/**
* Returns whether a cache file contains any module install info.
*
* @return bool Whether a cache file contains any module install info.
*/
public function hasInstallInfos()
{
return count($this->installInfos) > 0;
}
}
44 changes: 44 additions & 0 deletions src/Api/Cache/CacheManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the puli/manager package.
*
* (c) Bernhard Schussek <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Puli\Manager\Api\Cache;

use Puli\Manager\Api\Context\ProjectContext;
use Puli\Manager\Api\Module\ModuleProvider;

/**
* Manages cached packages information.
*
* @since 1.0
*
* @author Mateusz Sojda <[email protected]>
*/
interface CacheManager extends ModuleProvider
{
/**
* Returns the manager's context.
*
* @return ProjectContext The project context.
*/
public function getContext();

/**
* Reads and returns cache file.
*
* @return CacheFile The cache file.
*/
public function getCacheFile();

/**
* Refreshes the cache file if it contains outdated informations or cache file doesn't exist.
*/
public function refreshCacheFile();
}
3 changes: 3 additions & 0 deletions src/Api/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class Config

const DISCOVERY_STORE_CACHE = 'discovery.store.cache';

const CACHE_FILE = 'cache-file';

/**
* The accepted config keys.
*
Expand Down Expand Up @@ -203,6 +205,7 @@ class Config
self::DISCOVERY_STORE_PORT => true,
self::DISCOVERY_STORE_BUCKET => true,
self::DISCOVERY_STORE_CACHE => true,
self::CACHE_FILE => true,
);

private static $compositeKeys = array(
Expand Down
Loading