-
Notifications
You must be signed in to change notification settings - Fork 10
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
Md. Touhidur Rahman
authored and
Md. Touhidur Rahman
committed
Sep 7, 2021
0 parents
commit 4562bfb
Showing
21 changed files
with
1,581 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,5 @@ | ||
.DS_Store | ||
/vendor | ||
/build | ||
composer.lock | ||
.phpunit.result.cache |
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,17 @@ | ||
language: php | ||
|
||
php: | ||
- 7.3 | ||
- 7.4 | ||
- 8.0 | ||
|
||
env: | ||
matrix: | ||
- COMPOSER_FLAGS="--prefer-lowest" | ||
- COMPOSER_FLAGS="" | ||
|
||
before_script: | ||
- travis_retry composer update ${COMPOSER_FLAGS} | ||
|
||
script: | ||
- vendor/bin/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,7 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
## [Unreleased] | ||
|
||
## [1.0.0] - 2021-09-07 | ||
- Initial release |
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 @@ | ||
MIT License | ||
|
||
Copyright (c) Touhidur Rahman Abir | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,221 @@ | ||
# Laravel Stub Generator | ||
|
||
A php laravel package to generate useable php files from given stub files . | ||
|
||
## Installation | ||
|
||
Require the package using composer: | ||
|
||
```bash | ||
composer require touhidurabir/laravel-stub-generator | ||
``` | ||
|
||
## Usage | ||
|
||
The best approach to use this via the facade as | ||
|
||
```php | ||
use Touhidurabir\StubGenerator\Facades\StubGenerator | ||
``` | ||
|
||
and then implement as follow | ||
|
||
```php | ||
StubGenerator::from('some/path/to/stub/file.stub') // the stub file path | ||
->to('some/path/to/store/generated/file') // the store directory path | ||
->as('TheGeneratingFileNameItself') // the generatable file name without extension | ||
->withReplacers([]) // the stub replacing params | ||
->save(); // save the file | ||
``` | ||
|
||
Also possible to get the generated content as string or download the generated file | ||
|
||
```php | ||
StubGenerator::from('path')->to('path')->as('name')->withReplacers([])->toString(); // get generated content | ||
StubGenerator::from('path')->as('name')->withReplacers([])->download(); // download the file | ||
``` | ||
|
||
By default it assume all the given path for **from** and **to** methods are relative path, but it can also work with absolute path by specifying that. | ||
|
||
```php | ||
StubGenerator::from('some/path/to/stub/file.stub', true) // the second argument **true** specify absolute path | ||
->to('some/path/to/store/generated/file', false, true) // the third argument **true** specify absolute path | ||
->as('TheGeneratingFileNameItself') | ||
->withReplacers([]) | ||
->save(); | ||
``` | ||
|
||
Also if the store directory path does not exists, it can create that target store path if that is specified in the method call. | ||
|
||
```php | ||
StubGenerator::from('some/path/to/stub/file.stub', true) | ||
->to('some/path/to/store/generated/file', true, true) // the second argument **true** specify to generated path if not exists | ||
->as('TheGeneratingFileNameItself') | ||
->withReplacers([]) | ||
->save(); | ||
``` | ||
|
||
If the saved generated file aleady exists, it is also possible to replace that with the newly generated one if that is specified via the **replace** method. | ||
|
||
```php | ||
StubGenerator::from('some/path/to/stub/file.stub') // the stub file path | ||
->to('some/path/to/store/generated/file') // the store directory path | ||
->as('TheGeneratingFileNameItself') // the generatable file name without extension | ||
->withReplacers([]) // the stub replacing params | ||
->replace(true) // instruct to replace if already exists at the give path | ||
->save(); // save the file | ||
``` | ||
|
||
## Example | ||
|
||
Considering the following stub file | ||
|
||
```stub | ||
<?php | ||
namespace {{classNamespace}}; | ||
use {{baseClass}}; | ||
use {{modelNamespace}}\{{model}}; | ||
class {{class}} extends {{baseClassName}} { | ||
/** | ||
* Constructor to bind model to repo | ||
* | ||
* @param object<{{modelNamespace}}\{{model}}> ${{modelInstance}} | ||
* @return void | ||
*/ | ||
public function __construct({{model}} ${{modelInstance}}) { | ||
$this->model = ${{modelInstance}}; | ||
$this->modelClass = get_class(${{modelInstance}}); | ||
} | ||
} | ||
``` | ||
|
||
If the stub file stored at the location of **app/stubs/repository.stub** and we want to create a new repository file named **UserRepository.php** at the path **app/Repositories/**, then | ||
|
||
```php | ||
StubGeneratorFacade::from('/app/stubs/repository.stub') | ||
->to('/app/Repositories', true) | ||
->as('UserRepository') | ||
->withReplacers([ | ||
'class' => 'UserRepository', | ||
'model' => 'User', | ||
'modelInstance' => 'user', | ||
'modelNamespace' => 'App\\Models', | ||
'baseClass' => 'Touhidurabir\\ModelRepository\\BaseRepository', | ||
'baseClassName' => 'BaseRepository', | ||
'classNamespace' => 'App\\Repositories', | ||
]) | ||
->save(); | ||
``` | ||
|
||
will generate such file content of **UserRepository.php** | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Repositories; | ||
|
||
use Touhidurabir\ModelRepository\BaseRepository; | ||
use App\Models\User; | ||
|
||
class UserRepository extends BaseRepository { | ||
|
||
/** | ||
* Constructor to bind model to repo | ||
* | ||
* @param object<App\Models\User> $user | ||
* @return void | ||
*/ | ||
public function __construct(User $user) { | ||
|
||
$this->model = $user; | ||
|
||
$this->modelClass = get_class($user); | ||
} | ||
|
||
} | ||
|
||
``` | ||
|
||
## Extras | ||
|
||
Sometimes what we have is the namespace that follows the **psr-4** standeard and that namespace path is what we intend to use for path. This package can not direcly work with the namespace path and it includes a handy trait that can help up to some extent. | ||
|
||
To use the **trait** | ||
|
||
```php | ||
|
||
use Touhidurabir\StubGenerator\Concerns\NamespaceResolver; | ||
|
||
class Someclass { | ||
|
||
use NamespaceResolver; | ||
} | ||
``` | ||
|
||
### Available Methods of **NamespaceResolver** | ||
|
||
#### resolveClassName(string $name) | ||
|
||
As per defination | ||
|
||
```php | ||
/** | ||
* Resolve the class name and class store path from give class namespace | ||
* In case a full class namespace provides, need to extract class name | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public function resolveClassName(string $name) | ||
``` | ||
|
||
It extract the class name from given full class namespace. | ||
|
||
#### resolveClassNamespace(string $name) | ||
|
||
As per defination | ||
|
||
```php | ||
/** | ||
* Resolve the class namespace from given class name | ||
* | ||
* @param string $name | ||
* @return mixed<string|null> | ||
*/ | ||
public function resolveClassNamespace(string $name) | ||
``` | ||
|
||
It extract the class namespace only from given full class namespace. | ||
|
||
#### generateFilePathFromNamespace(string $namespace = null) | ||
|
||
As per defination | ||
|
||
```php | ||
/** | ||
* Generate class store path from give namespace | ||
* | ||
* @param mixed<string|null> $namespace | ||
* @return mixed<string|null> | ||
*/ | ||
public function generateFilePathFromNamespace(string $namespace = null) | ||
``` | ||
|
||
It generate the class relative path from given class full namespace. | ||
|
||
|
||
## Contributing | ||
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. | ||
|
||
Please make sure to update tests as appropriate. | ||
|
||
## License | ||
[MIT](./LICENSE.md) |
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,41 @@ | ||
{ | ||
"name": "touhidurabir/laravel-stub-generator", | ||
"description": "A laravel package to generate class files from stub files.", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Touhidur Rahman", | ||
"email": "abircse06@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.2.0", | ||
"illuminate/support": "^8.57" | ||
}, | ||
"autoload" : { | ||
"psr-4" : { | ||
"Touhidurabir\\StubGenerator\\": "src/" | ||
} | ||
}, | ||
"autoload-dev" : { | ||
"psr-4" : { | ||
"Touhidurabir\\StubGenerator\\Tests\\": "tests/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5", | ||
"orchestra/testbench": "^6.20", | ||
"illuminate/container": "^8.57" | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Touhidurabir\\StubGenerator\\StubGeneratorServiceProvider" | ||
], | ||
"aliases": { | ||
"ModelRepository": "Touhidurabir\\StubGenerator\\Facades\\StubGenerator" | ||
} | ||
} | ||
} | ||
} |
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,36 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Laravel Stub Generator TestSuite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">src/</directory> | ||
</include> | ||
</coverage> | ||
<php> | ||
<env name="APP_ENV" value="testing"/> | ||
<env name="DB_CONNECTION" value="sqlite"/> | ||
<env name="DB_DATABASE" value=":memory:"/> | ||
<env name="CACHE_DRIVER" value="array"/> | ||
<env name="SESSION_DRIVER" value="array"/> | ||
<env name="QUEUE_DRIVER" value="sync"/> | ||
</php> | ||
<logging> | ||
<log type="tap" target="build/report.tap"/> | ||
<log type="junit" target="build/report.junit.xml"/> | ||
<log type="coverage-text" target="build/coverage.txt"/> | ||
<log type="coverage-clover" target="build/logs/clover.xml"/> | ||
</logging> | ||
</phpunit> |
Oops, something went wrong.