Skip to content

Commit

Permalink
1.3.0 add --all
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Cabrera committed Apr 13, 2024
1 parent 194aad5 commit dd1170c
Show file tree
Hide file tree
Showing 18 changed files with 208 additions and 47 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

![model-repository](https://socialify.git.ci/Oscabrera/model-repository/image?language=1&name=1&owner=1&pattern=Floating%20Cogs&theme=Auto)

Please follow the documentation at [model-repository](https://oscabrera.github.io/model-repository/).

This package for Laravel greatly simplifies the process of creating a complete RESTful API for any model in your
application. By just running a command, you can generate all the files necessary to create, read, update and delete
instances of said model. This project is based on the Repository pattern, separating data access logic from business
Expand All @@ -26,6 +28,8 @@ abstraction and organization to the code.
- Validation requests (`Request`): Validates input data for controller routes.
- Response resource (`Resource`): Makes it easier to create JSON responses for controller routes.
- Resource collection (`Collection`): Defines the structure of the JSON response for the resource list.
- Seeder (`Seeder`): Populates the database with sample data for testing purposes.
- Factory (`Factory`): Generates dummy data for testing purposes.
- **Predefined CRUD methods:** The generated `Repository`, `Service` and `Controller` implement the standard methods of
a RESTful API:
- `create`: Create a new instance of the model
Expand Down Expand Up @@ -69,7 +73,13 @@ composer require oscabrera/model-repository
2. Run the command to generate the API:

```shell
php artisan make:repository DummyModel --seed --migration --service --controller --request --resource --collection
php artisan make:repository DummyModel --seed --migration --service --controller --request --resource --collection --factory
```

Alternatively, you can use the --all option to generate all available components at once:

```shell
php artisan make:repository DummyModel --all
```

3. Customize the generated code according to your needs.
Expand Down Expand Up @@ -114,8 +124,11 @@ The files generated by the make:repository command are created inside a folder w
├── database
│ ├── migrations
│ │ └── 2024_03_17_022718_create_names_table.php
│ └── Seeders
│ └── NameSeeder.php
│ ├── Seeders
│ │ └── NameSeeder.php
│ └── Factories
│ └── DummyModel
│ └── DummyModelFactory.php
```

**Benefits of organizing by folders:**
Expand Down
6 changes: 6 additions & 0 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export default {
{ text: "Example", link: "/query-options/example" },
],
},
{
text: "Code Quality",
items: [
{ text: "Ensuring Code Quality", link: "/code-quality/code-quality" }
],
},
],
footer: {
message: "Released under the MIT License.",
Expand Down
44 changes: 44 additions & 0 deletions docs/code-quality/code-quality.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Ensuring code quality in the package

In Laravel application development, maintaining code quality is crucial to ensure the project's stability, readability,
and maintainability. To achieve this, you can leverage static analysis tools like **PHPStan** and Laravel **Pint**.

## PHPStan

[PHPStan](https://phpstan.org/) is a static analyzer for PHP that detects type errors, logic issues, and code
violations. It's a powerful tool
to guarantee code reliability and security.

You can use [larastan/larastan](https://github.com/larastan/larastan) to analyze your code.

## Laravel Pint

Laravel Pint is a code formatting tool that enforces Laravel's code style conventions on your project. It helps maintain
consistent and easily readable code.

in this project we use [PSR-12](https://www.php-fig.org/psr/psr-12/) for code formatting.

## Using PHPStan and Laravel Pint Together

By using PHPStan and Laravel Pint together, you can ensure your Laravel code is error-free, consistent with Laravel's
code style, and easy to maintain. PHPStan catches type errors and logic issues, while Laravel Pint takes care of code
formatting.

Utilizing these tools can significantly improve your Laravel code quality and reduce the risk of errors and
inconsistencies.

## Benefits of Using PHPStan and Laravel Pint

- **Reduced Errors**: PHPStan detects type errors and logic problems before code execution, saving you debugging time
and
effort.
- **More Consistent Code**: Laravel Pint enforces Laravel's code style conventions, making your code easier for other
developers to read and understand.
- **Easier Code Maintenance**: Well-formatted and documented code is simpler to maintain and update.
- **Increased Confidence in Code Quality**: Using PHPStan and Laravel Pint allows you to have greater confidence in the
quality of your Laravel code.

## Conclusion

PHPStan and Laravel Pint are valuable tools for ensuring code quality in Laravel projects. By using these tools in
combination, you can enhance the reliability, readability, and maintainability of your codebase.
2 changes: 2 additions & 0 deletions docs/getting-started/Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- Validation requests (`Request`): Validates input data for controller routes.
- Response resource (`Resource`): Makes it easier to create JSON responses for controller routes.
- Resource collection (`Collection`): Defines the structure of the JSON response for the resource list.
- Seeder (`Seeder`): Populates the database with sample data for testing purposes.
- Factory (`Factory`): Generates dummy data for testing purposes.
- **Predefined CRUD methods:** The generated `Repository`, `Service` and `Controller` implement the standard methods of
a RESTful API:
- `create`: Create a new instance of the model
Expand Down
3 changes: 2 additions & 1 deletion docs/query-options/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ $options->conditionQuery($query);
$users = $query->get();
```

This example defines a `$conditions` array with various filtering conditions. Then, an instance of the `QueryOptions` class
This example defines a `$conditions` array with various filtering conditions. Then, an instance of the `QueryOptions`
class
is created and applied to the query builder object using the `conditionQuery` method. Finally, the query is executed to
retrieve the users that meet the specified conditions.
Binary file added docs/src/images/make_repository.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 8 additions & 5 deletions docs/usage/Folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ The generated files are created within a folder named after your model, providin
├── database
│ ├── migrations
│ │ └── 2024_03_17_022718_create_names_table.php
│ └── Seeders
│ └── NameSeeder.php
│ ├── Seeders
│ │ └── NameSeeder.php
│ └── Factories
│ └── DummyModel
│ └── DummyModelFactory.php
```

## Benefits of Organized Folder Structure:

- **Enhanced Organization**: Maintains a clean and well-defined structure, allowing you to easily locate the relevant
files.
files.
- **Improved Modularity**: Promotes modularity by separating the components for each API layer (models, controllers,
services, etc.), making the code more maintainable and reusable.
services, etc.), making the code more maintainable and reusable.
- **Simplified Maintenance**: Facilitates updates and maintenance of the codebase by keeping specific functionality
isolated within its designated files.
isolated within its designated files.
39 changes: 28 additions & 11 deletions docs/usage/Install.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@ To utilize this command and generate the API for your model (`DummyModel` in thi
command in your terminal:

```shell
php artisan make:repository DummyModel --seed --migration --service --controller --request --resource --collection
php artisan make:repository DummyModel --seed --migration --service --controller --request --resource --collection --factory
```

Alternatively, you can use the --all option to generate all available components at once:

```shell
php artisan make:repository DummyModel --all
```

This is an example of the output you'll see after running the command:

![Command Output](/src/images/make_repository.png)

## Command Breakdown:

php artisan: Invokes the Artisan CLI.
Expand All @@ -38,14 +48,17 @@ DummyModel: Denotes the name of your model for which the API will be created.

### Optional Flags:

- --seed: Generates a seed file to populate your database with sample data for testing purposes.
- --migration: Creates a migration file to define the database schema for your model.
- --service: Generates a service class to encapsulate business logic related to your model operations.
- --controller: Creates a controller class that handles incoming API requests and interacts with the service layer.
- --request: Generates request classes for validation and data formatting during API interactions.
- --resource: Creates a resource class to facilitate data formatting for API responses.
- --collection: Generates a collection class that assists in transforming model collections into a standardized format
for API responses.
- **`--seed, -sd`**: Generates a seed file to populate your database with sample data for testing purposes.
- **`--migration, -m`**: Creates a migration file to define the database schema for your model.
- **`--factory, -f`**: Generates a factory class to generate dummy data for your model.
- **`--service, -s`**: Generates a service class to encapsulate business logic related to your model operations.
- **`--controller, -c`**: Creates a controller class that handles incoming API requests and interacts with the service layer.
- **`--request, -r`**: Generates request classes for validation and data formatting during API interactions.
- **`--resource, -res`**: Creates a resource class to facilitate data formatting for API responses.
- **`--collection, -col`**: Generates a collection class that assists in transforming model collections into a standardized
format for API responses.
- **`--all`**: Creates all structure for working with the Repository.
- **`--force`**: Overwrites existing files if they already exist.

### Customization:

Expand All @@ -68,6 +81,10 @@ php artisan migrate
This command processes the generated migration file, instructing your database to create the tables required for your
model's data.

::: tip
Change the permissions of the Request more information [here](./Authorize)
:::

## Running the API

4. Ready for Action!
Expand All @@ -77,10 +94,10 @@ functional RESTful API for your model (`DummyModel`). You can interact with this
depending on your preferences and testing strategies. Here are some potential approaches:

- Direct API Calls: Employ HTTP request tools like Postman, curl, or an HTTP client library within your code to send
requests to the API endpoints for various operations (create, read, update, and delete) on your model's data.
requests to the API endpoints for various operations (create, read, update, and delete) on your model's data.

- Laravel's Testing Mechanisms: If you're utilizing Laravel's built-in testing framework (PHPUnit), you can craft unit
tests to verify the API's functionality and ensure its correct behavior under various conditions.
tests to verify the API's functionality and ensure its correct behavior under various conditions.

---

Expand Down
2 changes: 1 addition & 1 deletion semver.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "v1.2.0",
"version": "v1.3.0",
"last-update-type": "minor"
}
31 changes: 31 additions & 0 deletions src/Classes/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class Options
*/
public bool $hasMigration = false;

/**
* @var bool $hasFactory Indicates if the factory would be to created
*/
public bool $hasFactory = false;

/**
* @var bool $hasController Indicates if the controller would be to created
*/
Expand Down Expand Up @@ -43,4 +48,30 @@ class Options
* @var bool $hasSeeder Indicates if the seeder would be to created
*/
public bool $hasSeeder = false;

/**
* @var bool $force Indicates if the command should be forced
*/
public bool $force = false;

/**
* Set all force options to true if $all is true.
*
* @param bool $all Whether to force all options or not.
* @return void
*/
public function forceAll(bool $all): void
{
if (!$all) {
return;
}
$this->hasMigration = true;
$this->hasController = true;
$this->hasResource = true;
$this->hasCollection = true;
$this->hasRequest = true;
$this->hasService = true;
$this->hasSeeder = true;
$this->hasFactory = true;
}
}
16 changes: 13 additions & 3 deletions src/Commands/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,30 @@ class Handlers extends Command
/**
* The name and signature of the console command.
* use options:
* --seed --migration --service --interface --controller --request --resource --collection
* --seed --migration --service --interface --controller --request --resource --collection --all --force
*
* @var string
*/
protected $signature = 'make:repository {name}'
. ' {--seed} {--migration} {--service} {--controller} {--request} {--resource} {--collection}';
. ' {--sd|seed : Create a seed file to populate your database with sample data}'
. ' {--m|migration : Create a migration file to define the database schema for your model}'
. ' {--f|factory : Create the class even if the model already exists}'
. ' {--s|service : Create a service class to encapsulate business logic related to your repository operations}'
. ' {--c|controller : Create a controller class that handles incoming API requests and interacts with the service layer}'
. ' {--r|request : Create request classes for validation and data formatting during API interactions}'
. ' {--res|resource : Create a resource class to facilitate data formatting for API responses}'
. ' {--col|collection : Create a collection class that assists in transforming model collections into a standardized format for API responses}'
. ' {--all : Create all structure for working with the Repository}'
. ' {--force : Overwrites existing files if they already exist}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new model and its repository, with the options for '
. 'creating all structure for working with the Repository';
. 'creating all structure for working with the Repository'
. ' with the options for creating all structure for working with the Repository';

/**Command
* Execute the console command.
Expand Down
Loading

0 comments on commit dd1170c

Please sign in to comment.