-
Notifications
You must be signed in to change notification settings - Fork 20
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
1 parent
2fc7e51
commit 7591dc2
Showing
2 changed files
with
240 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,203 @@ | ||
<?php namespace Arcanedev\Support; | ||
|
||
/** | ||
* Class Stub | ||
* @package Arcanedev\Support | ||
*/ | ||
class Stub | ||
{ | ||
/* ------------------------------------------------------------------------------------------------ | ||
| Properties | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* The stub path. | ||
* | ||
* @var string | ||
*/ | ||
protected $path; | ||
|
||
/** | ||
* The base path of stub file. | ||
* | ||
* @var null|string | ||
*/ | ||
protected static $basePath = null; | ||
|
||
/** | ||
* The replacements array. | ||
* | ||
* @var array | ||
*/ | ||
protected $replaces = []; | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Constructor | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* The constructor. | ||
* | ||
* @param string $path | ||
* @param array $replaces | ||
*/ | ||
public function __construct($path, array $replaces = []) | ||
{ | ||
$this->path = $path; | ||
$this->replaces = $replaces; | ||
} | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Getters & Setters | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Get stub path. | ||
* | ||
* @return string | ||
*/ | ||
public function getPath() | ||
{ | ||
return static::$basePath . $this->path; | ||
} | ||
|
||
/** | ||
* Set stub path. | ||
* | ||
* @param string $path | ||
* | ||
* @return self | ||
*/ | ||
public function setPath($path) | ||
{ | ||
$this->path = $path; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get base path. | ||
* | ||
* @return string|null | ||
*/ | ||
public static function getBasePath() | ||
{ | ||
return static::$basePath; | ||
} | ||
|
||
/** | ||
* Set base path. | ||
* | ||
* @param string $path | ||
*/ | ||
public static function setBasePath($path) | ||
{ | ||
static::$basePath = $path; | ||
} | ||
|
||
/** | ||
* Get replacements. | ||
* | ||
* @return array | ||
*/ | ||
public function getReplaces() | ||
{ | ||
return $this->replaces; | ||
} | ||
|
||
/** | ||
* Set replacements array. | ||
* | ||
* @param array $replaces | ||
* | ||
* @return $this | ||
*/ | ||
public function replace(array $replaces = []) | ||
{ | ||
$this->replaces = $replaces; | ||
|
||
return $this; | ||
} | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Main Functions | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Create new self instance. | ||
* | ||
* @param string $path | ||
* @param array $replaces | ||
* | ||
* @return self | ||
*/ | ||
public static function create($path, array $replaces = []) | ||
{ | ||
return new static($path, $replaces); | ||
} | ||
|
||
/** | ||
* Create new self instance from full path. | ||
* | ||
* @param string $path | ||
* @param array $replaces | ||
* | ||
* @return self | ||
*/ | ||
public static function createFromPath($path, array $replaces = []) | ||
{ | ||
$stub = static::create($path, $replaces); | ||
$stub->setBasePath(''); | ||
|
||
return $stub; | ||
} | ||
|
||
/** | ||
* Get stub contents. | ||
* | ||
* @return string | ||
*/ | ||
public function render() | ||
{ | ||
return $this->getContents(); | ||
} | ||
|
||
/** | ||
* Get stub contents. | ||
* | ||
* @return mixed|string | ||
*/ | ||
public function getContents() | ||
{ | ||
$contents = file_get_contents($this->getPath()); | ||
|
||
foreach ($this->replaces as $search => $replace) { | ||
$contents = str_replace('$' . strtoupper($search) . '$', $replace, $contents); | ||
} | ||
|
||
return $contents; | ||
} | ||
|
||
/** | ||
* Save stub to specific path. | ||
* | ||
* @param string $path | ||
* @param string $filename | ||
* | ||
* @return bool | ||
*/ | ||
public function saveTo($path, $filename) | ||
{ | ||
return file_put_contents($path . '/' . $filename, $this->getContents()); | ||
} | ||
|
||
/** | ||
* Handle magic method __toString. | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->render(); | ||
} | ||
} |
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 namespace Arcanedev\Support\Tests; | ||
|
||
use Arcanedev\Support\Stub; | ||
|
||
class StubTest extends TestCase | ||
{ | ||
/* ------------------------------------------------------------------------------------------------ | ||
| Properties | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** @var Stub */ | ||
private $stub; | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Main Functions | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
parent::tearDown(); | ||
} | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Test Functions | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** @test */ | ||
public function it_can_be_instantiated() | ||
{ | ||
|
||
} | ||
} |