forked from naneau/php-project-versioner
-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit f1e8c6d
Showing
11 changed files
with
483 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
composer.lock |
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,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" | ||
} | ||
} |
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 @@ | ||
<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> |
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,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 | ||
); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
|
||
} |
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,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); | ||
} |
Oops, something went wrong.