Giter VIP home page Giter VIP logo

Comments (7)

Ne-Lexa avatar Ne-Lexa commented on May 31, 2024 3

I wrote a bundle for Symfony.
Now you can plug it in through the composer.
https://github.com/Ne-Lexa/roach-php-bundle

from core.

ksassnowski avatar ksassnowski commented on May 31, 2024 1

That looks really nice. Good job! I’ll add it to the docs shortly 👍

from core.

alejgarciarodriguez avatar alejgarciarodriguez commented on May 31, 2024

Ok I didn't read all the doc lol. Here is the answer

https://roach-php.dev/docs/dependency-injection/#swapping-out-the-container

We can close this :)

from core.

alejgarciarodriguez avatar alejgarciarodriguez commented on May 31, 2024

Ok so now the problem is, its not working because Roach now uses Symfony container even for loading their own classes and they are not loaded by Symfony since they're in vendor directory,

You have requested a non-existent service "RoachPHP\Core\Engine".

I think we need a bundle for this then

from core.

ksassnowski avatar ksassnowski commented on May 31, 2024

Yeah, that’s essentially what the framework integrations would have to do (that’s what the Laravel package does). Mostly providing a bunch of container bindings. I do want to provide a first-party Symfony package eventually, but I’m not super familiar with the Symfony ecosystem and I’ve never written a bundle before, so it’s been going a bit slow.

from core.

alejgarciarodriguez avatar alejgarciarodriguez commented on May 31, 2024

I could make it work by creating a bundle, but now I found some issues... services must be public since Roach library are getting them directly via container ($container->get()). Even the services in the userland (MySpider in the examples)

So I need to create some tags:

# RoachPHP\Bundle\DependencyInjection
class RoachPHPExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        // bindings go here

        $container->registerForAutoconfiguration(SpiderInterface::class)
            ->addTag('roach.spider');

        $container->registerForAutoconfiguration(ItemProcessorInterface::class)
            ->addTag('roach.item_processor');

        // extensions middlewares etc
    }
}

And a compiler pass:

// RoachPHP\Bundle\DependencyInjection\Compiler
class RoachCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
         // does the trick and inject the container even if its a static method
         $container
            ->getDefinition(Roach::class)
            ->addMethodCall('useContainer', [new Reference('service_container')])
            ->setPublic(true);

        foreach ($container->findTaggedServiceIds('roach.spider') as $id => $tags) {
            $container->getDefinition($id)->setPublic(true);
        }

        foreach ($container->findTaggedServiceIds('roach.item_processor') as $id => $tags) {
            $container->getDefinition($id)->setPublic(true);
        }

        foreach ($container->findTaggedServiceIds('roach.extension') as $id => $tags) {
            $container->getDefinition($id)->setPublic(true);
        }
    }
}

Not sure if its a good practice to do that in a symfony bundle, probably not

This is my symfony console command:

class AppCommand extends Command
{
    public function __construct(private Roach $roach)
    {
        parent::__construct();
    }
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        Roach::startSpider(MySpider::class);
        return Command::SUCCESS;
    }
}

Or creating a Roach service in the bundle, as a wrapper for the actual Roach static class and do something like $roach->startSpider(MySpider::class)

from core.

ksassnowski avatar ksassnowski commented on May 31, 2024

I added a link to the repo to the docs. I assume the bundle fixes this issue so I'll close it for now.

from core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.