Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use imported class in task from remote import #403

Merged
merged 1 commit into from
Apr 11, 2024
Merged

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Apr 10, 2024

Note: Due to the nature of PHP (cannot re-require a file), on the very first run, conditionned task are not loaded.

usage:

<?php
# castor.php
use Doctrine\Common\Collections\Selectable;

use function Castor\import;

import('composer://doctrine/collections', version: '^2.0');

if (interface_exists(Selectable::class)) {
    import(__DIR__.'/test.php');
}
<?php
#test.php
use Castor\Attribute\AsTask;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;

use function Castor\io;

#[AsTask(description: 'Use classes that use imported from remote packages')]
function collection(): void
{
    $collection = new ArrayCollection([1, 2, 3, 4, 5]);

    io()->title('Collection Items');
    foreach ($collection as $item) {
        io()->writeln($item);
    }

    io()->title('Collection Items (matching)');
    $collection = (new TestSelectable())->matching(Criteria::create());

    var_dump($collection->toArray());
}

class TestSelectable implements Selectable
{
    public function matching(Criteria $criteria): ArrayCollection
    {
        return new ArrayCollection(['a', 'b', 'c']);
    }
}

cc @TheoD02

$autoloadPath = PathHelper::getRoot() . Composer::VENDOR_DIR . 'autoload.php';

if (!file_exists($autoloadPath)) {
// FIXME: really needed?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @pyrech

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was one way to easily detect if somebody manually removed their .castor/vendor (in case of troubles or to force update). It may no longer be needed, i don't have the current implementation in mind

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I can drop it because we stored the installed version file in the vendor thanks

@lyrixx lyrixx merged commit b2d5dc9 into main Apr 11, 2024
9 checks passed
@lyrixx lyrixx deleted the autoload branch April 11, 2024 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants