-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
336 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ICanBoogie package. | ||
* | ||
* (c) Olivier Laviale <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ICanBoogie\CLDR; | ||
|
||
/** | ||
* Provides CLDR data from the filesystem, and falls back to a specified provider when the data | ||
* is not available. | ||
*/ | ||
class FileProvider implements ProviderInterface, CacheInterface | ||
{ | ||
/** | ||
* Create a store key from a CLDR path. | ||
* | ||
* @param string $path A CLDR path. | ||
* | ||
* @return string A store key. | ||
*/ | ||
static private function path_to_key($path) | ||
{ | ||
return str_replace('/', '--', $path); | ||
} | ||
|
||
/** | ||
* The directory where files are stored. | ||
* | ||
* @var string | ||
*/ | ||
protected $root; | ||
|
||
/** | ||
* @var ProviderInterface | ||
*/ | ||
protected $provider; | ||
|
||
/** | ||
* @param ProviderInterface $provider Fallback provider. | ||
* @param string $directory Path to the directory where cached files are stored. | ||
*/ | ||
public function __construct(ProviderInterface $provider, $directory) | ||
{ | ||
$this->root = rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; | ||
$this->provider = $provider; | ||
} | ||
|
||
public function exists($path) | ||
{ | ||
$key = self::path_to_key($path); | ||
$filename = $this->root . $key; | ||
|
||
return file_exists($filename); | ||
} | ||
|
||
public function retrieve($path) | ||
{ | ||
$key = self::path_to_key($path); | ||
$filename = $this->root . $key; | ||
|
||
if (!file_exists($filename)) | ||
{ | ||
return; | ||
} | ||
|
||
return json_decode(file_get_contents($filename), true); | ||
} | ||
|
||
public function store($path, $data) | ||
{ | ||
$key = self::path_to_key($path); | ||
$filename = $this->root . $key; | ||
|
||
file_put_contents($filename, json_encode($data)); | ||
} | ||
|
||
/** | ||
* The section path, following the pattern "<identity>/<section>". | ||
* | ||
* @param string $path | ||
* | ||
* @throws ResourceNotFound when the specified path does not exists on the CLDR source. | ||
* | ||
* @return string | ||
*/ | ||
public function provide($path) | ||
{ | ||
if ($this->exists($path)) | ||
{ | ||
return $this->retrieve($path); | ||
} | ||
|
||
$data = $this->provider->provide($path); | ||
$this->store($path, $data); | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ICanBoogie package. | ||
* | ||
* (c) Olivier Laviale <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ICanBoogie\CLDR; | ||
|
||
/** | ||
* An interface for classes that can provide CLDR data. | ||
* | ||
* @package ICanBoogie\CLDR | ||
*/ | ||
interface ProviderInterface | ||
{ | ||
/** | ||
* The section path, following the pattern "<identity>/<section>". | ||
* | ||
* @param string $path | ||
* | ||
* @throws ResourceNotFound when the specified path does not exists on the CLDR source. | ||
* | ||
* @return string | ||
*/ | ||
public function provide($path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.