Skip to content
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

[console] Relocate services cache file. #3404

Merged
merged 1 commit into from
Jul 6, 2017
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ autoload.local.php

# drupal/console-extend-plugin generated files
extend.console.*.yml
*.console.services.yml
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/Bootstrap/AddServicesCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ public function process(ContainerBuilder $container)
* @var Site $site
*/
$site = $container->get('console.site');
\Drupal::getContainer()->set(
'console.root',
$this->root
);

if (!$this->rebuild && $site->cachedServicesFileExists()) {
$loader->load($site->cachedServicesFile());
$loader->load($site->getCachedServicesFile());
} else {
$site->removeCachedServicesFile();
$finder = new Finder();
Expand Down Expand Up @@ -146,9 +150,9 @@ public function process(ContainerBuilder $container)
}
}

if ($servicesData && is_writable($site->getCacheDirectory())) {
if ($servicesData) {
file_put_contents(
$site->cachedServicesFile(),
$site->getCachedServicesFile(),
Yaml::dump($servicesData, 4, 2)
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Bootstrap/Drupal.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ public function boot()
$this->drupalFinder->getComposerRoot()
);

$consoleExtendConfigFile = $this->drupalFinder->getComposerRoot() . DRUPAL_CONSOLE .'/extend.console.config.yml';
$consoleExtendConfigFile = $this->drupalFinder
->getComposerRoot() . DRUPAL_CONSOLE
.'/extend.console.config.yml';
if (file_exists($consoleExtendConfigFile)) {
$container->get('console.configuration_manager')
->importConfigurationFile($consoleExtendConfigFile);
Expand Down
73 changes: 18 additions & 55 deletions src/Utils/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Drupal\Console\Utils;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Logger\LoggerChannelFactory;
Expand All @@ -27,7 +26,7 @@ class Site
/**
* @var string
*/
protected $cacheDirectory;
protected $cacheServicesFile;

/**
* Site constructor.
Expand Down Expand Up @@ -203,72 +202,36 @@ public function validMultisite($uri)
return false;
}

public function getCacheDirectory()
public function getCachedServicesFile()
{
if ($this->cacheDirectory) {
return $this->cacheDirectory;
}

$configFactory = \Drupal::configFactory();
$siteId = $configFactory->get('system.site')
->get('uuid');
$pathTemporary = $configFactory->get('system.file')
->get('path.temporary');
$configuration = $this->configurationManager->getConfiguration();
$cacheDirectory = $configuration->get('application.cache.directory')?:'';
if ($cacheDirectory) {
if (strpos($cacheDirectory, '/') != 0) {
$cacheDirectory = $this->configurationManager
->getApplicationDirectory() . '/' . $cacheDirectory;
}
$cacheDirectories[] = $cacheDirectory . '/' . $siteId . '/';
}
$cacheDirectories[] = sprintf(
'%s/cache/%s/',
$this->configurationManager->getConsoleDirectory(),
$siteId
);
$cacheDirectories[] = $pathTemporary . '/console/cache/' . $siteId . '/';
if (!$this->cacheServicesFile) {
$configFactory = \Drupal::configFactory();
$siteId = $configFactory->get('system.site')->get('uuid');

foreach ($cacheDirectories as $cacheDirectory) {
if ($this->isValidDirectory($cacheDirectory)) {
$this->cacheDirectory = $cacheDirectory;
break;
}
$this->cacheServicesFile = \Drupal::service('console.root') .
DRUPAL_CONSOLE . $siteId . '.console.services.yml';
}

return $this->cacheDirectory;
}

private function isValidDirectory($path)
{
$fileSystem = new Filesystem();
if ($fileSystem->exists($path)) {
return true;
}
try {
$fileSystem->mkdir($path);

return true;
} catch (\Exception $e) {
return false;
}
}

public function cachedServicesFile()
{
return $this->getCacheDirectory().'/console.services.yml';
return $this->cacheServicesFile;
}

public function cachedServicesFileExists()
{
return file_exists($this->cachedServicesFile());
return file_exists($this->getCachedServicesFile());
}

public function removeCachedServicesFile()
{
if ($this->cachedServicesFileExists()) {
unlink($this->cachedServicesFile());
unlink($this->getCachedServicesFile());
}
}

/**
* @param string $root
*/
public function setRoot($root)
{
$this->root = $root;
}
}