-
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
Showing
5 changed files
with
172 additions
and
19 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,99 @@ | ||
<?php | ||
|
||
namespace JG\Task; | ||
|
||
use \Task; | ||
|
||
/** | ||
* Class LoadConfigTask | ||
* | ||
* @package JG\Task | ||
* @version 1.0 | ||
* @author Julien Guittard <julien.guittard@me.com> | ||
*/ | ||
class LoadConfigTask extends Task | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $fromFile; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $toProperty; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $wait; | ||
|
||
/** | ||
* Set the fromFile | ||
* | ||
* @param string $fromFile | ||
* @return LoadConfigTask | ||
*/ | ||
public function setFromFile($fromFile) | ||
{ | ||
$this->fromFile = $fromFile; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Set the toProperty | ||
* | ||
* @param string $toProperty | ||
* @return LoadConfigTask | ||
*/ | ||
public function setToProperty($toProperty) | ||
{ | ||
$this->toProperty = $toProperty; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Set the wait | ||
* | ||
* @param int $wait | ||
* @return LoadConfigTask | ||
*/ | ||
public function setWait($wait) | ||
{ | ||
$this->wait = (int)$wait; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Task call | ||
* | ||
* @throws \Exception | ||
* @return void | ||
*/ | ||
public function main() | ||
{ | ||
if (empty($this->fromFile)) { | ||
throw new \Exception('File path must be set to load config'); | ||
} | ||
$wait = $this->wait ?: 5; | ||
for ($i=0; $i<10; $i++) { | ||
if (!file_exists($this->fromFile)) { | ||
sleep($wait); | ||
continue; | ||
} | ||
break; | ||
} | ||
if (!file_exists($this->fromFile)) { | ||
throw new \Exception('File path must exist to load config'); | ||
} | ||
$content = file_get_contents($this->fromFile); | ||
$config = json_decode($content, true); | ||
if (!$config) { | ||
throw new \Exception('File content must be valid JSON'); | ||
} | ||
foreach ($config as $key => $value) { | ||
$this->project->setNewProperty($this->toProperty .$key, $value); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace JGTests\Task; | ||
|
||
use JG\Task\LoadConfigTask; | ||
|
||
/** | ||
* Class LoadConfigTaskTest | ||
* | ||
* @package JGTest\Task | ||
* @version 1.0 | ||
* @author Julien Guittard <julien.guittard@me.com> | ||
*/ | ||
class LoadConfigTaskTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var LoadConfigTask | ||
*/ | ||
protected $task; | ||
/** | ||
* Setup | ||
* | ||
* @return void | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->task = new LoadConfigTask(); | ||
} | ||
/** | ||
* Test the fromFile Setter | ||
* | ||
* @return void | ||
*/ | ||
public function testFromFileSetter() | ||
{ | ||
$fromFile = '/usr/null'; | ||
$this->assertSame($this->task, $this->task->setFromFile($fromFile)); | ||
$this->assertAttributeSame($fromFile, 'fromFile', $this->task); | ||
} | ||
/** | ||
* Test the toProperty Setter | ||
* | ||
* @return void | ||
*/ | ||
public function testToPropertySetter() | ||
{ | ||
$toProperty = 'myConfig'; | ||
$this->assertSame($this->task, $this->task->setToProperty($toProperty)); | ||
$this->assertAttributeSame($toProperty, 'toProperty', $this->task); | ||
} | ||
/** | ||
* Test the wait Setter | ||
* | ||
* @return void | ||
*/ | ||
public function waitPropertySetter() | ||
{ | ||
$wait = 5; | ||
$this->assertSame($this->task, $this->task->setWait($wait)); | ||
$this->assertAttributeSame($wait, 'wait', $this->task); | ||
} | ||
} |
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