Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GReaper-DEV/e918fbce75599e45c7ad1fb4058e5b86 to your computer and use it in GitHub Desktop.
Save GReaper-DEV/e918fbce75599e45c7ad1fb4058e5b86 to your computer and use it in GitHub Desktop.
php php
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/untitled.iml" filepath="$PROJECT_DIR$/.idea/untitled.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpIncludePathManager">
<include_path>
<path value="$PROJECT_DIR$/vendor/composer" />
</include_path>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/vendor/composer" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="0d4b324e-9479-4341-8e5e-bca5e95c4886" name="Default Changelist" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ComposerSettings" doNotAsk="true" synchronizationState="SYNCHRONIZE">
<pharConfigPath>$PROJECT_DIR$/composer.json</pharConfigPath>
<execution />
</component>
<component name="DatabaseView">
<option name="SHOW_INTERMEDIATE" value="true" />
<option name="GROUP_DATA_SOURCES" value="true" />
<option name="GROUP_SCHEMA" value="true" />
<option name="GROUP_CONTENTS" value="false" />
<option name="SORT_POSITIONED" value="false" />
<option name="SHOW_EMPTY_GROUPS" value="false" />
<option name="AUTO_SCROLL_FROM_SOURCE" value="false" />
<option name="HIDDEN_KINDS">
<set />
</option>
<expand />
<select />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="CSS File" />
</list>
</option>
</component>
<component name="PhpWorkspaceProjectConfiguration">
<include_path>
<path value="$PROJECT_DIR$/vendor/composer" />
</include_path>
</component>
<component name="ProjectId" id="1YuPrP71M7jy4zjM0mR85i8JCAr" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="list.type.of.created.stylesheet" value="CSS" />
<property name="settings.editor.selected.configurable" value="preferences.lookFeel" />
</component>
<component name="RunManager">
<configuration name="index.php" type="PhpLocalRunConfigurationType" factoryName="PHP Console" temporary="true" path="$PROJECT_DIR$/index.php">
<CommandLine>
<PhpTestInterpreterSettings>
<option name="interpreterName" value="PHP 7.4" />
</PhpTestInterpreterSettings>
</CommandLine>
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="PHP Script.index.php" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="0d4b324e-9479-4341-8e5e-bca5e95c4886" name="Default Changelist" comment="" />
<created>1583789703593</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1583789703593</updated>
<workItem from="1583789704706" duration="1787000" />
<workItem from="1584282431241" duration="8856000" />
<workItem from="1584372904424" duration="9733000" />
<workItem from="1584449880503" duration="9358000" />
<workItem from="1584521008066" duration="6072000" />
<workItem from="1584652332799" duration="3291000" />
<workItem from="1585225219051" duration="851000" />
<workItem from="1585233287559" duration="5865000" />
<workItem from="1585305567059" duration="4072000" />
<workItem from="1585315789129" duration="1279000" />
<workItem from="1585322979579" duration="94000" />
<workItem from="1585386685792" duration="2089000" />
<workItem from="1586092134287" duration="3109000" />
<workItem from="1586095280634" duration="12410000" />
<workItem from="1586154528556" duration="776000" />
<workItem from="1586165870517" duration="3393000" />
<workItem from="1586513247633" duration="27000" />
<workItem from="1586513392882" duration="1756000" />
<workItem from="1586515200881" duration="616000" />
<workItem from="1586542251536" duration="2939000" />
<workItem from="1586890049867" duration="882000" />
<workItem from="1587576039420" duration="1514000" />
<workItem from="1587632952734" duration="8390000" />
<workItem from="1587722396734" duration="107000" />
<workItem from="1587723127206" duration="675000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="2" />
</component>
<component name="WindowStateProjectService">
<state x="762" y="164" key="#CLI_Interpreters" timestamp="1583790387641">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="762" y="164" key="#CLI_Interpreters/0.0.1920.1050@0.0.1920.1050" timestamp="1583790387641" />
<state x="1335" y="118" key="#PHP" timestamp="1583790338484">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="1335" y="118" key="#PHP/0.0.1920.1050@0.0.1920.1050" timestamp="1583790338484" />
<state x="1061" y="186" key="#SQL_Dialects" timestamp="1584476607694">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="1061" y="186" key="#SQL_Dialects/0.0.1920.1050@0.0.1920.1050" timestamp="1584476607694" />
<state x="1468" y="243" key="#com.intellij.ide.util.MemberChooser" timestamp="1583789923548">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="1468" y="243" key="#com.intellij.ide.util.MemberChooser/0.0.1920.1050@0.0.1920.1050" timestamp="1583789923548" />
<state x="1226" y="270" key="#com.intellij.openapi.updateSettings.impl.PluginUpdateInfoDialog" timestamp="1587722425322">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="1226" y="270" key="#com.intellij.openapi.updateSettings.impl.PluginUpdateInfoDialog/0.0.1920.1050@0.0.1920.1050" timestamp="1587722425322" />
<state x="901" y="197" key="#index.php" timestamp="1583790389133">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="901" y="197" key="#index.php/0.0.1920.1050@0.0.1920.1050" timestamp="1583790389133" />
<state x="962" y="264" key="FileChooserDialogImpl" timestamp="1585392407562">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="962" y="264" key="FileChooserDialogImpl/0.0.1920.1050@0.0.1920.1050" timestamp="1585392407562" />
<state width="1340" height="266" key="GridCell.Tab.0.bottom" timestamp="1583796211753">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state width="1340" height="266" key="GridCell.Tab.0.bottom/0.0.1920.1050@0.0.1920.1050" timestamp="1583796211753" />
<state width="1340" height="266" key="GridCell.Tab.0.center" timestamp="1583796211753">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state width="1340" height="266" key="GridCell.Tab.0.center/0.0.1920.1050@0.0.1920.1050" timestamp="1583796211753" />
<state width="1340" height="266" key="GridCell.Tab.0.left" timestamp="1583796211753">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state width="1340" height="266" key="GridCell.Tab.0.left/0.0.1920.1050@0.0.1920.1050" timestamp="1583796211753" />
<state width="1340" height="266" key="GridCell.Tab.0.right" timestamp="1583796211753">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state width="1340" height="266" key="GridCell.Tab.0.right/0.0.1920.1050@0.0.1920.1050" timestamp="1583796211753" />
<state x="1497" y="451" key="NewPhpFileDialog" timestamp="1587642034866">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="1497" y="451" key="NewPhpFileDialog/0.0.1920.1050@0.0.1920.1050" timestamp="1587642034866" />
<state x="922" y="170" key="SettingsEditor" timestamp="1586095862215">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="922" y="170" key="SettingsEditor/0.0.1920.1050@0.0.1920.1050" timestamp="1586095862215" />
<state x="907" y="358" key="com.intellij.ide.util.TipDialog" timestamp="1587722399404">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="907" y="358" key="com.intellij.ide.util.TipDialog/0.0.1920.1050@0.0.1920.1050" timestamp="1587722399404" />
<state x="1109" y="374" width="707" height="308" key="org.intellij.images.editor.actions.BackgroundImageDialog" timestamp="1583789800654">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="1109" y="374" width="707" height="308" key="org.intellij.images.editor.actions.BackgroundImageDialog/0.0.1920.1050@0.0.1920.1050" timestamp="1583789800654" />
<state x="874" y="254" width="672" height="678" key="search.everywhere.popup" timestamp="1586113142264">
<screen x="0" y="0" width="1920" height="1050" />
</state>
<state x="874" y="254" width="672" height="678" key="search.everywhere.popup/0.0.1920.1050@0.0.1920.1050" timestamp="1586113142264" />
</component>
</project>
{
"autoload": {
"classmap": [
"./"
]
}
}
<?php
return [
'database' => [
'dbname' => 'users;',
'username' => 'root',
'password' => '',
'connection' => 'mysql:host=127.0.0.1;dbname=',
'option' => [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]
]
];
<?php
/* So what we did here, is that we'll use the methods below to redirect to the desired page.
We deleted the index.php, contact.php, about.php in the Controllers folder.
*/
class PagesController{
public function home(){
//Receive the request
//Delegate
//Return a response.
return view('index');
}
public function contact(){
return view('contact');
}
public function about(){
$company = 'Laracasts';
return view('about',['company' => $company]);
}
}
<?php
class UsersController{
public function index(){
$users = App::get('database')->selectAll('users');
return view('users',compact('users'));
}
public function store(){
//ΚΑΛΕΙ ΤΗ ΜΕΘΟΔΟ ΚΑΙ ΠΑΡΑΘΕΤΕΙ ΣΤΗΝ ΒΔ ΤΙΣ ΤΙΜΕΣ ΤΗΣ ΦΟΡΜΑΣ
App::get('database')->insert('users',[
'firstname' => $_POST['firstname'],
'lastname' => $_POST['lastname']
]);
return redirect('users');
}
}
<?php
class App{
//it's protected static, so i can refer to it in the functions
protected static $registry = [];
//eisagei to key kai to value sto $registry pinaka. p.x 'config' => require 'config.php'
public static function bind($key, $value){
static::$registry[$key] = $value;
}
//epistrefei tin timi, opou zhth8ei
public static function get($key){
return static::$registry[$key];
}
}
<?php
// orizei sto 'config' (key) to arxeio config.php...... 'config' => require 'config.php'.. ti doyleia ayth thn kanei h bind sunartish
App::bind('config', require 'config.php');
/* $app = [];
$app['config'] = require 'config.php';
*/
App::bind('database', new QueryBuilder(
Connection::makeConnection(App::get('config')['database'])
));
function view($requestSite, $data = []){
extract($data);
return require "views/{$requestSite}.view.php";
}
function redirect($data){
header("Location: {$data}");
}
<?php
class Connection{
public static function makeConnection($database){
try{
return new PDO($database['connection'] . $database['dbname'],
$database['username'],
$database['password'],
$database['option']
);
}catch (PDOException $e){
die($e->getMessage());
}
}
}
<?php
class QueryBuilder{
protected $pdo;
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
public function insert($table,$parameters){
// array_keys($parameters); //returns only the keys, check add-name.php where 'description' => '$_POST['description]'
//implode(', ' $par_name) // seperates the values
// insert into %s (%s) values (:description, :completed)
// insert into table_name (column_name) values (:description, :completed)
$sql1 = sprintf(
'insert into %s (%s) values (%s)',
$table,
implode(', ', array_keys($parameters)),
':'.implode(', :',array_keys($parameters))
);
$statement = $this->pdo->prepare($sql1);
$statement->execute($parameters);
}
public function selectAll($table){
$statement = $this->pdo->prepare("select * from {$table}");
$statement->execute();
return $statement->fetchAll(PDO::FETCH_CLASS);
}
}
<?php
class Requests{
public static function uri(){
//Επειδή το url στο get είναι /names?names=John, πιάνει και τις ιδιότητες σαν url
//και δε μπορεί στα routes να βρεθεί το names. Οπότε με τον κωδικα παρακάτω
//παίρνει μόνο το /names και όχι τις ιδιότητες
return trim(parse_url(
$_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
}
public static function method(){
return $_SERVER['REQUEST_METHOD'];
}
}
<?php
class Router{
//Explicit for GET and POST
public $routes = [
'GET' => [],
'POST' => []
];
public static function load($file){
$router = new static;
require $file;
return $router;
}
/* πως λειτουργεί:
εστω $getRoutes[];
$getRoutes[] = 'foo';
τοτε το αποτελεσμα ειναι 0 => 'foo',
αν ομως $getRoutes['uri'] = 'bar';
τότε:
'uri' => 'bar'
Οποτε για τον κωδικα κατω, παίρνει το GET και το URI π.χ /names και ισουται με το
entry point του.
*/
public function get($uri, $controller){
$this->routes['GET'][$uri] = $controller;
}
public function post($uri, $controller){
$this->routes['POST'][$uri] = $controller;
}
public function direct($uri, $requestType){
if(array_key_exists($uri, $this->routes[$requestType])){ //koitaei ta types dld POST 'h GET
//die($this->routes[$requestType][$uri]); Edw mas epestrefe se String morfi to PagesController@home
//... <- this send the values converted from arrays to Strings in order to be used as values for the arguments in callAction method.
//explode method deletes the requested character in the ' '
return $this->callAction(
...explode('@', $this->routes[$requestType][$uri])
);
}
throw new Exception('No route defined for this URI.');
}
//It's protected since nobody out from this class needs to call it. So, it's a good method
protected function callAction($controller, $action){
$controller = new $controller;
if(! method_exists($controller, $action)){
throw new Exception(
"{$controller} controller does not respond to {$action} action."
);
}
return $controller->$action();
}
}
<?php
require "vendor/autoload.php";
require 'core/bootstrap.php';
$router = new Router();
Router::load('routes.php')
-> direct(Requests::uri(), Requests::method());
require "vendor/autoload.php";
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About Us</a></li>
<li><a href="/contact">Contact Us</a></li>
<li><a href="/users">Users</a></li>
</ul>
</nav>
<?php
/* ΠΡΙΝ ΤΗΝ ΑΛΛΑΓΗ (ΓΙΑ ΤΑ GET POST)
$router->define([
'' => 'controllers/index.php',
'about' => 'controllers/about.php',
'about/culture' => 'controllers/about-culture.php',
'contact' => 'controllers/contact.php',
'names' => 'controllers/add-name.php'
]); */
//We deleted the previous routes and we used the PagesController Class, connecting to the method using @<name_of_method>
$router->get('','PagesController@home');
$router->get('about','PagesController@about');
$router->get('contact', 'PagesController@contact');
$router->get('users', 'UsersController@index');
$router->post('users', 'UsersController@store');
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitd380aa67501138fa14d884bd63364ac8::getLoader();
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'App' => $baseDir . '/core/App.php',
'ComposerAutoloaderInitd380aa67501138fa14d884bd63364ac8' => $vendorDir . '/composer/autoload_real.php',
'Composer\\Autoload\\ClassLoader' => $vendorDir . '/composer/ClassLoader.php',
'Composer\\Autoload\\ComposerStaticInitd380aa67501138fa14d884bd63364ac8' => $vendorDir . '/composer/autoload_static.php',
'Connection' => $baseDir . '/core/database/Connection.php',
'PagesController' => $baseDir . '/controllers/PagesController.php',
'QueryBuilder' => $baseDir . '/core/database/QueryBuilder.php',
'Requests' => $baseDir . '/core/Requests.php',
'Router' => $baseDir . '/core/Router.php',
'UsersController' => $baseDir . '/controllers/UsersController.php',
);
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);
<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitd380aa67501138fa14d884bd63364ac8
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitd380aa67501138fa14d884bd63364ac8', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInitd380aa67501138fa14d884bd63364ac8', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitd380aa67501138fa14d884bd63364ac8::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
return $loader;
}
}
<?php
// autoload_static.php @generated by Composer
namespace Composer\Autoload;
class ComposerStaticInitd380aa67501138fa14d884bd63364ac8
{
public static $classMap = array (
'App' => __DIR__ . '/../..' . '/core/App.php',
'ComposerAutoloaderInitd380aa67501138fa14d884bd63364ac8' => __DIR__ . '/..' . '/composer/autoload_real.php',
'Composer\\Autoload\\ClassLoader' => __DIR__ . '/..' . '/composer/ClassLoader.php',
'Composer\\Autoload\\ComposerStaticInitd380aa67501138fa14d884bd63364ac8' => __DIR__ . '/..' . '/composer/autoload_static.php',
'Connection' => __DIR__ . '/../..' . '/core/database/Connection.php',
'PagesController' => __DIR__ . '/../..' . '/controllers/PagesController.php',
'QueryBuilder' => __DIR__ . '/../..' . '/core/database/QueryBuilder.php',
'Requests' => __DIR__ . '/../..' . '/core/Requests.php',
'Router' => __DIR__ . '/../..' . '/core/Router.php',
'UsersController' => __DIR__ . '/../..' . '/controllers/UsersController.php',
);
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->classMap = ComposerStaticInitd380aa67501138fa14d884bd63364ac8::$classMap;
}, null, ClassLoader::class);
}
}
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Autoload;
/**
* ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
*
* $loader = new \Composer\Autoload\ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* // activate the autoloader
* $loader->register();
*
* // to enable searching the include path (eg. for PEAR packages)
* $loader->setUseIncludePath(true);
*
* In this example, if you try to use a class in the Symfony\Component
* namespace or one of its children (Symfony\Component\Console for instance),
* the autoloader will first look for the class under the component/
* directory, and it will then fallback to the framework/ directory if not
* found before giving up.
*
* This class is loosely based on the Symfony UniversalClassLoader.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @see http://www.php-fig.org/psr/psr-0/
* @see http://www.php-fig.org/psr/psr-4/
*/
class ClassLoader
{
// PSR-4
private $prefixLengthsPsr4 = array();
private $prefixDirsPsr4 = array();
private $fallbackDirsPsr4 = array();
// PSR-0
private $prefixesPsr0 = array();
private $fallbackDirsPsr0 = array();
private $useIncludePath = false;
private $classMap = array();
private $classMapAuthoritative = false;
private $missingClasses = array();
private $apcuPrefix;
public function getPrefixes()
{
if (!empty($this->prefixesPsr0)) {
return call_user_func_array('array_merge', $this->prefixesPsr0);
}
return array();
}
public function getPrefixesPsr4()
{
return $this->prefixDirsPsr4;
}
public function getFallbackDirs()
{
return $this->fallbackDirsPsr0;
}
public function getFallbackDirsPsr4()
{
return $this->fallbackDirsPsr4;
}
public function getClassMap()
{
return $this->classMap;
}
/**
* @param array $classMap Class to filename map
*/
public function addClassMap(array $classMap)
{
if ($this->classMap) {
$this->classMap = array_merge($this->classMap, $classMap);
} else {
$this->classMap = $classMap;
}
}
/**
* Registers a set of PSR-0 directories for a given prefix, either
* appending or prepending to the ones previously set for this prefix.
*
* @param string $prefix The prefix
* @param array|string $paths The PSR-0 root directories
* @param bool $prepend Whether to prepend the directories
*/
public function add($prefix, $paths, $prepend = false)
{
if (!$prefix) {
if ($prepend) {
$this->fallbackDirsPsr0 = array_merge(
(array) $paths,
$this->fallbackDirsPsr0
);
} else {
$this->fallbackDirsPsr0 = array_merge(
$this->fallbackDirsPsr0,
(array) $paths
);
}
return;
}
$first = $prefix[0];
if (!isset($this->prefixesPsr0[$first][$prefix])) {
$this->prefixesPsr0[$first][$prefix] = (array) $paths;
return;
}
if ($prepend) {
$this->prefixesPsr0[$first][$prefix] = array_merge(
(array) $paths,
$this->prefixesPsr0[$first][$prefix]
);
} else {
$this->prefixesPsr0[$first][$prefix] = array_merge(
$this->prefixesPsr0[$first][$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-4 directories for a given namespace, either
* appending or prepending to the ones previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-4 base directories
* @param bool $prepend Whether to prepend the directories
*
* @throws \InvalidArgumentException
*/
public function addPsr4($prefix, $paths, $prepend = false)
{
if (!$prefix) {
// Register directories for the root namespace.
if ($prepend) {
$this->fallbackDirsPsr4 = array_merge(
(array) $paths,
$this->fallbackDirsPsr4
);
} else {
$this->fallbackDirsPsr4 = array_merge(
$this->fallbackDirsPsr4,
(array) $paths
);
}
} elseif (!isset($this->prefixDirsPsr4[$prefix])) {
// Register directories for a new namespace.
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
} elseif ($prepend) {
// Prepend directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
(array) $paths,
$this->prefixDirsPsr4[$prefix]
);
} else {
// Append directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
$this->prefixDirsPsr4[$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-0 directories for a given prefix,
* replacing any others previously set for this prefix.
*
* @param string $prefix The prefix
* @param array|string $paths The PSR-0 base directories
*/
public function set($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr0 = (array) $paths;
} else {
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
}
}
/**
* Registers a set of PSR-4 directories for a given namespace,
* replacing any others previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-4 base directories
*
* @throws \InvalidArgumentException
*/
public function setPsr4($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr4 = (array) $paths;
} else {
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
}
}
/**
* Turns on searching the include path for class files.
*
* @param bool $useIncludePath
*/
public function setUseIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}
/**
* Can be used to check if the autoloader uses the include path to check
* for classes.
*
* @return bool
*/
public function getUseIncludePath()
{
return $this->useIncludePath;
}
/**
* Turns off searching the prefix and fallback directories for classes
* that have not been registered with the class map.
*
* @param bool $classMapAuthoritative
*/
public function setClassMapAuthoritative($classMapAuthoritative)
{
$this->classMapAuthoritative = $classMapAuthoritative;
}
/**
* Should class lookup fail if not found in the current class map?
*
* @return bool
*/
public function isClassMapAuthoritative()
{
return $this->classMapAuthoritative;
}
/**
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
*
* @param string|null $apcuPrefix
*/
public function setApcuPrefix($apcuPrefix)
{
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
}
/**
* The APCu prefix in use, or null if APCu caching is not enabled.
*
* @return string|null
*/
public function getApcuPrefix()
{
return $this->apcuPrefix;
}
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return bool|null True if loaded, null otherwise
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|false The path if found, false otherwise
*/
public function findFile($class)
{
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
return false;
}
if (null !== $this->apcuPrefix) {
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
if ($hit) {
return $file;
}
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if (false === $file && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if (null !== $this->apcuPrefix) {
apcu_add($this->apcuPrefix.$class, $file);
}
if (false === $file) {
// Remember that this class does not exist.
$this->missingClasses[$class] = true;
}
return $file;
}
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath . '\\';
if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) {
if (file_exists($file = $dir . $pathEnd)) {
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
if (isset($this->prefixesPsr0[$first])) {
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
// PSR-0 include paths.
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
return $file;
}
return false;
}
}
/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*/
function includeFile($file)
{
include $file;
}
Copyright (c) Nils Adermann, Jordi Boggiano
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.
<?php require 'partials/head.php' ?>
<h1>About <?=$company?></h1>
<?php require 'partials/footer.php' ?>
<?php require 'partials/head.php' ?>
<h1>Contact Us</h1>
<?php require 'partials/footer.php' ?>
<?php require 'partials/head.php' ?>
<h1>Home Page</h1>
<?php require 'partials/footer.php' ?>
<?php require 'partials/head.php' ?>
<h1>Users Page</h1>
<?php foreach ($users as $user ) :?>
<li><?php echo $user->firstname; echo ' '; echo $user->lastname; ?> </li>
<?php endforeach; ?>
<h1>Submit your name</h1>
<form method="POST" action="/users">
<input name="firstname" />
<input name="lastname" />
<button type="submit">Submit</button>
</form>
<?php require 'partials/footer.php' ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment