Skip to content

Commit 4e4c780

Browse files
committed
first commit
0 parents  commit 4e4c780

File tree

9 files changed

+326
-0
lines changed

9 files changed

+326
-0
lines changed

changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
3+
All notable changes to `LaravelDynamicModel` will be documented in this file.
4+
5+
## Version 1.0
6+
7+
### Added
8+
- Everything

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "sairahcaz/laravel-dynamic-model",
3+
"description": "Dynamic Model for Laravel",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Zacharias Creutznacher",
8+
"email": "zacharias.creutznacher@gmail.com",
9+
"homepage": "https://www.zacharias-creutznacher.de/english/"
10+
}
11+
],
12+
"homepage": "https://github.com/sairahcaz/laravel-dynamic-model",
13+
"keywords": ["laravel", "eloquent", "model", "dynamic model", "dynamic table", "dynamic schema"],
14+
"require": {
15+
"php": ">=7.4",
16+
"laravel/framework": "^5.3|^6.0|^7.0|^8.0|^9.0"
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "~9.0",
20+
"orchestra/testbench": "~7"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Sairahcaz\\LaravelDynamicModel\\": "src/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Sairahcaz\\LaravelDynamicModel\\Tests\\": "tests"
30+
}
31+
},
32+
"extra": {
33+
"laravel": {
34+
"providers": [
35+
"DynamicModelServiceProvider"
36+
]
37+
}
38+
},
39+
"minimum-stability": "dev",
40+
"prefer-stable": true
41+
}

config/config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'current_table' => '',
5+
];

contributing.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Contributing
2+
3+
Contributions are welcome and will be fully credited.
4+
5+
Contributions are accepted via Pull Requests on [Github](https://github.com/sairahcaz/laravel-dynamic-model).
6+
7+
# Things you could do
8+
If you want to contribute but do not know where to start, this list provides some starting points.
9+
- Add license text
10+
- Remove rewriteRules.php
11+
- Set up TravisCI, StyleCI, ScrutinizerCI
12+
- Write a comprehensive ReadMe
13+
14+
## Pull Requests
15+
16+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
17+
18+
- **Document any change in behaviour** - Make sure the `readme.md` and any other relevant documentation are kept up-to-date.
19+
20+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
21+
22+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
23+
24+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
25+
26+
27+
**Happy coding**!

license.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# The license
2+
3+
Copyright (c) Zacharias Creutznacher <zacharias.creutznacher@gmail.com>
4+
5+
...Add your license text here...

phpunit.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Package">
14+
<directory suffix=".php">./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory>src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

readme.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# LaravelDynamicModel
2+
3+
[![Latest Version on Packagist][ico-version]][link-packagist]
4+
[![Total Downloads][ico-downloads]][link-downloads]
5+
[![Build Status][ico-travis]][link-travis]
6+
[![StyleCI][ico-styleci]][link-styleci]
7+
8+
This is where your description should go. Take a look at [contributing.md](contributing.md) to see a to do list.
9+
10+
## Installation
11+
12+
Via Composer
13+
14+
``` bash
15+
$ composer require sairahcaz/laravel-dynamic-model
16+
```
17+
18+
## Usage
19+
20+
## Change log
21+
22+
Please see the [changelog](changelog.md) for more information on what has changed recently.
23+
24+
## Testing
25+
26+
``` bash
27+
$ composer test
28+
```
29+
30+
## Contributing
31+
32+
Please see [contributing.md](contributing.md) for details and a todolist.
33+
34+
## Security
35+
36+
If you discover any security related issues, please email zacharias.creutznacher@gmail.com instead of using the issue tracker.
37+
38+
## Credits
39+
40+
- [Zacharias Creutznacher][link-author]
41+
- [All Contributors][link-contributors]
42+
43+
## License
44+
45+
MIT. Please see the [license file](license.md) for more information.
46+
47+
[ico-version]: https://img.shields.io/packagist/v/sairahcaz/laravel-dynamic-model.svg?style=flat-square
48+
[ico-downloads]: https://img.shields.io/packagist/dt/sairahcaz/laravel-dynamic-model.svg?style=flat-square
49+
[ico-travis]: https://img.shields.io/travis/sairahcaz/laravel-dynamic-model/master.svg?style=flat-square
50+
[ico-styleci]: https://styleci.io/repos/12345678/shield
51+
52+
[link-packagist]: https://packagist.org/packages/sairahcaz/laravel-dynamic-model
53+
[link-downloads]: https://packagist.org/packages/sairahcaz/laravel-dynamic-model
54+
[link-travis]: https://travis-ci.org/sairahcaz/laravel-dynamic-model
55+
[link-styleci]: https://styleci.io/repos/12345678
56+
[link-author]: https://github.com/sairahcaz
57+
[link-contributors]: ../../contributors

src/DynamicModel.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Sairahcaz\LaravelDynamicModel;
4+
5+
use Exception;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
class DynamicModel extends Model
10+
{
11+
/**
12+
* The primary key associated with the table.
13+
*
14+
* @var string
15+
*/
16+
protected $primaryKey = 'id';
17+
18+
/**
19+
* Indicates if the model's ID is auto-incrementing.
20+
*
21+
* @var bool
22+
*/
23+
public $incrementing = true;
24+
25+
/**
26+
* The data type of the auto-incrementing ID.
27+
*
28+
* @var string
29+
*/
30+
protected $keyType = 'integer';
31+
32+
protected $routeKeyName = 'id';
33+
34+
public function getRouteKeyName()
35+
{
36+
return $this->routeKeyName;
37+
}
38+
39+
protected $guarded = [];
40+
41+
/**
42+
* important! - attributes need to be passed,
43+
* cause of new instance generation inside laravel
44+
*
45+
* @param $attributes
46+
* @throws Exception
47+
*/
48+
public function __construct($attributes = [])
49+
{
50+
parent::__construct($attributes);
51+
52+
if (!$table = config('dynamic-model.current_table')) {
53+
throw new Exception("Seems like you called DynamicModel directly,
54+
please use service container: App::make(DynamicModel::class, ['table_name' => 'foo'])");
55+
}
56+
57+
$this->table = $table;
58+
59+
if (!Schema::hasTable($this->table)) {
60+
throw new Exception("The table you provided to the DynamicModel does not exists! Please create it first!");
61+
}
62+
63+
$connection = Schema::getConnection();
64+
$table = $connection->getDoctrineSchemaManager()->listTableDetails($this->table);
65+
$primaryKeyName = $table->getPrimaryKey()->getColumns()[0];
66+
$primaryColumn = $connection->getDoctrineColumn($this->table, $primaryKeyName);
67+
68+
$this->primaryKey = $primaryColumn->getName();
69+
$this->incrementing = $primaryColumn->getAutoincrement();
70+
$this->keyType = ($primaryColumn->getType()->getName() === 'string') ? 'string' : 'integer';
71+
$this->routeKeyName = $primaryColumn->getName();
72+
}
73+
}

src/DynamicModelServiceProvider.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Sairahcaz\LaravelDynamicModel;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class DynamicModelServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Perform post-registration booting of services.
11+
*
12+
* @return void
13+
*/
14+
public function boot(): void
15+
{
16+
// $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'sairahcaz');
17+
// $this->loadViewsFrom(__DIR__.'/../resources/views', 'sairahcaz');
18+
// $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
19+
// $this->loadRoutesFrom(__DIR__.'/routes.php');
20+
21+
// Publishing is only necessary when using the CLI.
22+
if ($this->app->runningInConsole()) {
23+
$this->bootForConsole();
24+
}
25+
}
26+
27+
/**
28+
* Register any package services.
29+
*
30+
* @return void
31+
*/
32+
public function register(): void
33+
{
34+
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'dynamic-model');
35+
36+
// Register the service the package provides.
37+
$this->app->bind(DynamicModel::class, function ($app, $parameters = []) {
38+
if (!isset($parameters['table_name'])) {
39+
throw new \Exception('please provide table_name parameter');
40+
}
41+
42+
config()->set('dynamic-model.current_table', $parameters['table_name']);
43+
44+
return new DynamicModel();
45+
});
46+
}
47+
48+
/**
49+
* Get the services provided by the provider.
50+
*
51+
* @return array
52+
*/
53+
public function provides()
54+
{
55+
return ['laravel-dynamic-model'];
56+
}
57+
58+
/**
59+
* Console-specific booting.
60+
*
61+
* @return void
62+
*/
63+
protected function bootForConsole(): void
64+
{
65+
// Publishing the configuration file.
66+
$this->publishes([
67+
__DIR__ . '/../config/config.php' => config_path('dynamic-model.php'),
68+
], 'config');
69+
70+
// Publishing the views.
71+
/*$this->publishes([
72+
__DIR__.'/../resources/views' => base_path('resources/views/vendor/sairahcaz'),
73+
], 'laravel-dynamic-model.views');*/
74+
75+
// Publishing assets.
76+
/*$this->publishes([
77+
__DIR__.'/../resources/assets' => public_path('vendor/sairahcaz'),
78+
], 'laravel-dynamic-model.views');*/
79+
80+
// Publishing the translation files.
81+
/*$this->publishes([
82+
__DIR__.'/../resources/lang' => resource_path('lang/vendor/sairahcaz'),
83+
], 'laravel-dynamic-model.views');*/
84+
85+
// Registering package commands.
86+
// $this->commands([]);
87+
}
88+
}

0 commit comments

Comments
 (0)