Custom add-on autoloading

Notice: This thread is very old.
fronty
Member | 16
+
0
-

Hi to all,

I'm trying to create custom Nette add-on, that could be required to Nette project using Composer. I have local Git repository in add-on/, which should be my future add-on and Nette application in app/ with pure Nette Sandbox instalation for testing purposes. I've updated app/composer.json like so:

<script>
{
	...
	"require": {
		...
		"fronty/add-on": "dev-master"
	},
	...
	"repositories": [
		{
			"type": "package",
			"package": {
				"name": "fronty/add-on",
				"version": "master",
				"source": {
					"url": "/Users/fronty/www/add-on/.git",
					"type": "git",
					"reference": "master"
				}
			}
		}
	]
}
</script>

In add-on/composer.json I have this:

<script>
{
    "name": "fronty/add-on",
    "description": "Test Add-on",
    "version": "1.0.0",
    "require": {
        "php": ">=5.3.0",
        "nette/database": "~2.3"
    },
    "autoload": {
        "psr-4": {
            "Fronty\\Addon\\": "src/"
        }
    }
}
</script>

In add-on/src/ there is only one Test.php file:

<?php
namespace Fronty\Addon;

class Test {

}
?>

When I ran composer update in app/, the add-on was correctly installed to app/vendor/fronty/add-on/, in this directory I have:

  • src/
    • Test.php
  • composer.json

Everything seemed fine, so I registered Test as an anonymous service to config.neon:
services:
- Fronty\Addon\Test

and injected this as dependency to default HomePresenter.php like so:

<?php
namespace App\Presenters;
use Fronty;

class HomePresenter extends BasePresenter {
	protected $test;

	public function injectTestClass(Fronty\Addon\Test.php $test) {
		$this->testClass = $test;
	}
}
?>

With this setting an exception of Nette\DI\ServiceCreationException is thrown with message:
Class Fronty\Addon\Test used in service '27_Fronty_Addon_Test' not found or is not instantiable.

Do I miss something? As far as I know, Nette should use the same autoloading method as declared in composer.json, in my case PSR-4, my namespace and class should match this standard.

Thanks for any thoughts!

fronty
Member | 16
+
0
-

I found one more resource about autoloading vendors (Czech) through which I get to here. I tried update autoload section in add-on/composer.json to "classmap": ["src/"], later "files": ["src/Test.php"]. In both cases I pushed changes to add-on/ Git, removed app/vendor/add-on/ and run composer update to get current version of add-on in app.
Unfortunately none of these updates worked.