Added Laravel project

This commit is contained in:
2017-09-17 00:35:10 +02:00
parent a3c19304d5
commit ecf605b8f5
6246 changed files with 682270 additions and 2 deletions

View File

@@ -0,0 +1,193 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\Version;
use PharIo\Version\Exception as VersionException;
use PharIo\Version\VersionConstraintParser;
class ManifestDocumentMapper {
/**
* @param ManifestDocument $document
*
* @returns Manifest
*
* @throws ManifestDocumentMapperException
*/
public function map(ManifestDocument $document) {
try {
$contains = $document->getContainsElement();
$type = $this->mapType($contains);
$copyright = $this->mapCopyright($document->getCopyrightElement());
$requirements = $this->mapRequirements($document->getRequiresElement());
$bundledComponents = $this->mapBundledComponents($document);
return new Manifest(
new ApplicationName($contains->getName()),
new Version($contains->getVersion()),
$type,
$copyright,
$requirements,
$bundledComponents
);
} catch (VersionException $e) {
throw new ManifestDocumentMapperException($e->getMessage(), $e->getCode(), $e);
} catch (Exception $e) {
throw new ManifestDocumentMapperException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* @param ContainsElement $contains
*
* @return Type
*
* @throws ManifestDocumentMapperException
*/
private function mapType(ContainsElement $contains) {
switch ($contains->getType()) {
case 'application':
return Type::application();
case 'library':
return Type::library();
case 'extension':
return $this->mapExtension($contains->getExtensionElement());
}
throw new ManifestDocumentMapperException(
sprintf('Unsupported type %s', $contains->getType())
);
}
/**
* @param CopyrightElement $copyright
*
* @return CopyrightInformation
*
* @throws InvalidUrlException
* @throws InvalidEmailException
*/
private function mapCopyright(CopyrightElement $copyright) {
$authors = new AuthorCollection();
foreach($copyright->getAuthorElements() as $authorElement) {
$authors->add(
new Author(
$authorElement->getName(),
new Email($authorElement->getEmail())
)
);
}
$licenseElement = $copyright->getLicenseElement();
$license = new License(
$licenseElement->getType(),
new Url($licenseElement->getUrl())
);
return new CopyrightInformation(
$authors,
$license
);
}
/**
* @param RequiresElement $requires
*
* @return RequirementCollection
*
* @throws ManifestDocumentMapperException
*/
private function mapRequirements(RequiresElement $requires) {
$collection = new RequirementCollection();
$phpElement = $requires->getPHPElement();
$parser = new VersionConstraintParser;
try {
$versionConstraint = $parser->parse($phpElement->getVersion());
} catch (VersionException $e) {
throw new ManifestDocumentMapperException(
sprintf('Unsupported version constraint - %s', $e->getMessage()),
$e->getCode(),
$e
);
}
$collection->add(
new PhpVersionRequirement(
$versionConstraint
)
);
if (!$phpElement->hasExtElements()) {
return $collection;
}
foreach($phpElement->getExtElements() as $extElement) {
$collection->add(
new PhpExtensionRequirement($extElement->getName())
);
}
return $collection;
}
/**
* @param ManifestDocument $document
*
* @return BundledComponentCollection
*/
private function mapBundledComponents(ManifestDocument $document) {
$collection = new BundledComponentCollection();
if (!$document->hasBundlesElement()) {
return $collection;
}
foreach($document->getBundlesElement()->getComponentElements() as $componentElement) {
$collection->add(
new BundledComponent(
$componentElement->getName(),
new Version(
$componentElement->getVersion()
)
)
);
}
return $collection;
}
/**
* @param ExtensionElement $extension
*
* @return Extension
*
* @throws ManifestDocumentMapperException
*/
private function mapExtension(ExtensionElement $extension) {
try {
$parser = new VersionConstraintParser;
$versionConstraint = $parser->parse($extension->getCompatible());
return Type::extension(
new ApplicationName($extension->getFor()),
$versionConstraint
);
} catch (VersionException $e) {
throw new ManifestDocumentMapperException(
sprintf('Unsupported version constraint - %s', $e->getMessage()),
$e->getCode(),
$e
);
}
}
}

View File

@@ -0,0 +1,66 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ManifestLoader {
/**
* @param string $filename
*
* @return Manifest
*
* @throws ManifestLoaderException
*/
public static function fromFile($filename) {
try {
return (new ManifestDocumentMapper())->map(
ManifestDocument::fromFile($filename)
);
} catch (Exception $e) {
throw new ManifestLoaderException(
sprintf('Loading %s failed.', $filename),
$e->getCode(),
$e
);
}
}
/**
* @param string $filename
*
* @return Manifest
*
* @throws ManifestLoaderException
*/
public static function fromPhar($filename) {
return self::fromFile('phar://' . $filename . '/manifest.xml');
}
/**
* @param string $manifest
*
* @return Manifest
*
* @throws ManifestLoaderException
*/
public static function fromString($manifest) {
try {
return (new ManifestDocumentMapper())->map(
ManifestDocument::fromString($manifest)
);
} catch (Exception $e) {
throw new ManifestLoaderException(
'Processing string failed',
$e->getCode(),
$e
);
}
}
}

View File

@@ -0,0 +1,163 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\AnyVersionConstraint;
use PharIo\Version\Version;
use PharIo\Version\VersionConstraint;
use XMLWriter;
class ManifestSerializer {
/**
* @var XMLWriter
*/
private $xmlWriter;
public function serializeToFile(Manifest $manifest, $filename) {
file_put_contents(
$filename,
$this->serializeToString($manifest)
);
}
public function serializeToString(Manifest $manifest) {
$this->startDocument();
$this->addContains($manifest->getName(), $manifest->getVersion(), $manifest->getType());
$this->addCopyright($manifest->getCopyrightInformation());
$this->addRequirements($manifest->getRequirements());
$this->addBundles($manifest->getBundledComponents());
return $this->finishDocument();
}
private function startDocument() {
$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->setIndent(true);
$xmlWriter->setIndentString(str_repeat(' ', 4));
$xmlWriter->startDocument('1.0', 'UTF-8');
$xmlWriter->startElement('phar');
$xmlWriter->writeAttribute('xmlns', 'https://phar.io/xml/manifest/1.0');
$this->xmlWriter = $xmlWriter;
}
private function finishDocument() {
$this->xmlWriter->endElement();
$this->xmlWriter->endDocument();
return $this->xmlWriter->outputMemory();
}
private function addContains($name, Version $version, Type $type) {
$this->xmlWriter->startElement('contains');
$this->xmlWriter->writeAttribute('name', $name);
$this->xmlWriter->writeAttribute('version', $version->getVersionString());
switch (true) {
case $type->isApplication(): {
$this->xmlWriter->writeAttribute('type', 'application');
break;
}
case $type->isLibrary(): {
$this->xmlWriter->writeAttribute('type', 'library');
break;
}
case $type->isExtension(): {
/* @var $type Extension */
$this->xmlWriter->writeAttribute('type', 'extension');
$this->addExtension($type->getApplicationName(), $type->getVersionConstraint());
break;
}
default: {
$this->xmlWriter->writeAttribute('type', 'custom');
}
}
$this->xmlWriter->endElement();
}
private function addCopyright(CopyrightInformation $copyrightInformation) {
$this->xmlWriter->startElement('copyright');
foreach($copyrightInformation->getAuthors() as $author) {
$this->xmlWriter->startElement('author');
$this->xmlWriter->writeAttribute('name', $author->getName());
$this->xmlWriter->writeAttribute('email', (string) $author->getEmail());
$this->xmlWriter->endElement();
}
$license = $copyrightInformation->getLicense();
$this->xmlWriter->startElement('license');
$this->xmlWriter->writeAttribute('type', $license->getName());
$this->xmlWriter->writeAttribute('url', $license->getUrl());
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
}
private function addRequirements(RequirementCollection $requirementCollection) {
$phpRequirement = new AnyVersionConstraint();
$extensions = [];
foreach($requirementCollection as $requirement) {
if ($requirement instanceof PhpVersionRequirement) {
$phpRequirement = $requirement->getVersionConstraint();
continue;
}
if ($requirement instanceof PhpExtensionRequirement) {
$extensions[] = (string) $requirement;
}
}
$this->xmlWriter->startElement('requires');
$this->xmlWriter->startElement('php');
$this->xmlWriter->writeAttribute('version', $phpRequirement->asString());
foreach($extensions as $extension) {
$this->xmlWriter->startElement('ext');
$this->xmlWriter->writeAttribute('name', $extension);
$this->xmlWriter->endElement();
}
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
}
private function addBundles(BundledComponentCollection $bundledComponentCollection) {
if (count($bundledComponentCollection) === 0) {
return;
}
$this->xmlWriter->startElement('bundles');
foreach($bundledComponentCollection as $bundledComponent) {
$this->xmlWriter->startElement('component');
$this->xmlWriter->writeAttribute('name', $bundledComponent->getName());
$this->xmlWriter->writeAttribute('version', $bundledComponent->getVersion()->getVersionString());
$this->xmlWriter->endElement();
}
$this->xmlWriter->endElement();
}
private function addExtension($application, VersionConstraint $versionConstraint) {
$this->xmlWriter->startElement('extension');
$this->xmlWriter->writeAttribute('for', $application);
$this->xmlWriter->writeAttribute('compatible', $versionConstraint->asString());
$this->xmlWriter->endElement();
}
}

View File

@@ -0,0 +1,14 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
interface Exception {
}

View File

@@ -0,0 +1,16 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class InvalidApplicationNameException extends \InvalidArgumentException implements Exception {
const NotAString = 1;
const InvalidFormat = 2;
}

View File

@@ -0,0 +1,14 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class InvalidEmailException extends \InvalidArgumentException implements Exception {
}

View File

@@ -0,0 +1,14 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class InvalidUrlException extends \InvalidArgumentException implements Exception {
}

View File

@@ -0,0 +1,6 @@
<?php
namespace PharIo\Manifest;
class ManifestDocumentException extends \RuntimeException implements Exception {
}

View File

@@ -0,0 +1,6 @@
<?php
namespace PharIo\Manifest;
class ManifestDocumentMapperException extends \RuntimeException implements Exception {
}

View File

@@ -0,0 +1,6 @@
<?php
namespace PharIo\Manifest;
class ManifestElementException extends \RuntimeException implements Exception {
}

View File

@@ -0,0 +1,6 @@
<?php
namespace PharIo\Manifest;
class ManifestLoaderException extends \Exception implements Exception {
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class Application extends Type {
/**
* @return bool
*/
public function isApplication() {
return true;
}
}

View File

@@ -0,0 +1,65 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ApplicationName {
/**
* @var string
*/
private $name;
/**
* ApplicationName constructor.
*
* @param string $name
*
* @throws InvalidApplicationNameException
*/
public function __construct($name) {
$this->ensureIsString($name);
$this->ensureValidFormat($name);
$this->name = $name;
}
/**
* @return string
*/
public function __toString() {
return $this->name;
}
public function isEqual(ApplicationName $name) {
return $this->name === $name->name;
}
/**
* @param string $name
*
* @throws InvalidApplicationNameException
*/
private function ensureValidFormat($name) {
if (!preg_match('#\w/\w#', $name)) {
throw new InvalidApplicationNameException(
sprintf('Format of name "%s" is not valid - expected: vendor/packagename', $name),
InvalidApplicationNameException::InvalidFormat
);
}
}
private function ensureIsString($name) {
if (!is_string($name)) {
throw new InvalidApplicationNameException(
'Name must be a string',
InvalidApplicationNameException::NotAString
);
}
}
}

View File

@@ -0,0 +1,57 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class Author {
/**
* @var string
*/
private $name;
/**
* @var Email
*/
private $email;
/**
* @param string $name
* @param Email $email
*/
public function __construct($name, Email $email) {
$this->name = $name;
$this->email = $email;
}
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @return Email
*/
public function getEmail() {
return $this->email;
}
/**
* @return string
*/
public function __toString() {
return sprintf(
'%s <%s>',
$this->name,
$this->email
);
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class AuthorCollection implements \Countable, \IteratorAggregate {
/**
* @var Author[]
*/
private $authors = [];
public function add(Author $author) {
$this->authors[] = $author;
}
/**
* @return Author[]
*/
public function getAuthors() {
return $this->authors;
}
/**
* @return int
*/
public function count() {
return count($this->authors);
}
/**
* @return AuthorCollectionIterator
*/
public function getIterator() {
return new AuthorCollectionIterator($this);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class AuthorCollectionIterator implements \Iterator {
/**
* @var Author[]
*/
private $authors = [];
/**
* @var int
*/
private $position;
public function __construct(AuthorCollection $authors) {
$this->authors = $authors->getAuthors();
}
public function rewind() {
$this->position = 0;
}
/**
* @return bool
*/
public function valid() {
return $this->position < count($this->authors);
}
/**
* @return int
*/
public function key() {
return $this->position;
}
/**
* @return Author
*/
public function current() {
return $this->authors[$this->position];
}
public function next() {
$this->position++;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\Version;
class BundledComponent {
/**
* @var string
*/
private $name;
/**
* @var Version
*/
private $version;
/**
* @param string $name
* @param Version $version
*/
public function __construct($name, Version $version) {
$this->name = $name;
$this->version = $version;
}
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @return Version
*/
public function getVersion() {
return $this->version;
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class BundledComponentCollection implements \Countable, \IteratorAggregate {
/**
* @var BundledComponent[]
*/
private $bundledComponents = [];
public function add(BundledComponent $bundledComponent) {
$this->bundledComponents[] = $bundledComponent;
}
/**
* @return BundledComponent[]
*/
public function getBundledComponents() {
return $this->bundledComponents;
}
/**
* @return int
*/
public function count() {
return count($this->bundledComponents);
}
/**
* @return BundledComponentCollectionIterator
*/
public function getIterator() {
return new BundledComponentCollectionIterator($this);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class BundledComponentCollectionIterator implements \Iterator {
/**
* @var BundledComponent[]
*/
private $bundledComponents = [];
/**
* @var int
*/
private $position;
public function __construct(BundledComponentCollection $bundledComponents) {
$this->bundledComponents = $bundledComponents->getBundledComponents();
}
public function rewind() {
$this->position = 0;
}
/**
* @return bool
*/
public function valid() {
return $this->position < count($this->bundledComponents);
}
/**
* @return int
*/
public function key() {
return $this->position;
}
/**
* @return BundledComponent
*/
public function current() {
return $this->bundledComponents[$this->position];
}
public function next() {
$this->position++;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class CopyrightInformation {
/**
* @var AuthorCollection
*/
private $authors;
/**
* @var License
*/
private $license;
public function __construct(AuthorCollection $authors, License $license) {
$this->authors = $authors;
$this->license = $license;
}
/**
* @return AuthorCollection
*/
public function getAuthors() {
return $this->authors;
}
/**
* @return License
*/
public function getLicense() {
return $this->license;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class Email {
/**
* @var string
*/
private $email;
/**
* @param string $email
*
* @throws InvalidEmailException
*/
public function __construct($email) {
$this->ensureEmailIsValid($email);
$this->email = $email;
}
/**
* @return string
*/
public function __toString() {
return $this->email;
}
/**
* @param string $url
*
* @throws InvalidEmailException
*/
private function ensureEmailIsValid($url) {
if (filter_var($url, \FILTER_VALIDATE_EMAIL) === false) {
throw new InvalidEmailException;
}
}
}

View File

@@ -0,0 +1,75 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\Version;
use PharIo\Version\VersionConstraint;
class Extension extends Type {
/**
* @var ApplicationName
*/
private $application;
/**
* @var VersionConstraint
*/
private $versionConstraint;
/**
* @param ApplicationName $application
* @param VersionConstraint $versionConstraint
*/
public function __construct(ApplicationName $application, VersionConstraint $versionConstraint) {
$this->application = $application;
$this->versionConstraint = $versionConstraint;
}
/**
* @return ApplicationName
*/
public function getApplicationName() {
return $this->application;
}
/**
* @return VersionConstraint
*/
public function getVersionConstraint() {
return $this->versionConstraint;
}
/**
* @return bool
*/
public function isExtension() {
return true;
}
/**
* @param ApplicationName $name
*
* @return bool
*/
public function isExtensionFor(ApplicationName $name) {
return $this->application->isEqual($name);
}
/**
* @param ApplicationName $name
* @param Version $version
*
* @return bool
*/
public function isCompatibleWith(ApplicationName $name, Version $version) {
return $this->isExtensionFor($name) && $this->versionConstraint->complies($version);
}
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class Library extends Type {
/**
* @return bool
*/
public function isLibrary() {
return true;
}
}

View File

@@ -0,0 +1,42 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class License {
/**
* @var string
*/
private $name;
/**
* @var Url
*/
private $url;
public function __construct($name, Url $url) {
$this->name = $name;
$this->url = $url;
}
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @return Url
*/
public function getUrl() {
return $this->url;
}
}

View File

@@ -0,0 +1,138 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\Version;
class Manifest {
/**
* @var ApplicationName
*/
private $name;
/**
* @var Version
*/
private $version;
/**
* @var Type
*/
private $type;
/**
* @var CopyrightInformation
*/
private $copyrightInformation;
/**
* @var RequirementCollection
*/
private $requirements;
/**
* @var BundledComponentCollection
*/
private $bundledComponents;
public function __construct(ApplicationName $name, Version $version, Type $type, CopyrightInformation $copyrightInformation, RequirementCollection $requirements, BundledComponentCollection $bundledComponents) {
$this->name = $name;
$this->version = $version;
$this->type = $type;
$this->copyrightInformation = $copyrightInformation;
$this->requirements = $requirements;
$this->bundledComponents = $bundledComponents;
}
/**
* @return ApplicationName
*/
public function getName() {
return $this->name;
}
/**
* @return Version
*/
public function getVersion() {
return $this->version;
}
/**
* @return Type
*/
public function getType() {
return $this->type;
}
/**
* @return CopyrightInformation
*/
public function getCopyrightInformation() {
return $this->copyrightInformation;
}
/**
* @return RequirementCollection
*/
public function getRequirements() {
return $this->requirements;
}
/**
* @return BundledComponentCollection
*/
public function getBundledComponents() {
return $this->bundledComponents;
}
/**
* @return bool
*/
public function isApplication() {
return $this->type->isApplication();
}
/**
* @return bool
*/
public function isLibrary() {
return $this->type->isLibrary();
}
/**
* @return bool
*/
public function isExtension() {
return $this->type->isExtension();
}
/**
* @param ApplicationName $application
* @param Version|null $version
*
* @return bool
*/
public function isExtensionFor(ApplicationName $application, Version $version = null) {
if (!$this->isExtension()) {
return false;
}
/** @var Extension $type */
$type = $this->type;
if ($version !== null) {
return $type->isCompatibleWith($application, $version);
}
return $type->isExtensionFor($application);
}
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class PhpExtensionRequirement implements Requirement {
/**
* @var string
*/
private $extension;
/**
* @param string $extension
*/
public function __construct($extension) {
$this->extension = $extension;
}
/**
* @return string
*/
public function __toString() {
return $this->extension;
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\VersionConstraint;
class PhpVersionRequirement implements Requirement {
/**
* @var VersionConstraint
*/
private $versionConstraint;
public function __construct(VersionConstraint $versionConstraint) {
$this->versionConstraint = $versionConstraint;
}
/**
* @return VersionConstraint
*/
public function getVersionConstraint() {
return $this->versionConstraint;
}
}

View File

@@ -0,0 +1,14 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
interface Requirement {
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class RequirementCollection implements \Countable, \IteratorAggregate {
/**
* @var Requirement[]
*/
private $requirements = [];
public function add(Requirement $requirement) {
$this->requirements[] = $requirement;
}
/**
* @return Requirement[]
*/
public function getRequirements() {
return $this->requirements;
}
/**
* @return int
*/
public function count() {
return count($this->requirements);
}
/**
* @return RequirementCollectionIterator
*/
public function getIterator() {
return new RequirementCollectionIterator($this);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class RequirementCollectionIterator implements \Iterator {
/**
* @var Requirement[]
*/
private $requirements = [];
/**
* @var int
*/
private $position;
public function __construct(RequirementCollection $requirements) {
$this->requirements = $requirements->getRequirements();
}
public function rewind() {
$this->position = 0;
}
/**
* @return bool
*/
public function valid() {
return $this->position < count($this->requirements);
}
/**
* @return int
*/
public function key() {
return $this->position;
}
/**
* @return Requirement
*/
public function current() {
return $this->requirements[$this->position];
}
public function next() {
$this->position++;
}
}

View File

@@ -0,0 +1,60 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\VersionConstraint;
abstract class Type {
/**
* @return Application
*/
public static function application() {
return new Application;
}
/**
* @return Library
*/
public static function library() {
return new Library;
}
/**
* @param ApplicationName $application
* @param VersionConstraint $versionConstraint
*
* @return Extension
*/
public static function extension(ApplicationName $application, VersionConstraint $versionConstraint) {
return new Extension($application, $versionConstraint);
}
/**
* @return bool
*/
public function isApplication() {
return false;
}
/**
* @return bool
*/
public function isLibrary() {
return false;
}
/**
* @return bool
*/
public function isExtension() {
return false;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class Url {
/**
* @var string
*/
private $url;
/**
* @param string $url
*
* @throws InvalidUrlException
*/
public function __construct($url) {
$this->ensureUrlIsValid($url);
$this->url = $url;
}
/**
* @return string
*/
public function __toString() {
return $this->url;
}
/**
* @param string $url
*
* @throws InvalidUrlException
*/
private function ensureUrlIsValid($url) {
if (filter_var($url, \FILTER_VALIDATE_URL) === false) {
throw new InvalidUrlException;
}
}
}

View File

@@ -0,0 +1,21 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class AuthorElement extends ManifestElement {
public function getName() {
return $this->getAttributeValue('name');
}
public function getEmail() {
return $this->getAttributeValue('email');
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class AuthorElementCollection extends ElementCollection {
public function current() {
return new AuthorElement(
$this->getCurrentElement()
);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class BundlesElement extends ManifestElement {
public function getComponentElements() {
return new ComponentElementCollection(
$this->getChildrenByName('component')
);
}
}

View File

@@ -0,0 +1,21 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ComponentElement extends ManifestElement {
public function getName() {
return $this->getAttributeValue('name');
}
public function getVersion() {
return $this->getAttributeValue('version');
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ComponentElementCollection extends ElementCollection {
public function current() {
return new ComponentElement(
$this->getCurrentElement()
);
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ContainsElement extends ManifestElement {
public function getName() {
return $this->getAttributeValue('name');
}
public function getVersion() {
return $this->getAttributeValue('version');
}
public function getType() {
return $this->getAttributeValue('type');
}
public function getExtensionElement() {
return new ExtensionElement(
$this->getChildByName('extension')
);
}
}

View File

@@ -0,0 +1,25 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class CopyrightElement extends ManifestElement {
public function getAuthorElements() {
return new AuthorElementCollection(
$this->getChildrenByName('author')
);
}
public function getLicenseElement() {
return new LicenseElement(
$this->getChildByName('license')
);
}
}

View File

@@ -0,0 +1,58 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use DOMElement;
use DOMNodeList;
abstract class ElementCollection implements \Iterator {
/**
* @var DOMNodeList
*/
private $nodeList;
private $position;
/**
* ElementCollection constructor.
*
* @param DOMNodeList $nodeList
*/
public function __construct(DOMNodeList $nodeList) {
$this->nodeList = $nodeList;
$this->position = 0;
}
abstract public function current();
/**
* @return DOMElement
*/
protected function getCurrentElement() {
return $this->nodeList->item($this->position);
}
public function next() {
$this->position++;
}
public function key() {
return $this->position;
}
public function valid() {
return $this->position < $this->nodeList->length;
}
public function rewind() {
$this->position = 0;
}
}

View File

@@ -0,0 +1,17 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ExtElement extends ManifestElement {
public function getName() {
return $this->getAttributeValue('name');
}
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ExtElementCollection extends ElementCollection {
public function current() {
return new ExtElement(
$this->getCurrentElement()
);
}
}

View File

@@ -0,0 +1,21 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ExtensionElement extends ManifestElement {
public function getFor() {
return $this->getAttributeValue('for');
}
public function getCompatible() {
return $this->getAttributeValue('compatible');
}
}

View File

@@ -0,0 +1,21 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class LicenseElement extends ManifestElement {
public function getType() {
return $this->getAttributeValue('type');
}
public function getUrl() {
return $this->getAttributeValue('url');
}
}

View File

@@ -0,0 +1,118 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use DOMDocument;
use DOMElement;
class ManifestDocument {
const XMLNS = 'https://phar.io/xml/manifest/1.0';
/**
* @var DOMDocument
*/
private $dom;
/**
* ManifestDocument constructor.
*
* @param DOMDocument $dom
*/
private function __construct(DOMDocument $dom) {
$this->ensureCorrectDocumentType($dom);
$this->dom = $dom;
}
public static function fromFile($filename) {
if (!file_exists($filename)) {
throw new ManifestDocumentException(
sprintf('File "%s" not found', $filename)
);
}
return self::fromString(
file_get_contents($filename)
);
}
public static function fromString($xmlString) {
$prev = libxml_use_internal_errors(true);
libxml_clear_errors();
$dom = new DOMDocument();
$dom->loadXML($xmlString);
$errors = libxml_get_errors();
libxml_use_internal_errors($prev);
if (count($errors) !== 0) {
throw new ManifestDocumentLoadingException($errors);
}
return new self($dom);
}
public function getContainsElement() {
return new ContainsElement(
$this->fetchElementByName('contains')
);
}
public function getCopyrightElement() {
return new CopyrightElement(
$this->fetchElementByName('copyright')
);
}
public function getRequiresElement() {
return new RequiresElement(
$this->fetchElementByName('requires')
);
}
public function hasBundlesElement() {
return $this->dom->getElementsByTagNameNS(self::XMLNS, 'bundles')->length === 1;
}
public function getBundlesElement() {
return new BundlesElement(
$this->fetchElementByName('bundles')
);
}
private function ensureCorrectDocumentType(DOMDocument $dom) {
$root = $dom->documentElement;
if ($root->localName !== 'phar' || $root->namespaceURI !== self::XMLNS) {
throw new ManifestDocumentException('Not a phar.io manifest document');
}
}
/**
* @param $elementName
*
* @return DOMElement
*
* @throws ManifestDocumentException
*/
private function fetchElementByName($elementName) {
$element = $this->dom->getElementsByTagNameNS(self::XMLNS, $elementName)->item(0);
if (!$element instanceof DOMElement) {
throw new ManifestDocumentException(
sprintf('Element %s missing', $elementName)
);
}
return $element;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use LibXMLError;
class ManifestDocumentLoadingException extends \Exception implements Exception {
/**
* @var LibXMLError[]
*/
private $libxmlErrors;
/**
* ManifestDocumentLoadingException constructor.
*
* @param LibXMLError[] $libxmlErrors
*/
public function __construct(array $libxmlErrors) {
$this->libxmlErrors = $libxmlErrors;
$first = $this->libxmlErrors[0];
parent::__construct(
sprintf(
'%s (Line: %d / Column: %d / File: %s)',
$first->message,
$first->line,
$first->column,
$first->file
),
$first->code
);
}
/**
* @return LibXMLError[]
*/
public function getLibxmlErrors() {
return $this->libxmlErrors;
}
}

View File

@@ -0,0 +1,100 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use DOMElement;
use DOMNodeList;
class ManifestElement {
const XMLNS = 'https://phar.io/xml/manifest/1.0';
/**
* @var DOMElement
*/
private $element;
/**
* ContainsElement constructor.
*
* @param DOMElement $element
*/
public function __construct(DOMElement $element) {
$this->element = $element;
}
/**
* @param string $name
*
* @return string
*
* @throws ManifestElementException
*/
protected function getAttributeValue($name) {
if (!$this->element->hasAttribute($name)) {
throw new ManifestElementException(
sprintf(
'Attribute %s not set on element %s',
$name,
$this->element->localName
)
);
}
return $this->element->getAttribute($name);
}
/**
* @param $elementName
*
* @return DOMElement
*
* @throws ManifestElementException
*/
protected function getChildByName($elementName) {
$element = $this->element->getElementsByTagNameNS(self::XMLNS, $elementName)->item(0);
if (!$element instanceof DOMElement) {
throw new ManifestElementException(
sprintf('Element %s missing', $elementName)
);
}
return $element;
}
/**
* @param $elementName
*
* @return DOMNodeList
*
* @throws ManifestElementException
*/
protected function getChildrenByName($elementName) {
$elementList = $this->element->getElementsByTagNameNS(self::XMLNS, $elementName);
if ($elementList->length === 0) {
throw new ManifestElementException(
sprintf('Element(s) %s missing', $elementName)
);
}
return $elementList;
}
/**
* @param string $elementName
*
* @return bool
*/
protected function hasChild($elementName) {
return $this->element->getElementsByTagNameNS(self::XMLNS, $elementName)->length !== 0;
}
}

View File

@@ -0,0 +1,27 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class PhpElement extends ManifestElement {
public function getVersion() {
return $this->getAttributeValue('version');
}
public function hasExtElements() {
return $this->hasChild('ext');
}
public function getExtElements() {
return new ExtElementCollection(
$this->getChildrenByName('ext')
);
}
}

View File

@@ -0,0 +1,19 @@
<?php
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class RequiresElement extends ManifestElement {
public function getPHPElement() {
return new PhpElement(
$this->getChildByName('php')
);
}
}