Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop libraries for symfony while keeping the library code framework independent

I'm creating an app in Symfony which is using a library I'm writing at the same time.

While my application is being built with symfony, I want the component library to be completely framework independent as it needs to be reusable by applications not necessarily written in symfony.

Therefore I have a symfony bundle which integrates the library (the component)

The folder structure is a follows

src/MyVendor/
|-- Bundle
|   `-- MyComponentBundle
|       |-- Controller
|       |-- DependencyInjection
|       |-- Resources
|       |   |-- config
|       |   `-- views
|       |       `-- Default
|       |-- Tests
|           `-- Controller
|               `-- MyObjectControllerTest.php
|       `-- MyComponentBundle.php
`-- Component
    `-- MyComponent
        |-- doc
        |-- src
            `-- MyObject.php
        |-- test
            `-- MyObjectTest.php
        |-- .gitignore
        |-- composer.json
        |-- LICENSE
        |-- README.md
        `-- phpunit.xml.dist

Questions:

  1. Is MyComponent directory structure correct as per the PSR-x Autoloading standards? For example, how do I use MyObject from the library inside the bundle, i.e. in MyObjectControllerTest.php

  2. Can MyObject reside in the namespace of MyVendor\MyComponent? If not, how do I have to amend the directory structure so that (1) I can use that namespace, and (2) so that it can be autoloaded inside MyObjectControllerTest.php, i.e. new MyVendor\MyComponent\MyObject(); will work; right now I'm getting PHP Fatal error: Class 'MyVendor\MyComponent\MyObject' not found in ...

  3. Could you please direct me to an online resource to help me to publish MyComponent on github and make it available to symfony (I'm guessing most of that involves just setting up composer.json correctly)

Thank you

like image 239
scibuff Avatar asked Dec 17 '25 15:12

scibuff


1 Answers

  1. It's compatible with PSR-x autoloaders. But your namespaces should be properly registered (PSR-4, PSR-0).
  2. You should create proper autoload to use your component (using composer, for example).
  3. Just create a repo for your component on github, create your composer.json (or copy/past and edit some side one), submit your package on packagist (just insert github project link)
like image 98
Ziumin Avatar answered Dec 19 '25 04:12

Ziumin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!