Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
naneau committed Sep 4, 2014
0 parents commit f1e8c6d
Show file tree
Hide file tree
Showing 11 changed files with 483 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "naneau/project-versioner",
"description": "A tool to maintain project/sub-project versions based on Composer, file mtimes, etc.",
"license": "MIT",
"authors": [
{
"name": "Maurice Fonk",
"email": "[email protected]"
}
],

"autoload": {
"psr-4": {"Naneau\\ProjectVersioner\\": "src/Naneau/ProjectVersioner/"}
},

"minimum-stability": "stable",
"require": {},
"require-dev": {
"phpunit/phpunit": "~4.2"
}
}
16 changes: 16 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
cacheTokens="false"
colors="true"
>

<testsuites>
<testsuite name="ProjectVersioner Test Squite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
40 changes: 40 additions & 0 deletions src/Naneau/ProjectVersioner/Reader/Composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Composer.php
*
* @package ProjectVersioner
* @subpackage Reader
*/

namespace Naneau\ProjectVersioner\Reader;

/**
* Composer
*
* Finds version from composer lock file
*
* @category Naneau
* @package ProjectVersioner
* @subpackage Reader
*/
class Composer
{
/**
* {@inheritdoc}
**/
public function canRead($directory)
{
return is_readable($directory . '/composer.lock');
}

/**
* {@inheritdoc}
**/
public function read($directory)
{
return substr(
md5(file_get_contents($directory . '/composer.lock')),
0, 6
);
}
}
111 changes: 111 additions & 0 deletions src/Naneau/ProjectVersioner/Reader/ComposerPackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* ComposerPackage.php
*
* @package ProjectVersioner
* @subpackage Reader
*/

namespace Naneau\ProjectVersioner\Reader;

/**
* ComposerPackage
*
* Finds the version of a specific Composer package from the lock file
*
* @category Naneau
* @package ProjectVersioner
* @subpackage Reader
*/
class ComposerPackage
{
/**
* The page name to look for
*
* @var string
**/
private $package;

/**
* Constructor
*
* @param string $package
* @return void
**/
public function __construct($package)
{
$this->setPackage($package);
}

/**
* {@inheritdoc}
**/
public function canRead($directory)
{
if (!is_readable($directory . '/composer.lock')) {
return false;
}

$package = $this->getPackageFromLockFile($directory);

if ($package === false) {
return false;
}

return !empty($package->version);
}

/**
* {@inheritdoc}
**/
public function read($directory)
{
$package = $this->getPackageFromLockFile($directory);

return $package->version;
}

/**
* Get the package
*
* @return string
*/
public function getPackage()
{
return $this->package;
}

/**
* Set the package
*
* @param string $package
* @return ComposerPackage
*/
public function setPackage($package)
{
$this->package = $package;

return $this;
}

/**
* Get the package from the lockfile
*
* @return stdClass|bool returns false when package can not be found
**/
private function getPackageFromLockFile($directory)
{
$parsed = json_decode(file_get_contents($directory . '/composer.lock'));
if (!isset($parsed->packages)) {
return false;
}

foreach ($parsed->packages as $package) {
if ($package->name === $this->getPackage()) {
return $package;
}
}

return false;
}
}
81 changes: 81 additions & 0 deletions src/Naneau/ProjectVersioner/Reader/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* File.php
*
* @package ProjectVersioner
* @subpackage Reader
*/

namespace Naneau\ProjectVersioner\Reader;

use Naneau\ProjectVersioner\ReaderInterface;

/**
* File
*
* Reads a version from a file
*
* @category Naneau
* @package ProjectVersioner
* @subpackage Reader
*/
class File implements ReaderInterface
{
/**
* The file
*
* @var string
**/
private $file;

/**
* Constructor
*
* @param string $file version file
* @return void
**/
public function __construct($file)
{
$this->setFile($file);
}

/**
* {@inheritdoc}
**/
public function canRead($directory)
{
return is_readable($directory . '/' . $this->getFile());
}

/**
* {@inheritdoc}
**/
public function read($directory)
{
return trim(file_get_contents($directory . '/' . $this->getFile()));
}

/**
* Get the file
*
* @return string
*/
public function getFile()
{
return $this->file;
}

/**
* Set the file
*
* @param string $file
* @return parent
*/
public function setFile($file)
{
$this->file = $file;

return $this;
}

}
37 changes: 37 additions & 0 deletions src/Naneau/ProjectVersioner/ReaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* ReaderInterface.php
*
* @package ProjectVersioner
* @subpackage Reader
*/

namespace Naneau\ProjectVersioner;

/**
* ReaderInterface
*
* Interface for reading
*
* @category Naneau
* @package ProjectVersioner
* @subpackage Reader
*/
interface ReaderInterface
{
/**
* Can a directory be versioned using this reader?
*
* @param string $directory directory to read the version from
* @return bool
**/
public function canRead($directory);

/**
* Read the version from a directory
*
* @param string $directory directory to read the version from
* @return string version
**/
public function read($directory);
}
Loading

0 comments on commit f1e8c6d

Please sign in to comment.