Skip to content

Commit

Permalink
normalize cache file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Sep 6, 2019
1 parent f387115 commit 7227083
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ public function shouldSkipMiddleware()
*/
public function getCachedServicesPath()
{
return Env::get('APP_SERVICES_CACHE', $this->bootstrapPath().'/cache/services.php');
return $this->normalizeCachePath('cache/services.php', 'APP_SERVICES_CACHE');
}

/**
Expand All @@ -904,7 +904,7 @@ public function getCachedServicesPath()
*/
public function getCachedPackagesPath()
{
return Env::get('APP_PACKAGES_CACHE', $this->bootstrapPath().'/cache/packages.php');
return $this->normalizeCachePath('cache/packages.php', 'APP_PACKAGES_CACHE');
}

/**
Expand All @@ -924,7 +924,7 @@ public function configurationIsCached()
*/
public function getCachedConfigPath()
{
return Env::get('APP_CONFIG_CACHE', $this->bootstrapPath().'/cache/config.php');
return $this->normalizeCachePath('cache/config.php', 'APP_CONFIG_CACHE');
}

/**
Expand All @@ -944,7 +944,7 @@ public function routesAreCached()
*/
public function getCachedRoutesPath()
{
return Env::get('APP_ROUTES_CACHE', $this->bootstrapPath().'/cache/routes.php');
return $this->normalizeCachePath('cache/routes.php', 'APP_ROUTES_CACHE');
}

/**
Expand All @@ -964,7 +964,29 @@ public function eventsAreCached()
*/
public function getCachedEventsPath()
{
return Env::get('APP_EVENTS_CACHE', $this->bootstrapPath().'/cache/events.php');
return $this->normalizeCachePath('cache/events.php', 'APP_EVENTS_CACHE');
}

/**
* Normalize a relative or absolute path to a cache file.
*
* @param string $path
* @param string $key
* @return string
*/
protected function normalizeCachePath($path, $key)
{
$env = Env::get($key);

if ($env === null) {
return $this->bootstrapPath($path);
}

if (Str::startsWith($env, '/')) {
return $env;
}

return $this->basePath($env);
}

/**
Expand Down
83 changes: 83 additions & 0 deletions tests/Foundation/FoundationApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,89 @@ public function testGetNamespace()
$this->assertSame('Laravel\\One\\', $app1->getNamespace());
$this->assertSame('Laravel\\Two\\', $app2->getNamespace());
}

public function testCachePathsResolveToBootstrapCacheDirectory()
{
$app = new Application('/base/path');

$this->assertSame('/base/path/bootstrap/cache/services.php', $app->getCachedServicesPath());
$this->assertSame('/base/path/bootstrap/cache/packages.php', $app->getCachedPackagesPath());
$this->assertSame('/base/path/bootstrap/cache/config.php', $app->getCachedConfigPath());
$this->assertSame('/base/path/bootstrap/cache/routes.php', $app->getCachedRoutesPath());
$this->assertSame('/base/path/bootstrap/cache/events.php', $app->getCachedEventsPath());
}

public function testEnvPathsAreUsedForCachePathsWhenSpecified()
{
$app = new Application('/base/path');
$_SERVER['APP_SERVICES_CACHE'] = '/absolute/path/services.php';
$_SERVER['APP_PACKAGES_CACHE'] = '/absolute/path/packages.php';
$_SERVER['APP_CONFIG_CACHE'] = '/absolute/path/config.php';
$_SERVER['APP_ROUTES_CACHE'] = '/absolute/path/routes.php';
$_SERVER['APP_EVENTS_CACHE'] = '/absolute/path/events.php';

$this->assertSame('/absolute/path/services.php', $app->getCachedServicesPath());
$this->assertSame('/absolute/path/packages.php', $app->getCachedPackagesPath());
$this->assertSame('/absolute/path/config.php', $app->getCachedConfigPath());
$this->assertSame('/absolute/path/routes.php', $app->getCachedRoutesPath());
$this->assertSame('/absolute/path/events.php', $app->getCachedEventsPath());

unset(
$_SERVER['APP_SERVICES_CACHE'],
$_SERVER['APP_PACKAGES_CACHE'],
$_SERVER['APP_CONFIG_CACHE'],
$_SERVER['APP_ROUTES_CACHE'],
$_SERVER['APP_EVENTS_CACHE']
);
}

public function testEnvPathsAreUsedAndMadeAbsoluteForCachePathsWhenSpecifiedAsRelative()
{
$app = new Application('/base/path');
$_SERVER['APP_SERVICES_CACHE'] = 'relative/path/services.php';
$_SERVER['APP_PACKAGES_CACHE'] = 'relative/path/packages.php';
$_SERVER['APP_CONFIG_CACHE'] = 'relative/path/config.php';
$_SERVER['APP_ROUTES_CACHE'] = 'relative/path/routes.php';
$_SERVER['APP_EVENTS_CACHE'] = 'relative/path/events.php';

$this->assertSame('/base/path/relative/path/services.php', $app->getCachedServicesPath());
$this->assertSame('/base/path/relative/path/packages.php', $app->getCachedPackagesPath());
$this->assertSame('/base/path/relative/path/config.php', $app->getCachedConfigPath());
$this->assertSame('/base/path/relative/path/routes.php', $app->getCachedRoutesPath());
$this->assertSame('/base/path/relative/path/events.php', $app->getCachedEventsPath());

unset(
$_SERVER['APP_SERVICES_CACHE'],
$_SERVER['APP_PACKAGES_CACHE'],
$_SERVER['APP_CONFIG_CACHE'],
$_SERVER['APP_ROUTES_CACHE'],
$_SERVER['APP_EVENTS_CACHE']
);
}

public function testEnvPathsAreUsedAndMadeAbsoluteForCachePathsWhenSpecifiedAsRelativeWithNullBasePath()
{
$app = new Application();
$_SERVER['APP_SERVICES_CACHE'] = 'relative/path/services.php';
$_SERVER['APP_PACKAGES_CACHE'] = 'relative/path/packages.php';
$_SERVER['APP_CONFIG_CACHE'] = 'relative/path/config.php';
$_SERVER['APP_ROUTES_CACHE'] = 'relative/path/routes.php';
$_SERVER['APP_EVENTS_CACHE'] = 'relative/path/events.php';

$this->assertSame('/relative/path/services.php', $app->getCachedServicesPath());
$this->assertSame('/relative/path/packages.php', $app->getCachedPackagesPath());
$this->assertSame('/relative/path/config.php', $app->getCachedConfigPath());
$this->assertSame('/relative/path/routes.php', $app->getCachedRoutesPath());
$this->assertSame('/relative/path/events.php', $app->getCachedEventsPath());

unset(
$_SERVER['APP_SERVICES_CACHE'],
$_SERVER['APP_PACKAGES_CACHE'],
$_SERVER['APP_CONFIG_CACHE'],
$_SERVER['APP_ROUTES_CACHE'],
$_SERVER['APP_EVENTS_CACHE']
);
}
}

class ApplicationBasicServiceProviderStub extends ServiceProvider
Expand Down

0 comments on commit 7227083

Please sign in to comment.