Forums / All Other Contributions/Addons / encapsulated plugins require question....

encapsulated plugins require question....

Views: 3,562

Results 1 to 20 of 20
1 Oct 2020, 4:05 PM
#1
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,969
Plugin Contributions:
8

encapsulated plugins require question....

using a v157 base install.... i'm working on new admin plugin...

lets say there i have a class file, that is in:

zc_plugins/myplugin/1.0.0/admin/includes/classes/class.php

i now want to require() a new script in this class file. previously i might do something like

require_once DIR_FS_ADMIN .  'includes/classes/other_class.php';

when i look at the $_SERVER vars, available to me, nothing pinpoints where i am in the file system to refer to new files that ideally i would like to keep in my encapsulated plugin.

is there an easy way to accomplish this goal? i'm guessing there is a ZC var that knows where i am. as i do not...

thanks in advance.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

1 Oct 2020, 4:18 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: encapsulated plugins require question....

There isn't any special function or variable for that as far as I recall.

But if the file is in the same or nearby directory structure, you could explore using relative paths with ../.. etc.

However, your post raises a question, and understanding context would help:
WHY are you requiring another file from within a class? Perhaps it's better to put your other desired file code into a class of its own and then simply refer to the class?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

1 Oct 2020, 4:36 PM
#3
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,969
Plugin Contributions:
8

Re: encapsulated plugins require question....

DrByte:

There isn't any special function or variable for that as far as I recall.

But if the file is in the same or nearby directory structure, you could explore using relative paths with ../.. etc.

However, your post raises a question, and understanding context would help:
WHY are you requiring another file from within a class? Perhaps it's better to put your other desired file code into a class of its own and then simply refer to the class?

drByte, thanks for the input. it seems:

$path_info = pathinfo(__FILE__)['dirname'];

works.

as far as why... whole other can of worms...

using api wrappers for some of the heavy lifting on common carrier codes rather than hand coding seems to make much more sense to me. and it is actually not within the class, but extending the class prior to the class declaration. ie:

<?php

	$path_info = pathinfo(__FILE__)['dirname'];
	if (!file_exists($sdk_loader =  $path_info . '/prose/vendor/autoload.php')) {
		return false;
	}
	require $sdk_loader;
	require_once $path_info . '/prose/shipping.php';

	class ups extends shipping
	{

does that make sense?

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

1 Oct 2020, 5:00 PM
#4
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,969
Plugin Contributions:
8

Re: encapsulated plugins require question....

i suppose my next question is whether observers work within the encapsulated plugin?

my initial testing suggests they do not. or perhaps it is just the auto loading observers that do not work?

i will try to manually load it.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

1 Oct 2020, 5:48 PM
#5
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,969
Plugin Contributions:
8

Re: encapsulated plugins require question....

carlwhat:

i suppose my next question is whether observers work within the encapsulated plugin?

my initial testing suggests they do not. or perhaps it is just the auto loading observers that do not work?

i will try to manually load it.

i can not get any observers to load from within an observers directory structure. which i suppose is why it is not listed here:

https://docs.zen-cart.com/dev/plugins/encapsulated_plugins/directory_structure/

it seems that the auto_loaders operates a little differently from within the zc_plugins directory that outside of it.

it seems that i can make do using a few hacks, but i think being able to load an observer from within the encapsulated directory structure would be a good idea.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

1 Oct 2020, 5:55 PM
#6
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: encapsulated plugins require question....

carlwhat:

i suppose my next question is whether observers work within the encapsulated plugin?

my initial testing suggests they do not. or perhaps it is just the auto loading observers that do not work?

i will try to manually load it.
In observing did you both load the observer class and instantiate it as well as ensure that the observer is loaded/instantiated before the trigger point of the notifier is reached?

Also as to the require type condition as DrByte was discussing, the initial portion of the path is not mandated. Note how the admin's index.php file has in it a require includes/application_top.php and not an "admin" related prefix (which of course also doesn't exist at that point in loading the file).

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

1 Oct 2020, 6:29 PM
#7
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: encapsulated plugins require question....

carlwhat:

class ups extends shipping
Be careful about declaring a new class named `ups` if the store might already have a `ups` module installed.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

1 Oct 2020, 6:31 PM
#8
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,969
Plugin Contributions:
8

Re: encapsulated plugins require question....

mc12345678:

In observing did you both load the observer class and instantiate it as well as ensure that the observer is loaded/instantiated before the trigger point of the notifier is reached?

Also as to the require type condition as DrByte was discussing, the initial portion of the path is not mandated. Note how the admin's index.php file has in it a require includes/application_top.php and not an "admin" related prefix (which of course also doesn't exist at that point in loading the file).

i turned the DEBUG_AUTOLOAD on. and i could not get the proper file path to come up with for an observer from within the encapsulated directory. so instantiating it is kind of besides the point.

file paths are much trickier from within the encapsulated plugins. i'm not sure how much you have played with them. the auto load does a whole bunch of different things which unfortunately i do not have time to debug and play with.

my testing suggests that currently you will have to do some tricky things from within the auto_loaders config file to get to an observer from within an encapsulated plugin.

would love for you to prove me wrong or show me the way!

best.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

1 Oct 2020, 6:34 PM
#9
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: encapsulated plugins require question....

v158 will support auto-loaded observers
(https://github.com/zencart/zencart/blob/v158/includes/init_includes/init_observers.php)

But v157 does not.
For v157 you could use the auto_loaders config files to declare which files/classes to load/instantiate "the old way".

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

1 Oct 2020, 6:38 PM
#10
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: encapsulated plugins require question....

DrByte:

There isn't any special function or variable for that as far as I recall.
zcwilt just mentioned that there does exist a getPluginVersionDirectory() function:

https://github.com/zencart/zencart/blob/v157/includes/classes/PluginManager.php

which is also used in the various methods of
https://github.com/zencart/zencart/blob/v157/includes/classes/InitSystem.php

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

1 Oct 2020, 6:39 PM
#11
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: encapsulated plugins require question....

Depending on which file you're in, using DIR could be useful (as opposed to FILE), as it gives you current directory information.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

1 Oct 2020, 7:12 PM
#12
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: encapsulated plugins require question....

carlwhat:

i turned the DEBUG_AUTOLOAD on. and i could not get the proper file path to come up with for an observer from within the encapsulated directory. so instantiating it is kind of besides the point.

file paths are much trickier from within the encapsulated plugins. i'm not sure how much you have played with them. the auto load does a whole bunch of different things which unfortunately i do not have time to debug and play with.

my testing suggests that currently you will have to do some tricky things from within the auto_loaders config file to get to an observer from within an encapsulated plugin.

would love for you to prove me wrong or show me the way!

best.

Challenge accepted?! :)

DrByte:

zcwilt just mentioned that there does exist a getPluginVersionDirectory() function:

https://github.com/zencart/zencart/blob/v157/includes/classes/PluginManager.php

which is also used in the various methods of
https://github.com/zencart/zencart/blob/v157/includes/classes/InitSystem.php
The second link provides functionality that would get you to the point of loading and instantiating an observer class through the use of the auto_loaders type structure. Basically whatever "function" you wanted to be done would be the 'autoType' in the auto_loaders file. Effectively the method called within the InitSystem is 'processAutoLoader' . $var['autoType'] (yes there is some case play, but lets say you wanted to load a class file (such as an observer), then the 'autoType' would be 'class' to load the class, then 'classInstantiate' to initialize the class (calling its __construct method).)

To "simply" require a file would in fact need some "path" information as discussed above about "finding" where things are currently located so that the associated processAutoLoaderRequire method could pull in the correct file(s).

For loading a class would likely have the autoloader look something like:

<?php
if (!defined('IS_ADMIN_FLAG')) {
    die('Illegal Access');
}

$autoLoadConfig[200][] = [
    'autoType'  => 'class',
    'classPath' => 'observers/',
    'loadFile'  => 'new_observer.php',
    ];
$autoLoadConfig[200][] = [
    'autoType'  => 'classInstantiate',
    'className' => 'zcObserverNewObserverClass',
    'objectName' => 'myObsVariable',
    'classSession' => false,
    'checkInstantiated' => false,
    ];

This seems as if it would load an observers class in the admin directory of the plugins section for "this" version of the plugin.

The last two variables of the classInstantiate section are not required, but at least provide a template for what might be expected in instantiating a class through the plugin controller...

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

1 Oct 2020, 7:18 PM
#13
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,969
Plugin Contributions:
8

Re: encapsulated plugins require question....

DrByte:

Be careful about declaring a new class named ups if the store might already have a ups module installed.

noted.

DrByte:

v158 will support auto-loaded observers
(https://github.com/zencart/zencart/blob/v158/includes/init_includes/init_ob$initSystem-setDebug%28true%29;$initSystem->setDebug%28true%29;servers.php>)

But v157 does not.
For v157 you could use the auto_loaders config files to declare which files/classes to load/instantiate "the old way".

as i just learned about the auto. loading of observers, i have previous done all of the "heavy lifting" myself. so this is not a problem.

DrByte:

zcwilt just mentioned that there does exist a getPluginVersionDirectory() function:

https://github.com/zencart/zencart/blob/v157/includes/classes/PluginManager.php

which is also used in the various methods of $initSystem->setDebug(true);
https://github.com/zencart/zencart/blob/v157/includes/classes/InitSystem.php

ok... i will debug and find out where i am going wrong. or if there is an issue in the initSystem. i'm guessing now that this is where i will be looking.

https://github.com/zencart/zencart/blob/1a38bf71260efb161037a75701564e49168b8011/includes/classes/InitSystem.php#L82

DrByte:

Depending on which file you're in, using DIR could be useful (as opposed to FILE), as it gives you current directory information.

i ended up using:

$path_info = dirname(__DIR__);

which i think is cleaner, in-line with what you are saying, and easier to read. i will perhaps investigate the aforementioned ZC function though.

thanks again. i will let you know where i have gone adrift in the loading of the observer.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

1 Oct 2020, 7:31 PM
#14
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,969
Plugin Contributions:
8

Re: encapsulated plugins require question....

mc12345678:

Challenge accepted?! :)

The second link provides functionality that would get you to the point of loading and instantiating an observer class through the use of the auto_loaders type structure. Basically whatever "function" you wanted to be done would be the 'autoType' in the auto_loaders file. Effectively the method called within the InitSystem is 'processAutoLoader' . $var['autoType'] (yes there is some case play, but lets say you wanted to load a class file (such as an observer), then the 'autoType' would be 'class' to load the class, then 'classInstantiate' to initialize the class (calling its __construct method).)

To "simply" require a file would in fact need some "path" information as discussed above about "finding" where things are currently located so that the associated processAutoLoaderRequire method could pull in the correct file(s).

For loading a class would likely have the autoloader look something like:

<?php if (!defined('IS_ADMIN_FLAG')) { die('Illegal Access'); } $autoLoadConfig[200][] = [ 'autoType' => 'class', 'classPath' => 'observers/', 'loadFile' => 'new_observer.php', ]; $autoLoadConfig[200][] = [ 'autoType' => 'classInstantiate', 'className' => 'zcObserverNewObserverClass', 'objectName' => 'myObsVariable', 'classSession' => false, 'checkInstantiated' => false, ]; ``` > > This seems as if it would load an observers class in the admin directory of the plugins section for "this" version of the plugin. > > The last two variables of the classInstantiate section are not required, but at least provide a template for what might be expected in instantiating a class through the plugin controller... thank you. this works here: ```php $autoLoadConfig[200][] = [ 'autoType' => 'class', 'classPath' => 'includes/classes/observers/', 'loadFile' => 'my_bloody_observer.php', ]; ``` **much** appreciated!

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

1 Oct 2020, 7:43 PM
#15
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: encapsulated plugins require question....

carlwhat:

thank you.

this works here:

$autoLoadConfig[200][] = [
'autoType' => 'class',
'classPath' => 'includes/classes/observers/',
'loadFile' => 'my_bloody_observer.php',
];

> 
> **much** appreciated!

Hmmm, because I was going to make a suggestion of a potential alteration to the above path, wanted to ask/confirm that the observer is in fact in the plugin and not installed to the "core" fileset, correct?

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

1 Oct 2020, 8:01 PM
#16
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,969
Plugin Contributions:
8

Re: encapsulated plugins require question....

mc12345678:

Hmmm, because I was going to make a suggestion of a potential alteration to the above path, wanted to ask/confirm that the observer is in fact in the plugin and not installed to the "core" fileset, correct?

correct. yes.

[236] => Auto Type Method - processAutoTypeClass
[237] => processing class - /var/www/base157/zc_plugins/shipAdmin/1.0.0/admin/includes/classes/observers/my_bloody_observer.php
[238] => loading class - /var/www/base157/zc_plugins/shipAdmin/1.0.0/admin/includes/classes/observers/my_bloody_observer.php - SUCCESS

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

1 Oct 2020, 8:20 PM
#17
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: encapsulated plugins require question....

carlwhat:

correct. yes.

[236] => Auto Type Method - processAutoTypeClass
[237] => processing class - /var/www/base157/zc_plugins/shipAdmin/1.0.0/admin/includes/classes/observers/my_bloody_observer.php
[238] => loading class - /var/www/base157/zc_plugins/shipAdmin/1.0.0/admin/includes/classes/observers/my_bloody_observer.php - SUCCESS

Seems like it would be a good idea in the docs to indicate that constants such as DIR_WS_INCLUDES should not be used in plugins when referring to directories such as the classes and/or other "defined" paths...

Reason? those particular constants are associated with the store and not with the plugins where plugins should be developed to be applicable to "any" store's configuration.  Meaning, the path used should be relative to the plugin's directory structure and not the store's directory structure when referencing items within the plugin's folder...

I was otherwise going to suggest actually using DIR_WS_CLASSES (which I thought was going to be used by the plugin loader path until you made the change identified above and showing the processed path here).

Btw, in line with DrByte's previous statement about loading a class file, using the above 'class'/'classInstantiate' would allow you to declare a variable name that can be used within your other plugin areas. That variable is the 'objectName'. In this way you don't have to "chase" down the current path for the file being loaded, just load the class as part of your plugin and reference it from within the plugin...

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

1 Oct 2020, 8:28 PM
#18
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,969
Plugin Contributions:
8

Re: encapsulated plugins require question....

mc12345678:

Btw, in line with DrByte's previous statement about loading a class file, using the above 'class'/'classInstantiate' would allow you to declare a variable name that can be used within your other plugin areas. That variable is the 'objectName'. In this way you don't have to "chase" down the current path for the file being loaded, just load the class as part of your plugin and reference it from within the plugin...

interesting. perhaps i will look into it.

next hurdle to jump over is some javascript files that need to get loaded on the page load. again trying to keep everything within the encapsulated plugin directory. i suppose i could load the contents into the page load as opposed to a reference to the file (which would necessitate being outside of the plugin). but i am open to other ideas.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

1 Oct 2020, 9:30 PM
#19
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: encapsulated plugins require question....

carlwhat:

interesting. perhaps i will look into it.

next hurdle to jump over is some javascript files that need to get loaded on the page load. again trying to keep everything within the encapsulated plugin directory. i suppose i could load the contents into the page load as opposed to a reference to the file (which would necessitate being outside of the plugin). but i am open to other ideas.
Umm. there's already script to load a plugin's javascript:

admin/includes/javascript_loader.php Starting at about line 54... Appears that there is "no magic" necessary. Filenaming may be the only "trigger" issue of concern.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

12 Oct 2020, 11:22 PM
#20
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,969
Plugin Contributions:
8

Re: encapsulated plugins require question....

mc12345678:

Umm. there's already script to load a plugin's javascript:

admin/includes/javascript_loader.php Starting at about line 54... Appears that there is "no magic" necessary. Filenaming may be the only "trigger" issue of concern.

ok, thanks for this. it seems that i had some difficulty with file names that had multiple words in them. for example, if my filename was bloody.php, includes/javascript/bloody.js would load as well as includes/javascript/bloody_rsvp-min-3.1.0.min.js. if, my filename was bloody_mask.php; i had some difficulty getting the .js files to load.

could be me.

but, again, i do appreciate the help. using the ZC file naming for loading javascript works out better here.

best.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.