-
-
Notifications
You must be signed in to change notification settings - Fork 74
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
12 changed files
with
226 additions
and
108 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 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 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 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,10 @@ | ||
<?php | ||
|
||
namespace Studio\Config; | ||
|
||
interface Serializer | ||
{ | ||
public function deserializePaths($obj); | ||
|
||
public function serializePaths(array $paths); | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace Studio\Config; | ||
|
||
use Studio\Package; | ||
|
||
class Version1Serializer implements Serializer | ||
{ | ||
public function deserializePaths($obj) | ||
{ | ||
return array_values($obj['packages']); | ||
} | ||
|
||
public function serializePaths(array $paths) | ||
{ | ||
$globbedPaths = array_map(function ($path) { | ||
return glob($path, GLOB_MARK | GLOB_ONLYDIR); | ||
}, $paths); | ||
|
||
$allPaths = array_reduce($globbedPaths, function ($collect, $pathOrPaths) { | ||
if (is_array($pathOrPaths)) { | ||
return array_merge($collect, $pathOrPaths); | ||
} else { | ||
$collect[] = $pathOrPaths; | ||
return $collect; | ||
} | ||
}, []); | ||
|
||
$allPaths = array_filter($allPaths, function ($path) { | ||
return is_dir($path) && file_exists("$path/composer.json"); | ||
}); | ||
|
||
$packages = array_map(function ($path) { | ||
return Package::fromFolder(rtrim($path, '/')); | ||
}, $allPaths); | ||
|
||
$packagePaths = array_reduce($packages, function ($collect, Package $package) { | ||
$collect[$package->getComposerId()] = $package->getPath(); | ||
return $collect; | ||
}, []); | ||
|
||
return ['packages' => $packagePaths]; | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Studio\Config; | ||
|
||
class Version2Serializer implements Serializer | ||
{ | ||
public function deserializePaths($obj) | ||
{ | ||
return $obj['paths']; | ||
} | ||
|
||
public function serializePaths(array $paths) | ||
{ | ||
return ['paths' => array_values($paths)]; | ||
} | ||
} |
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,74 @@ | ||
<?php | ||
|
||
namespace Studio\Config; | ||
|
||
/** | ||
* A decorator for serializing from/to multiple versions | ||
* | ||
* We support several versions of the Studio config file. | ||
* | ||
* This serializer class uses the serializer according to | ||
* the "version" field or the default one if no "version" | ||
* is provided for reading. | ||
* | ||
* For writing, the serializer with the highest version | ||
* number is used. | ||
* | ||
* @package Studio\Config | ||
*/ | ||
class VersionedSerializer implements Serializer | ||
{ | ||
/** | ||
* @var Serializer[] | ||
*/ | ||
protected $serializers; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $defaultVersion; | ||
|
||
/** | ||
* @param int $version | ||
* @param Serializer $serializer | ||
* @return static | ||
*/ | ||
public static function withDefault($version, Serializer $serializer) | ||
{ | ||
return new static([$version => $serializer], $version); | ||
} | ||
|
||
public function __construct(array $serializers, $defaultVersion) | ||
{ | ||
$this->serializers = $serializers; | ||
$this->defaultVersion = $defaultVersion; | ||
} | ||
|
||
public function version($version, Serializer $serializer) | ||
{ | ||
$this->serializers[$version] = $serializer; | ||
|
||
return $this; | ||
} | ||
|
||
public function deserializePaths($obj) | ||
{ | ||
if (!isset($obj['version'])) { | ||
$serializer = $this->serializers[$this->defaultVersion]; | ||
} else if (array_key_exists(intval($obj['version']), $this->serializers)) { | ||
$serializer = $this->serializers[$obj['version']]; | ||
} else { | ||
throw new \InvalidArgumentException('Invalid version'); | ||
} | ||
|
||
return $serializer->deserializePaths($obj); | ||
} | ||
|
||
public function serializePaths(array $paths) | ||
{ | ||
$lastVersion = max(array_keys($this->serializers)); | ||
$serializer = $this->serializers[$lastVersion]; | ||
|
||
return ['version' => $lastVersion] + $serializer->serializePaths($paths); | ||
} | ||
} |
Oops, something went wrong.