-
Notifications
You must be signed in to change notification settings - Fork 11
Nette 2.4 compatibility #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 19 commits
e53eac8
9c46056
570f0d6
6b06f4c
fb01df6
27d2bc9
db98b25
d4da69a
30a814a
bd73dcb
91614d6
7c2d610
683510a
4cbaf8b
d3a6884
6f9352f
c763cf5
e53bc11
27ef5c5
d30cb32
9aa59f3
adf2552
3f573d9
3820012
1494fbf
9ac871b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| /nbproject/ | ||
| /composer.lock | ||
| /Nette/ | ||
| /Nette/ | ||
| .idea/ | ||
| vendor/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| namespace MultipleFileUpload; | ||
|
|
||
| use Nette; | ||
|
|
||
| class Extension extends Nette\DI\CompilerExtension { | ||
|
|
||
| public function loadConfiguration() { | ||
| } | ||
|
|
||
| public function beforeCompile() { | ||
| parent::beforeCompile(); | ||
| } | ||
|
|
||
| public function afterCompile(Nette\PhpGenerator\ClassType $class) { | ||
| $initialize = $class->methods['initialize']; | ||
| $initialize->addBody('MultipleFileUpload\MultipleFileUpload::init($this->getByType(?), $this->getParameters());', ['Nette\Http\IRequest']); | ||
| $initialize->addBody('MultipleFileUpload\MultipleFileUpload::register($this->getService(?));', ['mfuStorage']); | ||
| $initialize->addBody('$this->getService(?)->onStartup[] = [?, ?];', ['application', 'MultipleFileUpload\MultipleFileUpload', 'handleUploads']); | ||
| $initialize->addBody('$this->getService(?)->onShutdown[] = [?, ?];', ['application', 'MultipleFileUpload\MultipleFileUpload', 'cleanCache']); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,7 +72,7 @@ function addFileManually($name, $chunk, $chunks); | |
| * @param type $chunk | ||
| * @param FileUpload $file | ||
| */ | ||
| function updateFile($name, $chunk, FileUpload $file = null); | ||
| function updateFile($name, $chunk, FileUpload $file); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @akyn84 why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put this question conversely: is there special reason to have optional parameter?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly. :) |
||
|
|
||
| /** | ||
| * Gets all files in queue | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ | |
|
|
||
| use MultipleFileUpload\Model\BaseQueues, | ||
| MultipleFileUpload\Model\IQueue, | ||
| Nette\InvalidStateException; | ||
| Nette; | ||
|
|
||
| /** | ||
| * Multiple File Uploader driver for Nette\Database | ||
|
|
@@ -48,9 +48,17 @@ public function getFilesTable(){ | |
| return self::$filesTable; | ||
| } | ||
|
|
||
| public function __construct(\Nette\Database\Context $database) | ||
| public function __construct($database, $tempDir) | ||
| { | ||
| $this->database = $database; | ||
| // TODO: wrong; there must be not dependency on container here | ||
| $connection = new Nette\Database\Connection($database['dsn'], $database['user'], $database['password']); | ||
| self::$uploadsTempDir = $tempDir . DIRECTORY_SEPARATOR . "uploads-MFU"; | ||
| if(!file_exists(self::$uploadsTempDir)) { | ||
| mkdir(self::$uploadsTempDir, 0775, TRUE); | ||
| }; | ||
| $cacheStorage = new Nette\Caching\Storages\FileStorage(self::$uploadsTempDir); | ||
| $structure = new Nette\Database\Structure($connection, $cacheStorage); | ||
| $this->database = new Nette\Database\Context($connection, $structure); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whole Database\Context should be injected. IT is responsibility of DI container to create connection. |
||
| } | ||
|
|
||
| // <editor-fold defaultstate="collapsed" desc="Database functions"> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,14 +15,15 @@ | |
| use MultipleFileUpload\Model\IQueue, | ||
| MultipleFileUpload\Model\IQueues, | ||
| MultipleFileUpload\UI\Registrator, | ||
| Nette\Environment, | ||
| Latte, | ||
| Nette, | ||
| Nette\Bridges, | ||
| Nette\Forms, | ||
| Nette\Forms\Container, | ||
| Nette\Forms\Controls\UploadControl, | ||
| Nette\Http\FileUpload, | ||
| Nette\InvalidStateException, | ||
| Nette\NotSupportedException, | ||
| Nette\Utils\Callback, | ||
| Nette\Utils\Html, | ||
| Nette\Utils\Strings; | ||
|
|
||
|
|
@@ -62,35 +63,35 @@ class MultipleFileUpload extends UploadControl | |
| */ | ||
| public static $baseWWWRoot = null; | ||
|
|
||
| /** @var Nette\Http\IRequest */ | ||
| private static $request; | ||
|
|
||
| /** @var bool */ | ||
| private static $productionMode; | ||
|
|
||
| /** | ||
| * Initialize MFU | ||
| */ | ||
| public static function init() | ||
| public static function init(Nette\Http\IRequest $request, Array $parameters) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. static methods from this class init(), register(), ... needs to be split into another class that will be created and registered in constructor. MFU will then take this object as a dependency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when this functionality is removed to separate class and injected in constructor, dependencies are missing in MultipleFileUpload::handleUploads called on startup of application. If handleUploads is dynamic and called on Nette\Application\Application->startUp[], class itself is initialized before attached to component (too soon). can be logic of MultipleFileUpload::handleUploads solved outside of class with completly independent service? |
||
| { | ||
| // Init UI registrator | ||
| $uiReg = self::$interfaceRegistrator = new Registrator(); | ||
| $uiReg->register("MultipleFileUpload\\UI\\HTML4SingleUpload"); | ||
| $uiReg->register("MultipleFileUpload\\UI\\Plupload"); | ||
|
|
||
| self::$request = $request; | ||
| self::$productionMode = $parameters['productionMode']; | ||
| // Set default check callback | ||
| self::$validateFileCallback = callback(__CLASS__, "validateFile"); | ||
|
|
||
| // TODO: remove this magic | ||
| self::$baseWWWRoot = Environment::getHttpRequest()->url->baseUrl . "MultipleFileUpload/"; | ||
| self::$validateFileCallback = [__CLASS__, "validateFile"]; | ||
| self::$baseWWWRoot = self::$request->url->baseUrl . "MultipleFileUpload/"; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Register MFU into Nette | ||
| */ | ||
| public static function register() | ||
| { | ||
| self::init(); | ||
|
|
||
| $application = Environment::getApplication(); | ||
| $application->onStartup[] = callback(__CLASS__, "handleUploads"); | ||
| $application->onShutdown[] = callback(__CLASS__, "cleanCache"); | ||
| public static function register(Model\IQueues $queuesModel) | ||
| { | ||
| self::$queuesModel = $queuesModel; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -133,8 +134,7 @@ public static function handleUploads() | |
| self::$handleUploadsCalled = true; | ||
| } | ||
|
|
||
| $req = Environment::getHttpRequest(); | ||
|
|
||
| $req = self::$request; | ||
| // Workaround for: http://forum.nettephp.com/cs/3680-httprequest-getheaders-a-content-type | ||
| $contentType = $req->getHeader("content-type"); | ||
| if (!$contentType and isset($_SERVER["CONTENT_TYPE"])) { | ||
|
|
@@ -147,7 +147,7 @@ public static function handleUploads() | |
|
|
||
| self::getQueuesModel()->initialize(); | ||
|
|
||
| foreach (self::getUIRegistrator()->getInterfaces() AS $interface) { | ||
| foreach (self::getUIRegistrator()->getInterfaces(self::$request) AS $interface) { | ||
| // \Nette\Diagnostics\Debugger::log($interface->getReflection()->getName().": is this your upload? ".$interface->isThisYourUpload()); | ||
| if ($interface->isThisYourUpload()) { | ||
| $ret = $interface->handleUploads(); | ||
|
|
@@ -174,7 +174,7 @@ public static function validateFile(FileUpload $file) | |
| */ | ||
| public static function cleanCache() | ||
| { | ||
| if (!Environment::isProduction() or rand(1, 100) < 5) { | ||
| if (FALSE == self::$productionMode or rand(1, 100) < 5) { | ||
| self::getQueuesModel()->cleanup(); | ||
| } | ||
| } | ||
|
|
@@ -186,12 +186,8 @@ public static function cleanCache() | |
| */ | ||
| public static function getQueuesModel() | ||
| { | ||
| if (!self::$queuesModel) { // if nothing is set, setup sqlite model, which should work on all systems with SQLite | ||
| self::setQueuesModel(new Model\SQLite3\Queues()); | ||
| } | ||
|
|
||
| if (!self::$queuesModel instanceof IQueues) { | ||
| throw new InvalidStateException("Queues model is not instance of Model\IQueues!"); | ||
| throw new InvalidStateException("Queues model was not set or is not instance of Model\IQueues!"); | ||
| } | ||
| return self::$queuesModel; | ||
| } | ||
|
|
@@ -258,15 +254,13 @@ public static function getHead() | |
| */ | ||
| public $simUploadThreads; | ||
|
|
||
|
|
||
| /** | ||
| * Constructor | ||
| * @param string $label Label | ||
| */ | ||
| public function __construct($label = NULL, $maxSelectedFiles = 25) | ||
| { | ||
| parent::__construct($label); | ||
|
|
||
| if (!self::$handleUploadsCalled) { | ||
| throw new InvalidStateException("MultipleFileUpload::handleUpload() has not been called. Call `MultipleFileUpload::register();` from your bootstrap before you call Applicaton::run();"); | ||
| }; | ||
|
|
@@ -291,12 +285,11 @@ public function getControl() | |
|
|
||
| // <section token field> | ||
| $tokenField = Html::el('input type=hidden')->name($this->getHtmlName() . '[token]')->value($this->getToken()); | ||
| $control->add($tokenField); | ||
| $control->addHtml($tokenField); | ||
| // </section token field> | ||
|
|
||
| $fallbacks = array(); | ||
|
|
||
| $interfaces = self::getUIRegistrator()->getInterfaces(); | ||
| $interfaces = self::getUIRegistrator()->getInterfaces(self::$request); | ||
| $num = count($interfaces); | ||
| $cnt = 1; | ||
| foreach ($interfaces AS $interface) { | ||
|
|
@@ -324,14 +317,15 @@ public function getControl() | |
| } | ||
| $cnt++; | ||
|
|
||
| $control->add($container); | ||
| $control->addHtml($container); | ||
| } | ||
|
|
||
| $template = new UI\Template(); | ||
| $template->setFile(dirname(__FILE__) . DIRECTORY_SEPARATOR . "RegisterJS.latte"); | ||
| $template->id = $this->getHtmlId(); | ||
| $template->fallbacks = $fallbacks; | ||
| $control->add($template->__toString(TRUE)); | ||
| $latte = new Latte\Engine(); | ||
| $template = new Bridges\ApplicationLatte\Template($latte); | ||
| $template->setFile(__DIR__ . DIRECTORY_SEPARATOR . "RegisterJS.latte"); | ||
| $template->id = $this->getHtmlId(); | ||
| $template->fallbacks = $fallbacks; | ||
| $control->addHtml($template->__toString(TRUE)); | ||
| /* | ||
| // <section without JavaScript> | ||
| $withoutJS = Html::el("div class=withoutJS"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
completely missing configuration of storage driver + UIs