Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pepakriz committed Apr 11, 2014
0 parents commit fd0be43
Show file tree
Hide file tree
Showing 14 changed files with 593 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
@@ -0,0 +1,5 @@
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
tests/ export-ignore
*.sh eol=lf
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
vendor
composer.lock
11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
language: php

php:
- 5.4
- 5.5

before_script:
- composer self-update
- composer install --no-interaction --dev

script: VERBOSE=true vendor/bin/tester tests/
46 changes: 46 additions & 0 deletions composer.json
@@ -0,0 +1,46 @@
{
"name": "venne/widgets",
"description": "Venne widgets component",
"keywords": ["nette", "venne", "widgets"],
"homepage": "http://venne.cz",
"license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"],
"authors": [
{
"name": "Josef Kříž",
"homepage": "http://josef-kriz.cz"
}
],
"support": {
"email": "info@venne.cz",
"issues": "https://github.com/Venne/Forms/issues"
},
"require": {
"php": ">=5.4.0",
"nette/nette": "~2.1.0"
},
"require-dev": {
"nette/tester": "~1.1.0"
},
"autoload": {
"psr-4": {
"Venne\\Widgets\\": "src/Widgets/"
}
},
"autoload-dev": {
"psr-4": {
"VenneTests\\": "tests/src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
},
"venne": {
"configuration": {
"extensions": {
"widgets": "Venne\\Widgets\\DI\\WidgetsExtension"
}
}
}
}
}
55 changes: 55 additions & 0 deletions license.md
@@ -0,0 +1,55 @@
Licenses
========

Good news! You may use Venne:CMS under the terms of either
the New BSD License or the GNU General Public License (GPL) version 2 or 3.

The BSD License is recommended for most projects. It is easy to understand and it
places almost no restrictions on what you can do with the framework. If the GPL
fits better to your project, you can use the framework under this license.

You don't have to notify anyone which license you are using. You can freely
use Venne:CMS in commercial projects as long as the copyright header
remains intact.


New BSD License
---------------

Copyright (c) 2013 Josef Kříž (http://josef-kriz.cz)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of "Venne:CMS" nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright owner or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.


GNU General Public License
--------------------------

GPL licenses are very very long, so instead of including them here we offer
you URLs with full text:

- [GPL version 2](http://www.gnu.org/licenses/gpl-2.0.html)
- [GPL version 3](http://www.gnu.org/licenses/gpl-3.0.html)
9 changes: 9 additions & 0 deletions readme.md
@@ -0,0 +1,9 @@
# Venne:Widgets [![Build Status](https://secure.travis-ci.org/Venne/widgets.png)](http://travis-ci.org/Venne/widgets)

## Installation

The best way to install Venne/Widgets is using Composer:

```sh
composer require venne/widgets:@dev
```
122 changes: 122 additions & 0 deletions src/Widgets/WidgetManager.php
@@ -0,0 +1,122 @@
<?php

/**
* This file is part of the Venne:CMS (https://github.com/Venne)
*
* Copyright (c) 2011, 2012 Josef Kříž (http://www.josef-kriz.cz)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/

namespace Venne\Widgets;

use Nette\Application\UI\Control;
use Nette\DI\Container;
use Nette\InvalidArgumentException;
use Nette\InvalidStateException;
use Nette\Object;

/**
* @author Josef Kříž <pepakriz@gmail.com>
*/
class WidgetManager extends Object
{

/** @var Container */
private $container;

/** @var Callback[] */
private $widgets = array();


/**
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}


/**
* @param $name
* @param $factory
* @return $this
* @throws \Nette\InvalidArgumentException
*/
public function addWidget($name, $factory)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Name of widget must be string');
}

if (!is_string($factory) && !method_exists($factory, 'create') && !is_callable($factory)) {
throw new InvalidArgumentException('Second argument must be string or factory or callable');
}

if (is_string($factory) && !$this->container->hasService($factory)) {
throw new InvalidArgumentException("Service '$factory' does not exist");
}

$this->widgets[$name] = $factory;
return $this;
}


/**
* @param string $name
* @return bool
*/
public function hasWidget($name)
{
return isset($this->widgets[$name]);
}


/**
* @return \Callback[]
*/
public function getWidgets()
{
return $this->widgets;
}


/**
* @param $name
* @return mixed
* @throws \Nette\InvalidStateException
* @throws \Nette\InvalidArgumentException
*/
public function getWidget($name)
{
if (!$this->hasWidget($name)) {
throw new InvalidArgumentException("Widget $name does not exists");
}

$factory = $this->widgets[$name];

if (is_callable($factory)) {
$widget = $factory();
} else {
if (is_string($factory)) {
$factory = $this->container->getService($factory);

if (!method_exists($factory, 'create')) {
throw new InvalidStateException('Service is not factory.');
}
}

$widget = $factory->create();
}

if (!$widget instanceof Control) {
throw new InvalidStateException("Widget is not instance of 'Nette\Application\UI\Control'.");
}

return $widget;
}

}

62 changes: 62 additions & 0 deletions src/Widgets/WidgetsControlTrait.php
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of the Venne:CMS (https://github.com/Venne)
*
* Copyright (c) 2011, 2012 Josef Kříž (http://www.josef-kriz.cz)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/

namespace Venne\Widgets;

use Nette\InvalidArgumentException;

/**
* @author Josef Kříž <pepakriz@gmail.com>
*/
trait WidgetsControlTrait
{

/** @var WidgetManager|NULL */
private $widgetManager;


/**
* @param WidgetManager $widgetManager
*/
public function injectWidgetManager(WidgetManager $widgetManager)
{
$this->widgetManager = $widgetManager;
}


/**
* @return WidgetManager|NULL
*/
public function getWidgetManager()
{
return $this->widgetManager;
}


/**
* @param $name
* @return \Nette\Application\UI\Control
* @throws \Nette\InvalidArgumentException
*/
protected function createComponent($name)
{
if (($control = parent::createComponent($name)) == TRUE) {
return $control;
}

if ($this->widgetManager && $this->widgetManager->hasWidget($name)) {
return $this->widgetManager->getWidget($name);
}

throw new InvalidArgumentException("Component or widget with name '$name' does not exist.");
}

}
45 changes: 45 additions & 0 deletions src/Widgets/WidgetsExtension.php
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of the Venne:CMS (https://github.com/Venne)
*
* Copyright (c) 2011, 2012 Josef Kříž (http://www.josef-kriz.cz)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/

namespace Venne\Widgets;

use Nette\DI\CompilerExtension;

/**
* @author Josef Kříž <pepakriz@gmail.com>
*/
class WidgetsExtension extends CompilerExtension
{

public function loadConfiguration()
{
$container = $this->getContainerBuilder();

$container->addDefinition($this->prefix('widgetManager'))
->setClass('Venne\Widgets\WidgetManager');
}


public function beforeCompile()
{
$container = $this->getContainerBuilder();
$config = $container->getDefinition($this->prefix('widgetManager'));

foreach ($container->findByTag('venne.widget') as $factory => $meta) {
if (!is_string($meta)) {
throw new \Nette\InvalidArgumentException("Tag venne.widget require name. Provide it in configuration. (tags: [venne.widget: name])");
}
$config->addSetup('addWidget', array($meta, $factory));
}
}

}

0 comments on commit fd0be43

Please sign in to comment.