Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Jan 31, 2014
1 parent 2b256b5 commit c691236
Show file tree
Hide file tree
Showing 16 changed files with 738 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/vendor/
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Bernhard Schussek

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 40 additions & 0 deletions README.md
@@ -0,0 +1,40 @@
Puli - A PHP Resource Manager
=============================

Puli manages the file resources of you PHP project and provides access to these
resources through a unified naming system. Puli manages files in *repositories*,
where you map them to a path:

```php
use Webmozart\Puli\Configuration\RepositoryConfiguration;

$config = new RepositoryConfiguration('/path/to/project');
$config->setPath('/webmozart/puli', 'resources/assets/*');
$config->setPath('/webmozart/puli/trans', 'resources/trans');
```

Currently, Puli only provides a repository implementation that caches the
repository paths in PHP files. Pass the path where these files are stored when
you call the `dump()` method of the `PhpRepositoryDumper`:

```php
use Webmozart\Puli\Dumper\PhpRepositoryDumper;

$dumper = new PhpRepositoryDumper();
$dumper->dump($config, '/path/to/cache/resources');
```

Then create a `PhpDumpRepository` at this location, which lets you locate the
paths of the files in your repository:

```php
use Webmozart\Puli\Repository\PhpDumpRepository;

$repo = new PhpDumpRepository('/path/to/cache/resources');

echo $repo->getResource('/webmozart/puli/css/style.css')->getPath();
// => /path/to/project/resources/assets/css/style.css

echo $repo->getResource('/webmozart/puli/trans/en.xlf')->getPath();
// => /path/to/project/resources/trans/en.xlf
```
18 changes: 18 additions & 0 deletions composer.json
@@ -0,0 +1,18 @@
{
"name": "webmozart/puli",
"description": "Puli manages the file resources of your project.",
"license": "MIT",
"authors": [
{
"name": "Bernhard Schussek",
"email": "bschussek@gmail.com"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Webmozart\\Puli\\": "src/"
}
}
}
git status
21 changes: 21 additions & 0 deletions src/Configuration/PathNotExportedException.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Puli package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Webmozart\Puli\Configuration;

/**
* @since %%NextVersion%%
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PathNotExportedException extends \Exception
{

}
161 changes: 161 additions & 0 deletions src/Configuration/RepositoryConfiguration.php
@@ -0,0 +1,161 @@
<?php

/*
* This file is part of the Puli package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Webmozart\Puli\Configuration;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class RepositoryConfiguration
{
private $files = array();

private $directories = array();

private $tags = array();

private $knownPaths = array();

private $rootDirectory;

public function __construct($rootDirectory = null)
{
if (!is_dir($rootDirectory)) {
throw new \InvalidArgumentException(sprintf(
'The directory "%s" does not exist.',
$rootDirectory
));
}

$this->rootDirectory = $rootDirectory ? rtrim($rootDirectory, '/').'/' : '';
}

public function getRootDirectory()
{
return rtrim($this->rootDirectory, '/');
}

public function setPath($repositoryPath, $pattern)
{
$paths = glob($this->rootDirectory.$pattern);

if (0 === count($paths)) {
throw new UnmatchedPatternException(sprintf(
'The pattern "%s" did not match any file.',
$pattern
));
}

$rootLength = strlen($this->rootDirectory);

// If exactly one directory is matched, let the repository path point
// to that directory
if (1 === count($paths) && is_dir($paths[0])) {
$this->addDirectory(rtrim(substr($paths[0], $rootLength), '/'), $repositoryPath);

return;
}

// If multiple paths are matched, create sub-paths for each entry
foreach ($paths as $path) {
$nestedRepositoryPath = $repositoryPath.'/'.basename($path);

if (is_dir($path)) {
$this->addDirectory(rtrim(substr($path, $rootLength), '/'), $nestedRepositoryPath);
} else {
$this->addFile(substr($path, $rootLength), $nestedRepositoryPath);
}
}
}

public function getFiles()
{
return $this->files;
}

public function getDirectories()
{
return $this->directories;
}

public function addTag($tag, $pattern)
{
$paths = glob($this->rootDirectory.$pattern);

if (0 === count($paths)) {
throw new UnmatchedPatternException(sprintf(
'The pattern "%s" did not match any file.',
$pattern
));
}

if (!isset($this->tags[$tag])) {
$this->tags[$tag] = array();
}

$rootLength = strlen($this->rootDirectory);

foreach ($paths as $path) {
$path = rtrim(substr($path, $rootLength), '/');

// Check whether the path was exported directly
$isExported = isset($this->knownPaths[$path]);

// Else check whether one of its parent directories was exported
if (!$isExported) {
foreach ($this->directories as $dirPaths) {
foreach ($dirPaths as $dirPath) {
if (0 === strpos($path, $dirPath.'/')) {
$isExported = true;

break 2;
}
}
}
}

// Else report an error
if (!$isExported) {
throw new PathNotExportedException(sprintf(
'The path "%s" was not exported.',
$path
));
}

$this->tags[$tag][] = $path;
}
}

public function getTags()
{
return $this->tags;
}

private function addFile($path, $repositoryPath)
{
if (!isset($this->files[$repositoryPath])) {
$this->files[$repositoryPath][] = array();
}

$this->files[$repositoryPath][] = $path;
$this->knownPaths[$path] = true;
}

private function addDirectory($path, $repositoryPath)
{
if (!isset($this->directories[$repositoryPath])) {
$this->directories[$repositoryPath] = array();
}

$this->directories[$repositoryPath][] = $path;
$this->knownPaths[$path] = true;
}
}
20 changes: 20 additions & 0 deletions src/Configuration/UnmatchedPatternException.php
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Puli package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Webmozart\Puli\Configuration;

/**
* @since %%NextVersion%%
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UnmatchedPatternException extends \Exception
{
}
106 changes: 106 additions & 0 deletions src/Dumper/PhpRepositoryDumper.php
@@ -0,0 +1,106 @@
<?php

/*
* This file is part of the Puli package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Webmozart\Puli\Dumper;

use Webmozart\Puli\Configuration\RepositoryConfiguration;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class PhpRepositoryDumper implements RepositoryDumperInterface
{
const PATHS_FILE = '/resources_paths.php';

const TAGS_FILE = '/resources_tags.php';

const CONFIG_FILE = '/resources_config.php';

public function dump(RepositoryConfiguration $config, $targetPath)
{
$paths = array();
$root = $config->getRootDirectory();
$rootLength = strlen($root);

foreach ($config->getDirectories() as $dirRepositoryPath => $dirPaths) {
foreach ($dirPaths as $dirPath) {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
$dirPath,
\FilesystemIterator::CURRENT_AS_PATHNAME |
\FilesystemIterator::SKIP_DOTS |
\FilesystemIterator::UNIX_PATHS
),
\RecursiveIteratorIterator::SELF_FIRST
);

$dirPathLength = strlen($dirPath);

if (0 === strpos($dirPath, $root)) {
$dirPath = substr($dirPath, $rootLength);
}

if (!isset($paths[$dirRepositoryPath])) {
$paths[$dirRepositoryPath] = array();
}

$paths[$dirRepositoryPath][] = $dirPath;

foreach ($iterator as $path) {
$repositoryPath = $dirRepositoryPath.substr($path, $dirPathLength);

if (!isset($paths[$repositoryPath])) {
$paths[$repositoryPath] = array();
}

if (0 === strpos($path, $root)) {
$path = substr($path, $rootLength);
}

$paths[$repositoryPath][] = $path;
}
}
}

foreach ($config->getFiles() as $dirRepositoryPath => $filePaths) {
foreach ($filePaths as $filePath) {
if (!isset($paths[$dirRepositoryPath])) {
$paths[$dirRepositoryPath] = array();
}

if (0 === strpos($filePath, $root)) {
$filePath = substr($filePath, $rootLength);
}

$paths[$dirRepositoryPath][] = $filePath;
}
}

if (!file_exists($targetPath)) {
mkdir($targetPath, 0777, true);
}

if (!is_dir($targetPath)) {
throw new \InvalidArgumentException(sprintf(
'The path "%s" is not a directory.',
$targetPath
));
}

$dumpedConfig = array(
'root' => $config->getRootDirectory(),
);

file_put_contents($targetPath.self::PATHS_FILE, "<?php\n\nreturn ".var_export($paths, true).";");
file_put_contents($targetPath.self::TAGS_FILE, "<?php\n\nreturn ".var_export($config->getTags(), true).";");
file_put_contents($targetPath.self::CONFIG_FILE, "<?php\n\nreturn ".var_export($dumpedConfig, true).";");
}
}

0 comments on commit c691236

Please sign in to comment.