137 lines
4.2 KiB
PHP
Executable File
137 lines
4.2 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* FuzeWorks Forms Library
|
|
*
|
|
* The FuzeWorks PHP FrameWork
|
|
*
|
|
* Copyright (C) 2013-${YEAR} i15
|
|
*
|
|
* 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.
|
|
*
|
|
* @author i15
|
|
* @copyright Copyright (c) 2013 - ${YEAR}, i15. (http://i15.nl)
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
*
|
|
* @link https://i15.nl
|
|
* @since Version 1.3.0
|
|
*
|
|
* @version Version 1.3.0
|
|
*/
|
|
|
|
namespace FuzeWorks\Forms;
|
|
|
|
use FuzeWorks\Factory;
|
|
use FuzeWorks\iLibrary;
|
|
use FuzeWorks\Layout;
|
|
use FuzeWorks\Logger;
|
|
use FuzeWorks\ObjectStorage\ObjectStorageCache;
|
|
use FuzeWorks\ObjectStorage\ObjectStorageComponent;
|
|
use FuzeWorks\Priority;
|
|
use FuzeWorks\Security;
|
|
|
|
class Forms implements iLibrary
|
|
{
|
|
protected string $libraryPath;
|
|
|
|
public function __construct()
|
|
{
|
|
// Set path
|
|
$this->libraryPath = dirname(__DIR__, 3);
|
|
|
|
// Add layout component path
|
|
/** @var Layout $layouts */
|
|
$layouts = Factory::getInstance('layouts');
|
|
$layouts->addComponentPath($this->libraryPath . DS . 'layouts', Priority::LOWEST);
|
|
}
|
|
|
|
/**
|
|
* Retrieve a new Form object with the given $name and $label.
|
|
*
|
|
* @param string $name
|
|
* @param string $label
|
|
* @return Form
|
|
*/
|
|
public function getForm(string $name, string $label = ""): Form
|
|
{
|
|
return new Form($name, $label);
|
|
}
|
|
|
|
/**
|
|
* Build and retrieve a Form object, and save the result into Cache to save time on next instantiation.
|
|
*
|
|
* The $callable must return a Form object.
|
|
*
|
|
* $name and $label will be passed to the Form constructor.
|
|
*
|
|
* @param callable $callable
|
|
* @param string $name
|
|
* @param string $label
|
|
* @return Form
|
|
*/
|
|
public function getCachedForm(callable $callable, string $name, string $label = ""): Form
|
|
{
|
|
// Fetch storage component
|
|
/** @var ObjectStorageComponent $storage */
|
|
$storage = Factory::getInstance('storage');
|
|
|
|
// Fetch object storage
|
|
/** @var ObjectStorageCache $cache */
|
|
$cache = $storage->getCache();
|
|
|
|
// Find form in storage
|
|
$key = "formStorage" . $name;
|
|
if ($cache->has($key))
|
|
{
|
|
Logger::log("Returning cached Form '".$name."'");
|
|
|
|
/** @var Form $form */
|
|
$form = $cache->get($key);
|
|
$csrf = $form->getCsrfField();
|
|
if (!is_null($csrf))
|
|
{
|
|
/** @var Security $security */
|
|
$security = Factory::getInstance("security");
|
|
$hash = $security->get_csrf_hash();
|
|
$csrf->setValue($hash);
|
|
}
|
|
|
|
return $form;
|
|
}
|
|
|
|
// Otherwise, create the form
|
|
Logger::log("Generating new Form '".$name."'");
|
|
$form = call_user_func($callable, $this->getForm($name, $label));
|
|
|
|
// Save the form
|
|
$cache->set($key, $form, 3600);
|
|
|
|
return $form;
|
|
}
|
|
|
|
|
|
public function getClassesPrefix(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function getSourceDirectory(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
} |