This commit is contained in:
Manish Verma
2016-12-13 18:18:25 +05:30
parent fc98add11c
commit 2d8e640e9b
2314 changed files with 97798 additions and 75664 deletions

View File

@@ -0,0 +1,110 @@
<?php
namespace Collective\Html;
use BadMethodCallException;
use Illuminate\Support\HtmlString;
trait Componentable
{
/**
* The registered components.
*
* @var array
*/
protected static $components = [];
/**
* Register a custom component.
*
* @param $name
* @param $view
* @param array $signature
*
* @return void
*/
public static function component($name, $view, array $signature)
{
static::$components[$name] = compact('view', 'signature');
}
/**
* Check if a component is registered.
*
* @param $name
*
* @return bool
*/
public static function hasComponent($name)
{
return isset(static::$components[$name]);
}
/**
* Render a custom component.
*
* @param $name
* @param array $arguments
*
* @return \Illuminate\Contracts\View\View
*/
protected function renderComponent($name, array $arguments)
{
$component = static::$components[$name];
$data = $this->getComponentData($component['signature'], $arguments);
return new HtmlString(
$this->view->make($component['view'], $data)->render()
);
}
/**
* Prepare the component data, while respecting provided defaults.
*
* @param array $signature
* @param array $arguments
*
* @return array
*/
protected function getComponentData(array $signature, array $arguments)
{
$data = [];
$i = 0;
foreach ($signature as $variable => $default) {
// If the "variable" value is actually a numeric key, we can assume that
// no default had been specified for the component argument and we'll
// just use null instead, so that we can treat them all the same.
if (is_numeric($variable)) {
$variable = $default;
$default = null;
}
$data[$variable] = array_get($arguments, $i, $default);
$i++;
}
return $data;
}
/**
* Dynamically handle calls to the class.
*
* @param string $method
* @param array $parameters
*
* @return \Illuminate\Contracts\View\View|mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
if (static::hasComponent($method)) {
return $this->renderComponent($method, $parameters);
}
throw new BadMethodCallException("Method {$method} does not exist.");
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace Collective\Html\Eloquent;
use ReflectionClass;
use ReflectionMethod;
use Illuminate\Support\Str;
trait FormAccessible
{
/**
* A cached ReflectionClass instance for $this
*
* @var ReflectionClass
*/
protected $reflection;
/**
* @param string $key
*
* @return mixed
*/
public function getFormValue($key)
{
$value = $this->getAttributeFromArray($key);
// If the attribute is listed as a date, we will convert it to a DateTime
// instance on retrieval, which makes it quite convenient to work with
// date fields without having to create a mutator for each property.
if (in_array($key, $this->getDates())) {
if (! is_null($value)) {
$value = $this->asDateTime($value);
}
}
// If the attribute has a get mutator, we will call that then return what
// it returns as the value, which is useful for transforming values on
// retrieval from the model to a form that is more useful for usage.
if ($this->hasFormMutator($key)) {
return $this->mutateFormAttribute($key, $value);
}
return $value;
}
/**
* @param $key
*
* @return bool
*/
protected function hasFormMutator($key)
{
$methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC);
$mutator = collect($methods)
->first(function ($index, ReflectionMethod $method) use ($key) {
return $method->getName() == 'form' . Str::studly($key) . 'Attribute';
});
return (bool) $mutator;
}
/**
* @param $key
* @param $value
*
* @return mixed
*/
private function mutateFormAttribute($key, $value)
{
return $this->{'form' . Str::studly($key) . 'Attribute'}($value);
}
/**
* Get a ReflectionClass Instance
* @return ReflectionClass
*/
protected function getReflection()
{
if (! $this->reflection) {
$this->reflection = new ReflectionClass($this);
}
return $this->reflection;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,22 @@
<?php
namespace Collective\Html;
use Illuminate\Support\Facades\Facade;
/**
* @see \Collective\Html\FormBuilder
*/
class FormFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'form';
}
}

View File

@@ -0,0 +1,542 @@
<?php
namespace Collective\Html;
use BadMethodCallException;
use Illuminate\Support\HtmlString;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Routing\UrlGenerator;
class HtmlBuilder
{
use Macroable, Componentable {
Macroable::__call as macroCall;
Componentable::__call as componentCall;
}
/**
* The URL generator instance.
*
* @var \Illuminate\Contracts\Routing\UrlGenerator
*/
protected $url;
/**
* The View Factory instance.
*
* @var \Illuminate\Contracts\View\Factory
*/
protected $view;
/**
* Create a new HTML builder instance.
*
* @param \Illuminate\Contracts\Routing\UrlGenerator $url
* @param \Illuminate\Contracts\View\Factory $view
*/
public function __construct(UrlGenerator $url = null, Factory $view)
{
$this->url = $url;
$this->view = $view;
}
/**
* Convert an HTML string to entities.
*
* @param string $value
*
* @return string
*/
public function entities($value)
{
return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
}
/**
* Convert entities to HTML characters.
*
* @param string $value
*
* @return string
*/
public function decode($value)
{
return html_entity_decode($value, ENT_QUOTES, 'UTF-8');
}
/**
* Generate a link to a JavaScript file.
*
* @param string $url
* @param array $attributes
* @param bool $secure
*
* @return \Illuminate\Support\HtmlString
*/
public function script($url, $attributes = [], $secure = null)
{
$attributes['src'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<script' . $this->attributes($attributes) . '></script>' . PHP_EOL);
}
/**
* Generate a link to a CSS file.
*
* @param string $url
* @param array $attributes
* @param bool $secure
*
* @return \Illuminate\Support\HtmlString
*/
public function style($url, $attributes = [], $secure = null)
{
$defaults = ['media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet'];
$attributes = $attributes + $defaults;
$attributes['href'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>' . PHP_EOL);
}
/**
* Generate an HTML image element.
*
* @param string $url
* @param string $alt
* @param array $attributes
* @param bool $secure
*
* @return \Illuminate\Support\HtmlString
*/
public function image($url, $alt = null, $attributes = [], $secure = null)
{
$attributes['alt'] = $alt;
return $this->toHtmlString('<img src="' . $this->url->asset($url,
$secure) . '"' . $this->attributes($attributes) . '>');
}
/**
* Generate a link to a Favicon file.
*
* @param string $url
* @param array $attributes
* @param bool $secure
*
* @return \Illuminate\Support\HtmlString
*/
public function favicon($url, $attributes = [], $secure = null)
{
$defaults = ['rel' => 'shortcut icon', 'type' => 'image/x-icon'];
$attributes = $attributes + $defaults;
$attributes['href'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>' . PHP_EOL);
}
/**
* Generate a HTML link.
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $secure
*
* @return \Illuminate\Support\HtmlString
*/
public function link($url, $title = null, $attributes = [], $secure = null)
{
$url = $this->url->to($url, [], $secure);
if (is_null($title) || $title === false) {
$title = $url;
}
return $this->toHtmlString('<a href="' . $url . '"' . $this->attributes($attributes) . '>' . $this->entities($title) . '</a>');
}
/**
* Generate a HTTPS HTML link.
*
* @param string $url
* @param string $title
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function secureLink($url, $title = null, $attributes = [])
{
return $this->link($url, $title, $attributes, true);
}
/**
* Generate a HTML link to an asset.
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $secure
*
* @return \Illuminate\Support\HtmlString
*/
public function linkAsset($url, $title = null, $attributes = [], $secure = null)
{
$url = $this->url->asset($url, $secure);
return $this->link($url, $title ?: $url, $attributes, $secure);
}
/**
* Generate a HTTPS HTML link to an asset.
*
* @param string $url
* @param string $title
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function linkSecureAsset($url, $title = null, $attributes = [])
{
return $this->linkAsset($url, $title, $attributes, true);
}
/**
* Generate a HTML link to a named route.
*
* @param string $name
* @param string $title
* @param array $parameters
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function linkRoute($name, $title = null, $parameters = [], $attributes = [])
{
return $this->link($this->url->route($name, $parameters), $title, $attributes);
}
/**
* Generate a HTML link to a controller action.
*
* @param string $action
* @param string $title
* @param array $parameters
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function linkAction($action, $title = null, $parameters = [], $attributes = [])
{
return $this->link($this->url->action($action, $parameters), $title, $attributes);
}
/**
* Generate a HTML link to an email address.
*
* @param string $email
* @param string $title
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function mailto($email, $title = null, $attributes = [])
{
$email = $this->email($email);
$title = $title ?: $email;
$email = $this->obfuscate('mailto:') . $email;
return $this->toHtmlString('<a href="' . $email . '"' . $this->attributes($attributes) . '>' . $this->entities($title) . '</a>');
}
/**
* Obfuscate an e-mail address to prevent spam-bots from sniffing it.
*
* @param string $email
*
* @return string
*/
public function email($email)
{
return str_replace('@', '&#64;', $this->obfuscate($email));
}
/**
* Generate an ordered list of items.
*
* @param array $list
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString|string
*/
public function ol($list, $attributes = [])
{
return $this->listing('ol', $list, $attributes);
}
/**
* Generate an un-ordered list of items.
*
* @param array $list
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString|string
*/
public function ul($list, $attributes = [])
{
return $this->listing('ul', $list, $attributes);
}
/**
* Generate a description list of items.
*
* @param array $list
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function dl(array $list, array $attributes = [])
{
$attributes = $this->attributes($attributes);
$html = "<dl{$attributes}>";
foreach ($list as $key => $value) {
$value = (array) $value;
$html .= "<dt>$key</dt>";
foreach ($value as $v_key => $v_value) {
$html .= "<dd>$v_value</dd>";
}
}
$html .= '</dl>';
return $this->toHtmlString($html);
}
/**
* Create a listing HTML element.
*
* @param string $type
* @param array $list
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString|string
*/
protected function listing($type, $list, $attributes = [])
{
$html = '';
if (count($list) == 0) {
return $html;
}
// Essentially we will just spin through the list and build the list of the HTML
// elements from the array. We will also handled nested lists in case that is
// present in the array. Then we will build out the final listing elements.
foreach ($list as $key => $value) {
$html .= $this->listingElement($key, $type, $value);
}
$attributes = $this->attributes($attributes);
return $this->toHtmlString("<{$type}{$attributes}>{$html}</{$type}>");
}
/**
* Create the HTML for a listing element.
*
* @param mixed $key
* @param string $type
* @param mixed $value
*
* @return string
*/
protected function listingElement($key, $type, $value)
{
if (is_array($value)) {
return $this->nestedListing($key, $type, $value);
} else {
return '<li>' . e($value) . '</li>';
}
}
/**
* Create the HTML for a nested listing attribute.
*
* @param mixed $key
* @param string $type
* @param mixed $value
*
* @return string
*/
protected function nestedListing($key, $type, $value)
{
if (is_int($key)) {
return $this->listing($type, $value);
} else {
return '<li>' . $key . $this->listing($type, $value) . '</li>';
}
}
/**
* Build an HTML attribute string from an array.
*
* @param array $attributes
*
* @return string
*/
public function attributes($attributes)
{
$html = [];
foreach ((array) $attributes as $key => $value) {
$element = $this->attributeElement($key, $value);
if (! is_null($element)) {
$html[] = $element;
}
}
return count($html) > 0 ? ' ' . implode(' ', $html) : '';
}
/**
* Build a single attribute element.
*
* @param string $key
* @param string $value
*
* @return string
*/
protected function attributeElement($key, $value)
{
// For numeric keys we will assume that the key and the value are the same
// as this will convert HTML attributes such as "required" to a correct
// form like required="required" instead of using incorrect numerics.
if (is_numeric($key)) {
$key = $value;
}
if (! is_null($value)) {
return $key . '="' . e($value) . '"';
}
}
/**
* Obfuscate a string to prevent spam-bots from sniffing it.
*
* @param string $value
*
* @return string
*/
public function obfuscate($value)
{
$safe = '';
foreach (str_split($value) as $letter) {
if (ord($letter) > 128) {
return $letter;
}
// To properly obfuscate the value, we will randomly convert each letter to
// its entity or hexadecimal representation, keeping a bot from sniffing
// the randomly obfuscated letters out of the string on the responses.
switch (rand(1, 3)) {
case 1:
$safe .= '&#' . ord($letter) . ';';
break;
case 2:
$safe .= '&#x' . dechex(ord($letter)) . ';';
break;
case 3:
$safe .= $letter;
}
}
return $safe;
}
/**
* Generate a meta tag.
*
* @param string $name
* @param string $content
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function meta($name, $content, array $attributes = [])
{
$defaults = compact('name', 'content');
$attributes = array_merge($defaults, $attributes);
return $this->toHtmlString('<meta' . $this->attributes($attributes) . '>' . PHP_EOL);
}
/**
* Generate an html tag.
*
* @param string $tag
* @param mixed $content
* @param array $attributes
*
* @return \Illuminate\Support\HtmlString
*/
public function tag($tag, $content, array $attributes = [])
{
$content = is_array($content) ? implode(PHP_EOL, $content) : $content;
return $this->toHtmlString('<' . $tag . $this->attributes($attributes) . '>' . PHP_EOL . $this->toHtmlString($content) . PHP_EOL . '</' . $tag . '>' . PHP_EOL);
}
/**
* Transform the string to an Html serializable object
*
* @param $html
*
* @return \Illuminate\Support\HtmlString
*/
protected function toHtmlString($html)
{
return new HtmlString($html);
}
/**
* Dynamically handle calls to the class.
*
* @param string $method
* @param array $parameters
*
* @return \Illuminate\Contracts\View\View|mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
try {
return $this->componentCall($method, $parameters);
} catch (BadMethodCallException $e) {
//
}
try {
return $this->macroCall($method, $parameters);
} catch (BadMethodCallException $e) {
//
}
throw new BadMethodCallException("Method {$method} does not exist.");
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Collective\Html;
use Illuminate\Support\Facades\Facade;
/**
* @see \Collective\Html\HtmlBuilder
*/
class HtmlFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'html';
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Collective\Html;
use Illuminate\Support\ServiceProvider;
class HtmlServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerHtmlBuilder();
$this->registerFormBuilder();
$this->app->alias('html', 'Collective\Html\HtmlBuilder');
$this->app->alias('form', 'Collective\Html\FormBuilder');
}
/**
* Register the HTML builder instance.
*
* @return void
*/
protected function registerHtmlBuilder()
{
$this->app->singleton('html', function ($app) {
return new HtmlBuilder($app['url'], $app['view']);
});
}
/**
* Register the form builder instance.
*
* @return void
*/
protected function registerFormBuilder()
{
$this->app->singleton('form', function ($app) {
$form = new FormBuilder($app['html'], $app['url'], $app['view'], $app['session.store']->getToken());
return $form->setSessionStore($app['session.store']);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['html', 'form', 'Collective\Html\HtmlBuilder', 'Collective\Html\FormBuilder'];
}
}

View File

@@ -0,0 +1,69 @@
<?php
if (! function_exists('link_to')) {
/**
* Generate a HTML link.
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $secure
*
* @return string
*/
function link_to($url, $title = null, $attributes = [], $secure = null)
{
return app('html')->link($url, $title, $attributes, $secure);
}
}
if (! function_exists('link_to_asset')) {
/**
* Generate a HTML link to an asset.
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $secure
*
* @return string
*/
function link_to_asset($url, $title = null, $attributes = [], $secure = null)
{
return app('html')->linkAsset($url, $title, $attributes, $secure);
}
}
if (! function_exists('link_to_route')) {
/**
* Generate a HTML link to a named route.
*
* @param string $name
* @param string $title
* @param array $parameters
* @param array $attributes
*
* @return string
*/
function link_to_route($name, $title = null, $parameters = [], $attributes = [])
{
return app('html')->linkRoute($name, $title, $parameters, $attributes);
}
}
if (! function_exists('link_to_action')) {
/**
* Generate a HTML link to a controller action.
*
* @param string $action
* @param string $title
* @param array $parameters
* @param array $attributes
*
* @return string
*/
function link_to_action($action, $title = null, $parameters = [], $attributes = [])
{
return app('html')->linkAction($action, $title, $parameters, $attributes);
}
}