Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
3KyNoX Posted May 17, 2015 Posted May 17, 2015 Hello !Following currently Plugin Creation and The Autoloader, and got some difficulties to find out how to load a 3rd party framework.I know new 4.x guides are in hard writing, but end of Autoloader Guide is quite small.To explain what I'm doing, I need to uses PhealNG framework inside my plugin, so I :Created a Code HookInstalled PhealNG with composer in system/3rd_party/phealngInside that folder, I got the vendor foldervendor-> autoload.php (that loads all other classes)Used->\IPS\IPS::$PSR0Namespaces['phealng'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/vendor/autoload.php'; in my hook.Anf don't know what to do nextIt looks like when I call :use Pheal\Pheal; $pheal = new Pheal();It throw an Exception.Using IPS 4.0.5.1.Any advices please ?
Marcher Technologies Posted May 18, 2015 Posted May 18, 2015 Assuming you expect the end-user to use composer as well(plugins do not support custom source files on their own), without seeing more of your code and the exception being generated it's going to be shooting in the dark to assist. Have you tried just calling it by it's FQN?$pheal = new \Pheal\Pheal();
3KyNoX Posted May 18, 2015 Author Posted May 18, 2015 Thanks for reply. I must do something wrong. First, code can be found here -> PhealNG Framework I made it work with a simple php page. Inside IPB I made a code hook following tutorial and tried to link autoload.php inside vendor folder. calling Pheal throw an error, same if I move alone ... \IPS\IPS::$PSR0Namespaces['phealng'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/vendor/autoload.php'; Inside the class, throw too an error. I tried to load autoload.php manually after upload of framework inside ../plugins/EvEBridge/hooks : require_once 'vendor/autoload.php'; throwing error page since call. I hope more details are given and thanks in advance for help.
Marcher Technologies Posted May 18, 2015 Posted May 18, 2015 The developer center is correct, that is not valid code, you are basically defining an object property as a new object, something php doesn't support. Click on the method you wish to extend on the left, then construct the object within it.
3KyNoX Posted May 19, 2015 Author Posted May 19, 2015 Hello again,I first gives some apologies, because I'm a .Net developer and while object programming concepts is aquired, php syntax is not perfectly known in the present day.I tried to extend a method in the left column in developer center, and while declaring call of the framework before the _hook class (just after //<?php)\IPS\IPS::$PSR0Namespaces['phealng'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/vendor/autoload.php'; use Pheal\Pheal I create a object instance inside the extended method :$pheal = new Pheal();No error is this time shown. But it looks like my object instance is not usable, because if I remove first 2 lines calling framework, my object instance do not throw an error alone too.Not sure if it's well explained; http://puu.sh/hSMOn/59bbe4ead1.png <--- Works with no error, and it's not normal, because framework not called.---------------------------------------Anyway, I tried to take the problem differently and try to get access to my framework classes manually.I this time created a Theme Hook (like on the plugin tutorial example "core --> front: global -> globalTemplate")I inserted globalMessage div content:<div class="ipsMessage ipsMessage_information">This is the global message.</div>and using my ftp client, I edited manually the hook file to call framework://<?php require_once 'vendor/autoload.php'; class hook24 extends _HOOK_CLASS_ { /* !Hook Data - DO NOT REMOVE */ public static function hookData() { return array_merge_recursive( array ( 'globalTemplate' => array ( 0 => array ( 'selector' => '#ipsLayout_mainArea', 'type' => 'add_inside_start', 'content' => '<div class="ipsMessage ipsMessage_information">This is the global message.</div>', ), ), ), parent::hookData() ); } /* End Hook Data */ }I surely uploaded my framework vendor/ folder inside my hooks folder.But when loading IPB front page, "vendor/autoload.php" is not found -> http://puu.sh/hSNtd/02f33baeaa.pngSo, I'm blocked now and not sure what do do next, so if more support is possible, it would be greatly appreciated.
Marcher Technologies Posted May 19, 2015 Posted May 19, 2015 \IPS\IPS::$PSR0Namespaces['phealng'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/vendor/autoload.php';this is the problem itself, sorry for not spotting it sooner. you are simply supposed to provide the base path to the psr-0 compatible library, not call their autoloader, also the array key is the namespace's first 'bit', so...\IPS\IPS::$PSR0Namespaces['Pheal'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/';such is the beauty of the psr-0 standard, this is enough for the IPS autoloader to figure it out based on the class name called.... as support for such is baked in, the autoloader composer provides is strictly not needed.
3KyNoX Posted May 19, 2015 Author Posted May 19, 2015 Ok trying this, you do not have to be sorry for anything, help is gratefull.Maybe you have a suggestion to test it out the content of a variable to see if everything is ok ? (still caused by my lack of knowing ipb system and classes correctly).Like show a popup with the display of this var content when extending a action button method of a specific class ? EDIT :Used new IPS call : http://puu.sh/hSTsz/8438ccb1ad.pngAs it don't show code error from developer center, now forum index page shows : http://puu.sh/hSTzd/4605936390.pngphealng folder Permissions are set to 777 recursively for testing purpose and path is correct : http://puu.sh/hSTC3/964d28095c.png
Marcher Technologies Posted May 19, 2015 Posted May 19, 2015 I see, I didn't expect the source files themselves to be in /vendor/, just the autoloader unneeded.\IPS\IPS::$PSR0Namespaces['Pheal'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/vendor/'; should work in that case.
3KyNoX Posted May 19, 2015 Author Posted May 19, 2015 Ok got it, your answer puts me on right way, correct path was : \IPS\IPS::$PSR0Namespaces['Pheal'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/vendor/3rdpartyeve/phealng/lib/Pheal/'; using that path, I was able to print some output. Next steps will be find out what class / method to extend to modify registration form and user cp. I will maybe create another topic about that. Thanks a lot for your help !
3KyNoX Posted May 20, 2015 Author Posted May 20, 2015 Another question, is it possible to load my framework once on a top level position and be able to use it anywhere in plugins hooks / Apps ?
Daniel F Posted May 20, 2015 Posted May 20, 2015 You could add\IPS\IPS::$PSR0Namespaces['Pheal'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/vendor/3rdpartyeve/phealng/lib/Pheal/';to the constants.php
3KyNoX Posted May 20, 2015 Author Posted May 20, 2015 My test forum did not liked that, white pages shown. No error shown.If I make this works, could the line :use Pheal\Pheal;be added in constants also ?
Ryan H. Posted May 20, 2015 Posted May 20, 2015 If you have a white page, check your server's PHP error log.
3KyNoX Posted May 20, 2015 Author Posted May 20, 2015 Thanks for your answer, unfortunately, apache2 error.log don't show anything while reloading and displaying that blank page.
newbie LAC Posted May 22, 2015 Posted May 22, 2015 Hello,My test forum did not liked that, white pages shown. No error shown.Because constant \IPS\ROOT_PATH is not definedinit.php /* Load constants.php */ if( file_exists( __DIR__ . '/constants.php' ) ) { @include_once( __DIR__ . '/constants.php' ); } /* Import and set defaults */ $defaultConstants = static::defaultConstants(); foreach ( $defaultConstants as $k => $v ) { if( defined( $k ) ) { define( 'IPS\\' . $k, constant( $k ) ); } else { define( 'IPS\\' . $k, $v ); } }Your code runs before the constant \IPS\ROOT_PATH will be defined
3KyNoX Posted May 23, 2015 Author Posted May 23, 2015 Thanks a lot. Corrected path, and it works. Assuming constants.php is already in the root path : \IPS\IPS::$PSR0Namespaces['Pheal'] = '/system/3rd_party/phealng/vendor/3rdpartyeve/phealng/lib/Pheal/'; use Pheal\Pheal; Now, still from plugin example, in modified template \IPS\Theme\class_core_front_global: {{$pheal = new \Pheal\Pheal();}} {{$response = $pheal->serverScope->ServerStatus();}} <div class="ipsMessage ipsMessage_information">{{echo $response->onlinePlayers;}}</div> Displays correctly online players number, but why a simple 'echo' statement make the front page displays without css decorating : Thanks for further help.
3KyNoX Posted May 23, 2015 Author Posted May 23, 2015 Ok got it, double {{ }} and echo is not needed here, this lines done the job:{{$pheal = new \Pheal\Pheal();}} {{$response = $pheal->serverScope->ServerStatus();}} <div class="ipsMessage ipsMessage_information">There is {$response->onlinePlayers} players online.</div>
3KyNoX Posted May 23, 2015 Author Posted May 23, 2015 Finally no, it don't work, explaining myself.It was working because I'm having currently two plugin hooks.The Theme Hook like on the plugin tutorial example -> \IPS\Theme\class_core_front_global{{$pheal = new \Pheal\Pheal();}} {{$response = $pheal->serverScope->ServerStatus();}} <div class="ipsMessage ipsMessage_information">The servers are currently {$scope->serverOpen ? "open" : "closed"} and there is currently {$scope->onlinePlayers} players online.</div> And a code hook with call to my framework inside -> \IPS\Login//<?php \IPS\IPS::$PSR0Namespaces['Pheal'] = \IPS\ROOT_PATH . '/system/3rd_party/phealng/vendor/3rdpartyeve/phealng/lib/Pheal/'; use Pheal\Pheal; class hook12 extends _HOOK_CLASS_ { public function __construct( \IPS\Http\Url $url ) { $pheal = new Pheal(); $response = $pheal->serverScope->ServerStatus(); return call_user_func_array( 'parent::__construct', func_get_args() ); } } Because I was not logged in on my forums, and the presence of the code hook (with my framework call inside), it displayed my variables correctly in front page.But once I log-in or I deleted my code hook, I get back error -> Class 'Pheal\Pheal' not found.Meaning my code inside contants.php is not used for whole project and my theme hook so.Here is the content of constants.php:<?php define( 'IN_DEV', TRUE ); \IPS\IPS::$PSR0Namespaces['Pheal'] = '/system/3rd_party/phealng/vendor/3rdpartyeve/phealng/lib/Pheal/'; use Pheal\Pheal;Thanks for any further help.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.