Skip to content

Commit

Permalink
Cache combined puli.json files of all installed packages
Browse files Browse the repository at this point in the history
  • Loading branch information
msojda committed Feb 9, 2016
1 parent 3ebd862 commit 0ec12b1
Show file tree
Hide file tree
Showing 21 changed files with 1,874 additions and 59 deletions.
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 Module.
*
* (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
{
/**
* @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);

$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
47 changes: 47 additions & 0 deletions src/Api/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Puli\Discovery\Api\Discovery;
use Puli\Discovery\Api\EditableDiscovery;
use Puli\Manager\Api\Asset\AssetManager;
use Puli\Manager\Api\Cache\CacheManager;
use Puli\Manager\Api\Config\Config;
use Puli\Manager\Api\Config\ConfigFileManager;
use Puli\Manager\Api\Context\Context;
Expand All @@ -33,6 +34,8 @@
use Puli\Manager\Api\Storage\Storage;
use Puli\Manager\Assert\Assert;
use Puli\Manager\Asset\DiscoveryAssetManager;
use Puli\Manager\Cache\CacheFileConverter;
use Puli\Manager\Cache\CacheManagerImpl;
use Puli\Manager\Config\ConfigFileConverter;
use Puli\Manager\Config\ConfigFileManagerImpl;
use Puli\Manager\Config\DefaultConfig;
Expand Down Expand Up @@ -284,6 +287,16 @@ class Container
*/
private $logger;

/**
* @var CacheFileConverter|null
*/
private $cacheFileConverter;

/**
* @var CacheManager|null
*/
private $cacheManager;

/**
* @var bool
*/
Expand Down Expand Up @@ -1007,6 +1020,40 @@ public function getJsonValidator()
return $this->jsonValidator;
}

/**
* Returns the cache file converter.
*
* @return CacheFileConverter The cache file converter.
*/
public function getCacheFileConverter()
{
if (!$this->cacheFileConverter) {
$this->cacheFileConverter = new CacheFileConverter(new ModuleFileConverter(
$this->getJsonVersioner()
));
}

return $this->cacheFileConverter;
}

/**
* Returns the cached configuration manager.
*
* @return CacheManager The cached configuration manager.
*/
public function getCacheManager()
{
if (!$this->cacheManager && $this->context instanceof ProjectContext) {
$this->cacheManager = new CacheManagerImpl(
$this->getModuleManager(),
$this->getJsonStorage(),
$this->context
);
}

return $this->cacheManager;
}

private function activatePlugins()
{
foreach ($this->context->getRootModuleFile()->getPluginClasses() as $pluginClass) {
Expand Down
Loading

0 comments on commit 0ec12b1

Please sign in to comment.