How to reuse sylius components in another project? - php

I recently got to know the Sylius project and was trying to reuse its components in a separate project (study only).
My goal was to test if I can use the sylius components in a separate project. Only a few components.
Following the documentation (http://sylius-older.readthedocs.io/en/latest/components/Order/basic_usage.html), I was able to install the components and use their classes, but how do I do the database tables?
I installed the doctrine and tried to map the classes, but I could not.
I was thinking of creating the migration (doctrine or eloquent) for each table and doing the actions (CRUD).
Thank you so much guys.

Assuming you have installed the OrderBundle using Composer you will probably have to tell Doctrine where to read the entity mapping. In case of Sylius' OrderBundle they are stored as xml files in Resources/config/doctrine/models, e.g. Order.orm.xml. If you look at the sample configuration in the DoctrineBundle-recipe you can find a reference for a manual mapping. In your case it probably should look something like this:
# app/config/config.yml (in Symfony 3.4)
# config/packages/doctrine.yaml (in Symfony 4)
doctrine:
dbal:
...
orm:
mappings:
SyliusOrderBundle:
is_bundle: false
type: xml
dir: '%kernel.project_dir%/../vendor/sylius/order-bundle/Resources/config/doctrine/models'
prefix: 'Sylius\Component\Order\Model'
alias: SyliusOrder
You might have to tweak this, e.g. if you have a Symfony 4 app, but with this you should be able to create the appropriate schema using the default Doctrine commands. You might also have to adjust auto_mapping under doctrine.orm and possibly manually map your own entities if you do this.

Related

Is Caching necessary when using Custom Annotations in Symfony?

In my Syfmony 4.1 project, I'm using Custom Annotations to apply some meta data to some Value Objects. I'm following the Doctrine Annotation Documentation, which (I think) is assuming that you're using Doctrine as a stand alone package.
However, it seems to me that Symfony is handing some of the Annotation Setup for you. As an example, the documentation talks about using its own autoloading mechanism rather than the one provided by PHP / Composer. Symfony seems to handle this automatically.
The documentation talks about Annotation Readers and recommends caching your Annotations with either a FileCacheReader or Doctrine AppCache. I'm assuming that Symfony is using one of these mechanisms (or it's own caching mechanism) to cache Symfony's built in Annotations.
My question is this: Does Symofny automatically handle the caching of Custom Annotations, or do I need to cache them manually?
Symfony takes care the caching of doctrine annotations through the doctrine bundle configuration:
doctrine:
orm:
metadata_cache_driver: array #this is the option to configure
query_cache_driver: array
result_cache_driver: array
The metadata_cache_driver options define the cache driver that symfony uses to cache the annotations.
All you have to do is to configure this option properly to have your annotations cached.
More on how to properly configure the cache driver options in documentation.

SF3: reusable bundle and model updates

We currently have several instances of SF3 websites, and we need to create a new bundle that'll be used by some of these instances. To do so, we decided to go with a reusable bundle as discussed here and it seems that everything is working as expected.
The issue we have is regarding entities; how should we create them in the database and, more importantly, how can we update the model when we update the bundle ?
According to a DoctrineMigration's thread this bundle is not to be used the way we want to and the developers do not plan on adding this feature. Another bundle seems to do the work but I am unsure on whether it's a good idea or not.
Some documentation states that it is possible to add a mapping in the doctrine configuration node (like this and that) but I don't think that'll be enough to manage future changes in entities.
Is there any "official" way of doing so ? Did we choose the wrong path and should consider an alternative ?
I may be out of bounds, but couldn't you define manually the mappings from the shared bundle?
orm:
auto_mapping: false
mappings:
MySharedBundleName:
type: yml
dir: %kernel.root_dir%/../src/MySharedBundleName/Resources/config/doctrine

Symfony2 bundle based application config merged into global config

I'm currently building a fairly large Symfony 2 application with a number of ever increasing bundles that I'm struggling to keep count of.
I try to keep all bundles specific to each module of the system, which has provided me with a nice easy to manage structure. What isn't so nice is my app/config/config.yml that's very quickly grown out of control. Understand imports can be done within this file which have helped me somewhat but I'd like to move a whole range of config specific to each bundle which should help to keep these bundles reusable within our other applications.
Here of some examples of what I'd like to move to individual bundles.
1) Dependency Injection
We use JMSDiExtraBundle within all internal bundles as we find it makes developing a little bit quicker and easier to manage.
# Annotations/Config
jms_di_extra:
locations:
bundles:
- AppBundle
- BlogBundle
- ContentBundle
- StoreBundle
- UserBundle
I'd like this block to be moved to each individuals bundle config.yml or better yet this would be enabled in each bundles DependencyInjection/AppBundleExtension.php.
2) Routing
Use for route config, but each bundle still needs including within app/config/routing.yml leaving us with a pretty large unmanageable file.
Some examples
app:
resource: "#AppBundle/Controller"
type: annotation
blog:
resource: "#BlogBundle/Controller"
type: annotation
content:
resource: "#ContentBundle/Controller"
type: annotation
oneup_uploader:
resource: .
type: uploader
3) OneUp Uploader
This is my biggest biting point, this configuration is over 500 lines long currently between the 20+ bundles we use. Here are some examples pulled from app/config/uploader.yml which is imported from config.yml. If I could somehow get these into each bundles config I'd be very happy!
# Uploader
oneup_uploader:
mappings:
bundle_name_reference1:
frontend: blueimp
storage:
type: gaufrette
filesystem: gaufrette.local_filesystem
bundle_name_reference2:
frontend: blueimp
storage:
type: gaufrette
filesystem: gaufrette.local_filesystem
My thoughts are by getting all these configs into each individual bundle, I can enable/disable them really easily by just updating AppKernel.php. I'm assuming all of this is possible as Symfony is incredibly flexible, I'm just a little lost when it comes to these lower level alterations.
You could simply just add
oneup_uploader:
#...
to src/Vendor/WhateverBundle/Resources/config/services.yml
OR
There is a cookbook entry on how to decouple bundles from app/config:
How to Load Service Configuration inside a Bundle
I think the ideal, low-coupling, solution would be to create a bundle extending the other bundle (e.g. MyOneUpUploaderBundle) and using the cookbook approach there.

Setting up orm configuration in symfony2 to be similar to Sylius

I'm having a tough time setting up doctrine ORM configuration using Symfony2. I'm setting up the ORM files using yml.
I've looked at a couple of open source Symfony2 projects(mostly Sylius) and the norm seems to be placing Entities outsides of the bundles in a folder called Components but placing the ORM definitions in the bundles. Can anyone suggest any useful resources to help me set this up? The default seems to require my entities to be within bundles which I don't want to do because some of my entities have scope across multiple bundles.
I've noticed in the documentation you can configure entity managers individually but I can't see how the configuration options below can achieve what I'm after, especially because the entities may not all share the same prefix:
mappings:
AcmeOrderBundle:
type: ~
dir: ~
alias: ~
prefix: ~
is_bundle: ~
Any help with this would be greatly appreciated and I can provide more information if necessary.
Here is an example using a couple of the options:
entity_managers:
games:
connection: games
mappings:
CeradGameBundle:
type: yml
dir: Resources/config/doctrine2
prefix: Cerad\Bundle\GameBundle\Doctrine\Entity
The dir points to where the orm.yml files live. As shown it is relative to the bundle. But you can replace it with an absolute path if you want.
The prefix is basically the namespace of your entities.
As far as multiple prefixes go, you can have multiple mappings for a given manager. Make one mapping per prefix.

How to register a bridge in symfony2?

I was trying to setup metabor/statemachine-doctrine-bridge within a symfony2 project. I am used to register new bundles, yet I have no clue how I would get symfony2 to find the bridge.
I tried including it via use statement but it seems to be not enough resulting in the error:
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'Metabor\Bridge\Doctrine\Statemachine\State' was not found in the chain configured namespaces Hn\AssetDbBundle\Entity, Hn\UserBundle\Entity, FOS\UserBun
dle\Entity, FOS\UserBundle\Model
Only bundles can be registered. Bridges are just library with a specific task: making a 3rd party library ready to be implemented by a Bundle in the Symfony2 framework. Usually, this starts by creating a bundle which implements a library into the Symfony2 framework, then all reusable stuff which isn't specifically tied to the framework are extracted from the bundle and put in a bridge, so it also can be used on places where bundles are not used (e.g. Silex).
In your case it should work, if you correctly configured the autoloading. You never have to register a library to a framework in order to make PHP able to autoload it, that are 2 different things.
One needs to both configure the autoloader and point doctrine to the correct entities.
app/config.yml:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
mappings:
MetaborBridge:
type: annotation
dir: %kernel.root_dir%/../vendor/metabor/statemachine-doctrine-bridge/src/Metabor
prefix: Metabor
is_bundle: false
composer.json:
...
"autoload": {
"psr-0": {
"": "src/",
"Metabor": "vendor/metabor/statemachine-doctrine-bridge/src/Metabor/",
"MetaborStd": "vendor/metabor/metabor-std/src/MetaborStd/"
}
},
...

Categories