Browse Source

php di install

main
TristanC98 2 years ago
parent
commit
2c8b442791
6 changed files with 294 additions and 19 deletions
  1. 0
    0
      Users/Model/DB.php
  2. 19
    0
      Users/View/Twig.php
  3. 2
    1
      composer.json
  4. 244
    11
      composer.lock
  5. 22
    6
      public/index.php
  6. 7
    1
      templates/navbar.html.twig

Users/DB.php → Users/Model/DB.php View File


+ 19
- 0
Users/View/Twig.php View File

@@ -26,4 +26,23 @@ class Twig {
return $this->twig->load($template);

}

public function addBlockVariable($block, $data){
// to do add data to var

$current = $this->variables[$block];
if ($current) {
$new = array_merge($current, $data);
} else {
$new = $data;
}
$this->variables[$block] = $new;
}

public function render($template, $vars) {

$temp = $this->load($template);
$variables = $vars; // TO DO add global var
return $temp->render($variables);
}
}

+ 2
- 1
composer.json View File

@@ -2,7 +2,8 @@
"require": {
"slim/slim": "4.*",
"slim/psr7": "^1.5",
"twig/twig": "^3.0"
"twig/twig": "^3.0",
"php-di/php-di": "^6.3"
},
"config": {
"platform-check": false

+ 244
- 11
composer.lock View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "599b28b1091417b9a8e23442a8e6220a",
"content-hash": "4630a6452ef26ee67e9fd1d2651d237e",
"packages": [
{
"name": "fig/http-message-util",
@@ -113,28 +113,261 @@
"time": "2018-02-13T20:26:39+00:00"
},
{
"name": "psr/container",
"version": "2.0.2",
"name": "opis/closure",
"version": "3.6.3",
"source": {
"type": "git",
"url": "https://github.com/php-fig/container.git",
"reference": "c71ecc56dfe541dbd90c5360474fbc405f8d5963"
"url": "https://github.com/opis/closure.git",
"reference": "3d81e4309d2a927abbe66df935f4bb60082805ad"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/container/zipball/c71ecc56dfe541dbd90c5360474fbc405f8d5963",
"reference": "c71ecc56dfe541dbd90c5360474fbc405f8d5963",
"url": "https://api.github.com/repos/opis/closure/zipball/3d81e4309d2a927abbe66df935f4bb60082805ad",
"reference": "3d81e4309d2a927abbe66df935f4bb60082805ad",
"shasum": ""
},
"require": {
"php": ">=7.4.0"
"php": "^5.4 || ^7.0 || ^8.0"
},
"require-dev": {
"jeremeamia/superclosure": "^2.0",
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
"dev-master": "3.6.x-dev"
}
},
"autoload": {
"files": [
"functions.php"
],
"psr-4": {
"Opis\\Closure\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Marius Sarca",
"email": "marius.sarca@gmail.com"
},
{
"name": "Sorin Sarca",
"email": "sarca_sorin@hotmail.com"
}
],
"description": "A library that can be used to serialize closures (anonymous functions) and arbitrary objects.",
"homepage": "https://opis.io/closure",
"keywords": [
"anonymous functions",
"closure",
"function",
"serializable",
"serialization",
"serialize"
],
"support": {
"issues": "https://github.com/opis/closure/issues",
"source": "https://github.com/opis/closure/tree/3.6.3"
},
"time": "2022-01-27T09:35:39+00:00"
},
{
"name": "php-di/invoker",
"version": "2.3.3",
"source": {
"type": "git",
"url": "https://github.com/PHP-DI/Invoker.git",
"reference": "cd6d9f267d1a3474bdddf1be1da079f01b942786"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-DI/Invoker/zipball/cd6d9f267d1a3474bdddf1be1da079f01b942786",
"reference": "cd6d9f267d1a3474bdddf1be1da079f01b942786",
"shasum": ""
},
"require": {
"php": ">=7.3",
"psr/container": "^1.0|^2.0"
},
"require-dev": {
"athletic/athletic": "~0.1.8",
"mnapoli/hard-mode": "~0.3.0",
"phpunit/phpunit": "^9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Invoker\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Generic and extensible callable invoker",
"homepage": "https://github.com/PHP-DI/Invoker",
"keywords": [
"callable",
"dependency",
"dependency-injection",
"injection",
"invoke",
"invoker"
],
"support": {
"issues": "https://github.com/PHP-DI/Invoker/issues",
"source": "https://github.com/PHP-DI/Invoker/tree/2.3.3"
},
"funding": [
{
"url": "https://github.com/mnapoli",
"type": "github"
}
],
"time": "2021-12-13T09:22:56+00:00"
},
{
"name": "php-di/php-di",
"version": "6.3.5",
"source": {
"type": "git",
"url": "https://github.com/PHP-DI/PHP-DI.git",
"reference": "b8126d066ce144765300ee0ab040c1ed6c9ef588"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-DI/PHP-DI/zipball/b8126d066ce144765300ee0ab040c1ed6c9ef588",
"reference": "b8126d066ce144765300ee0ab040c1ed6c9ef588",
"shasum": ""
},
"require": {
"opis/closure": "^3.5.5",
"php": ">=7.2.0",
"php-di/invoker": "^2.0",
"php-di/phpdoc-reader": "^2.0.1",
"psr/container": "^1.0"
},
"provide": {
"psr/container-implementation": "^1.0"
},
"require-dev": {
"doctrine/annotations": "~1.2",
"friendsofphp/php-cs-fixer": "^2.4",
"mnapoli/phpunit-easymock": "^1.2",
"ocramius/proxy-manager": "^2.0.2",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^8.5|^9.0"
},
"suggest": {
"doctrine/annotations": "Install it if you want to use annotations (version ~1.2)",
"ocramius/proxy-manager": "Install it if you want to use lazy injection (version ~2.0)"
},
"type": "library",
"autoload": {
"files": [
"src/functions.php"
],
"psr-4": {
"DI\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "The dependency injection container for humans",
"homepage": "https://php-di.org/",
"keywords": [
"PSR-11",
"container",
"container-interop",
"dependency injection",
"di",
"ioc",
"psr11"
],
"support": {
"issues": "https://github.com/PHP-DI/PHP-DI/issues",
"source": "https://github.com/PHP-DI/PHP-DI/tree/6.3.5"
},
"funding": [
{
"url": "https://github.com/mnapoli",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/php-di/php-di",
"type": "tidelift"
}
],
"time": "2021-09-02T09:49:58+00:00"
},
{
"name": "php-di/phpdoc-reader",
"version": "2.2.1",
"source": {
"type": "git",
"url": "https://github.com/PHP-DI/PhpDocReader.git",
"reference": "66daff34cbd2627740ffec9469ffbac9f8c8185c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-DI/PhpDocReader/zipball/66daff34cbd2627740ffec9469ffbac9f8c8185c",
"reference": "66daff34cbd2627740ffec9469ffbac9f8c8185c",
"shasum": ""
},
"require": {
"php": ">=7.2.0"
},
"require-dev": {
"mnapoli/hard-mode": "~0.3.0",
"phpunit/phpunit": "^8.5|^9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"PhpDocReader\\": "src/PhpDocReader"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "PhpDocReader parses @var and @param values in PHP docblocks (supports namespaced class names with the same resolution rules as PHP)",
"keywords": [
"phpdoc",
"reflection"
],
"support": {
"issues": "https://github.com/PHP-DI/PhpDocReader/issues",
"source": "https://github.com/PHP-DI/PhpDocReader/tree/2.2.1"
},
"time": "2020-10-12T12:39:22+00:00"
},
{
"name": "psr/container",
"version": "1.1.2",
"source": {
"type": "git",
"url": "https://github.com/php-fig/container.git",
"reference": "513e0666f7216c7459170d56df27dfcefe1689ea"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/container/zipball/513e0666f7216c7459170d56df27dfcefe1689ea",
"reference": "513e0666f7216c7459170d56df27dfcefe1689ea",
"shasum": ""
},
"require": {
"php": ">=7.4.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Psr\\Container\\": "src/"
@@ -161,9 +394,9 @@
],
"support": {
"issues": "https://github.com/php-fig/container/issues",
"source": "https://github.com/php-fig/container/tree/2.0.2"
"source": "https://github.com/php-fig/container/tree/1.1.2"
},
"time": "2021-11-05T16:47:00+00:00"
"time": "2021-11-05T16:50:12+00:00"
},
{
"name": "psr/http-factory",

+ 22
- 6
public/index.php View File

@@ -3,7 +3,7 @@
declare(strict_types=1);
session_start();

use DI\Container;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
@@ -12,10 +12,16 @@ use Users\View\Twig;

require __DIR__. '/../vendor/autoload.php';

$twig = new Twig('../templates');
$container = new Container();

// de container is een lijst van key values pairs, waarbij de value een object is
//of de functie die het object voor de eeerste keer aanmaakt


AppFactory::setContainer($container);
$app = AppFactory::create(); //:: static create

$twig = new Twig('../templates');


// routes definieren
@@ -29,10 +35,9 @@ $app->map(['GET', 'POST'],'/create', function(Request $request, Response $respon

if($request->getMethod()=='GET'){
global $twig;
$template = $twig->load('create.html.twig');
$vars = ["key"=>"Twig var test", "key2"=>["test","test2","test3"],
"key3"=>["assoc" =>"assoc array"]];
$render = $template->render($vars);
$render = $twig->render('create.html.twig',$vars);
$response->getBody()->write($render);
return $response;
}
@@ -57,7 +62,7 @@ $app->map(['GET', 'POST'],'/create', function(Request $request, Response $respon


$app->get('/', function(Request $request, Response $response, $args){
$name = '/create';
addNavbar();
global $twig;
$template = $twig->load('index.html.twig');
$render = $template->render();
@@ -65,4 +70,15 @@ $app->get('/', function(Request $request, Response $response, $args){
return $response;
});
$test = 1;
$app->run();
$app->run();


function addNavbar(){
global $twig;
$urls = [
["link"=> "https://www.google.be", "name"=> "Google"],
["link"=> "https://www.facebook.com", "name"=> "Google"],
["link"=> "https://www.twitter.com", "name"=> "Google"]
];
$twig->addBlockVariable('navbar', ['urls'=> $urls]);
}

+ 7
- 1
templates/navbar.html.twig View File

@@ -3,4 +3,10 @@
<li><a href="/create">Create User</a></li>
<li><a href="">Info</a></li>
<li><a href="">Contact</a></li>
</ul>
</ul>

<ul>
{% for url in navbar.urls %}
<li><a href="{{url.link}}"> {{url.name}} </a><li>
{% endfor %}
</ul>

Loading…
Cancel
Save