Newwebshop/docs/API_DOCUMENTATION.md

661 lines
12 KiB
Markdown

# WEBSHOP SYSTEM - API DOKUMENTATION
## ÜBERBLICK
Das Webshop-System bietet eine vollständig PrestaShop-kompatible API mit erweiterten Funktionen für moderne E-Commerce-Anwendungen.
**Version:** 1.0.0
**Basis:** PrestaShop 8.x kompatibel
**Lizenz:** GPL v3
**Autor:** Webshop System
## 🚀 SCHNELLSTART
### Installation
```bash
# Repository klonen
git clone https://github.com/webshop-system/core.git
# Dependencies installieren
composer install
# Docker-Container starten
docker-compose up -d
# Datenbank initialisieren
php bin/console doctrine:database:create
php bin/console doctrine:migrations:migrate
```
### Erste Schritte
```php
// Context initialisieren
$context = Context::getContext();
// Produkt erstellen
$product = new Product();
$product->name = 'Mein Produkt';
$product->price = 29.99;
$product->add();
// Produkt abrufen
$product = new Product(1);
echo $product->name; // "Mein Produkt"
```
## 📚 CORE-KLASSEN
### Product.php
**Vollständige PrestaShop-kompatible Produktverwaltung**
#### Konstruktor
```php
$product = new Product($id = null, $id_lang = null, $id_shop = null);
```
#### Hauptmethoden
**Produkt erstellen:**
```php
$product = new Product();
$product->name = 'Produktname';
$product->reference = 'REF-001';
$product->price = 29.99;
$product->active = true;
$result = $product->add();
```
**Produkt abrufen:**
```php
$product = new Product(1);
echo $product->name;
echo $product->price;
```
**Produkt aktualisieren:**
```php
$product = new Product(1);
$product->price = 39.99;
$result = $product->update();
```
**Produkt löschen:**
```php
$product = new Product(1);
$result = $product->delete();
```
**Produkt suchen:**
```php
$products = Product::searchByName('Suchbegriff');
$product = Product::getByReference('REF-001');
```
**Preisberechnung:**
```php
$product = new Product(1);
$priceWithTax = $product->getPrice(true);
$priceWithoutTax = $product->getPrice(false);
```
**Lagerbestand:**
```php
$product = new Product(1);
$available = $product->checkQty(5);
$stock = $product->quantity;
```
#### Webservice-API
```php
// Alle Produkte abrufen
$products = $product->getWebserviceObjectList('', '', '', '');
// Produkt über Webservice erstellen
$wsProduct = [
'name' => 'Webservice Produkt',
'reference' => 'WS-001',
'price' => 19.99
];
```
### Category.php
**Kategorieverwaltung mit Hierarchie-Support**
#### Hauptmethoden
```php
// Kategorie erstellen
$category = new Category();
$category->name = 'Elektronik';
$category->active = true;
$category->add();
// Unterkategorie erstellen
$subCategory = new Category();
$subCategory->name = 'Smartphones';
$subCategory->id_parent = 1;
$subCategory->add();
// Kategoriehierarchie abrufen
$categories = Category::getCategories(1);
$children = $category->getChildren(1);
```
### ObjectModel.php
**Basis-Klasse für alle Modelle**
#### Hauptmethoden
```php
// Objekt erstellen
$object = new MyModel();
$object->add();
// Objekt abrufen
$object = new MyModel(1);
// Objekt aktualisieren
$object->update();
// Objekt löschen
$object->delete();
// Validierung
$isValid = $object->validateFields();
```
### Db.php
**Erweiterte Datenbankfunktionen**
#### Hauptmethoden
```php
// Query ausführen
$result = Db::getInstance()->executeS('SELECT * FROM product');
// Einzelnen Wert abrufen
$name = Db::getInstance()->getValue('SELECT name FROM product WHERE id = 1');
// Insert
$result = Db::getInstance()->insert('product', [
'name' => 'Test',
'price' => 29.99
]);
// Update
$result = Db::getInstance()->update('product', [
'price' => 39.99
], 'id = 1');
// Delete
$result = Db::getInstance()->delete('product', 'id = 1');
```
### Context.php
**Kontext-Management für Multi-Shop**
#### Hauptmethoden
```php
// Context abrufen
$context = Context::getContext();
// Shop-Informationen
$shop = $context->shop;
$language = $context->language;
$currency = $context->currency;
$customer = $context->customer;
$cart = $context->cart;
```
### Order.php
**Bestellverwaltung**
#### Hauptmethoden
```php
// Bestellung erstellen
$order = new Order();
$order->id_customer = 1;
$order->id_cart = 1;
$order->total_paid = 99.99;
$order->add();
// Bestellstatus aktualisieren
$order = new Order(1);
$order->setCurrentState(2); // 2 = Bezahlt
// Bestellhistorie
$history = $order->getHistory(1);
```
### Customer.php
**Kundenverwaltung**
#### Hauptmethoden
```php
// Kunde erstellen
$customer = new Customer();
$customer->firstname = 'Max';
$customer->lastname = 'Mustermann';
$customer->email = 'max@example.com';
$customer->add();
// Kunde abrufen
$customer = new Customer(1);
echo $customer->firstname;
// Kundenadressen
$addresses = $customer->getAddresses(1);
```
### Cart.php
**Warenkorb-Management**
#### Hauptmethoden
```php
// Warenkorb erstellen
$cart = new Cart();
$cart->id_customer = 1;
$cart->add();
// Produkt zum Warenkorb hinzufügen
$cart->updateQty(1, 2); // Produkt ID 1, Menge 2
// Warenkorb-Inhalt abrufen
$products = $cart->getProducts();
// Gesamtsumme
$total = $cart->getOrderTotal();
```
## 🔧 KONFIGURATION
### Configuration.php
**Zentrale Konfigurationsverwaltung**
```php
// Konfiguration setzen
Configuration::set('PS_SHOP_NAME', 'Mein Webshop');
Configuration::set('PS_SHOP_EMAIL', 'info@meinwebshop.de');
// Konfiguration abrufen
$shopName = Configuration::get('PS_SHOP_NAME');
$shopEmail = Configuration::get('PS_SHOP_EMAIL');
// Globale Konfiguration
Configuration::updateGlobalValue('PS_MAINTENANCE_MODE', false);
// Multi-Shop Konfiguration
Configuration::set('PS_SHOP_NAME', 'Shop Name', 1, 1);
```
### Language.php
**Sprachverwaltung**
```php
// Sprachen abrufen
$languages = Language::getLanguages();
$activeLanguages = Language::getLanguages(true);
// Sprache nach ISO-Code
$language = Language::getIdByIso('de');
$language = Language::getIdByLocale('de_DE');
// Sprache installieren
Language::checkAndAddLanguage('fr', true);
```
### Shop.php
**Multi-Shop Management**
```php
// Shops abrufen
$shops = Shop::getShops();
$activeShops = Shop::getShops(true);
// Context setzen
Shop::setContext(Shop::CONTEXT_SHOP, 1);
Shop::setContext(Shop::CONTEXT_GROUP, 1);
Shop::setContext(Shop::CONTEXT_ALL);
// Aktueller Shop
$currentShop = Shop::getContextShopID();
```
## 🌐 WEBSERVICE-API
### REST-API Endpoints
**Produkte:**
```
GET /api/products
GET /api/products/{id}
POST /api/products
PUT /api/products/{id}
DELETE /api/products/{id}
```
**Kategorien:**
```
GET /api/categories
GET /api/categories/{id}
POST /api/categories
PUT /api/categories/{id}
DELETE /api/categories/{id}
```
**Bestellungen:**
```
GET /api/orders
GET /api/orders/{id}
POST /api/orders
PUT /api/orders/{id}
DELETE /api/orders/{id}
```
**Kunden:**
```
GET /api/customers
GET /api/customers/{id}
POST /api/customers
PUT /api/customers/{id}
DELETE /api/customers/{id}
```
### API-Authentifizierung
```php
// API-Key generieren
$apiKey = Tools::generateApiKey();
// API-Zugriff
$headers = [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
];
```
## 🐳 DOCKER-DEPLOYMENT
### Docker-Compose Setup
```yaml
version: '3.8'
services:
php:
build: ./docker/php
volumes:
- .:/var/www/html
depends_on:
- mysql
- redis
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: webshop
volumes:
- mysql_data:/var/lib/mysql
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- .:/var/www/html
- ./docker/nginx:/etc/nginx/conf.d
depends_on:
- php
redis:
image: redis:alpine
ports:
- "6379:6379"
```
### Deployment-Skript
```bash
#!/bin/bash
# deploy.sh
echo "🚀 Deploying Webshop System..."
# Docker-Container stoppen
docker-compose down
# Neueste Version pullen
git pull origin main
# Dependencies installieren
composer install --no-dev --optimize-autoloader
# Docker-Container starten
docker-compose up -d
# Datenbank-Migrationen
docker-compose exec php php bin/console doctrine:migrations:migrate
# Cache leeren
docker-compose exec php php bin/console cache:clear
echo "✅ Deployment abgeschlossen!"
```
## 📊 PERFORMANCE-OPTIMIERUNG
### Caching-Strategien
```php
// Redis-Cache konfigurieren
Cache::setRedisConnection([
'host' => 'redis',
'port' => 6379,
'database' => 0
]);
// Produkt-Cache
$product = Cache::retrieve('product_1');
if (!$product) {
$product = new Product(1);
Cache::store('product_1', $product, 3600);
}
```
### Datenbank-Optimierung
```sql
-- Indizes für bessere Performance
CREATE INDEX idx_product_active ON product(active);
CREATE INDEX idx_product_category ON product(id_category_default);
CREATE INDEX idx_product_price ON product(price);
CREATE INDEX idx_order_customer ON `order`(id_customer);
CREATE INDEX idx_cart_customer ON cart(id_customer);
```
### Monitoring
```php
// Performance-Monitoring
$startTime = microtime(true);
$product = new Product(1);
$endTime = microtime(true);
$executionTime = $endTime - $startTime;
Logger::log('Product load time: ' . $executionTime . 's');
```
## 🔒 SICHERHEIT
### Validierung
```php
// Input-Validierung
$name = Tools::safeOutput($_POST['name']);
$email = Validate::isEmail($_POST['email']);
$price = Validate::isPrice($_POST['price']);
if (!$email) {
throw new Exception('Ungültige E-Mail-Adresse');
}
```
### SQL-Injection-Schutz
```php
// Sichere Queries
$sql = 'SELECT * FROM product WHERE id = ' . (int)$id;
$sql = 'SELECT * FROM product WHERE name = \'' . pSQL($name) . '\'';
```
### XSS-Schutz
```php
// Output-Escaping
echo Tools::safeOutput($userInput);
echo htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');
```
## 🧪 TESTING
### Unit-Tests ausführen
```bash
# Alle Tests
vendor/bin/phpunit
# Spezifische Test-Klasse
vendor/bin/phpunit tests/Unit/ProductTest.php
# Mit Coverage-Report
vendor/bin/phpunit --coverage-html coverage/
```
### Integration-Tests
```php
// Beispiel Integration-Test
class OrderIntegrationTest extends TestCase
{
public function testCompleteOrderFlow()
{
// Kunde erstellen
$customer = new Customer();
$customer->firstname = 'Test';
$customer->lastname = 'Customer';
$customer->email = 'test@example.com';
$customer->add();
// Warenkorb erstellen
$cart = new Cart();
$cart->id_customer = $customer->id;
$cart->add();
// Produkt zum Warenkorb hinzufügen
$cart->updateQty(1, 2);
// Bestellung erstellen
$order = new Order();
$order->id_customer = $customer->id;
$order->id_cart = $cart->id;
$order->total_paid = 99.99;
$order->add();
$this->assertGreaterThan(0, $order->id);
}
}
```
## 📈 MONITORING & LOGGING
### Logging-Konfiguration
```php
// Logger konfigurieren
Logger::setLogLevel(Logger::INFO);
Logger::log('Application started', Logger::INFO);
Logger::log('Error occurred', Logger::ERROR);
```
### Performance-Monitoring
```php
// Response-Zeit messen
$startTime = microtime(true);
// ... Code ausführen ...
$endTime = microtime(true);
$responseTime = $endTime - $startTime;
if ($responseTime > 1.0) {
Logger::log('Slow response: ' . $responseTime . 's', Logger::WARNING);
}
```
## 🚀 DEPLOYMENT-CHECKLISTE
### Vor dem Deployment
- [ ] Alle Tests erfolgreich
- [ ] Code-Review abgeschlossen
- [ ] Datenbank-Backup erstellt
- [ ] Umgebungsvariablen konfiguriert
- [ ] SSL-Zertifikate installiert
### Nach dem Deployment
- [ ] Anwendung erreichbar
- [ ] Datenbank-Verbindung funktioniert
- [ ] Cache funktioniert
- [ ] Logs werden geschrieben
- [ ] Monitoring aktiviert
## 📞 SUPPORT
### Kontakt
- **E-Mail:** support@webshop-system.de
- **Dokumentation:** https://docs.webshop-system.de
- **GitHub:** https://github.com/webshop-system/core
### Häufige Probleme
**Problem:** Datenbank-Verbindung fehlschlägt
```bash
# Lösung: Docker-Container neu starten
docker-compose restart mysql
```
**Problem:** Cache-Probleme
```bash
# Lösung: Cache leeren
php bin/console cache:clear
```
**Problem:** Performance-Probleme
```bash
# Lösung: OpCache aktivieren
docker-compose exec php php -d opcache.enable=1
```
---
**© 2024 Webshop System - Vollständig PrestaShop-kompatibel**