# Making sources

It can be possible that you need to create your own source because what you need is not provided by Phecks.

Lucily, it's very easy to make them.

If you follow the recommended setup, you can allocate your Sources in `./phecks/Sources`.

## New Source

To make a source, make sure to implement the Source interface:

```php
use Juampi92\Phecks\Domain\Contracts\Source;
```

It will make you implement the `run` method.

Also, you'll have to define the type of items this Source will return. You do that by using `@implements Source<YourMatchType>`

```php
/**
 * @implements Source<FileMatch>
 */
class FileSource implements Source
{
    private ?string $dir = null;

    public function __construct(
        private readonly Filesystem $filesystem
    ) {}

    public function directory(string $dir): self
    {
        $this->dir = $dir;

        return $this;
    }

    /**
     * @return MatchCollection<FileMatch>
     */
    public function run(): MatchCollection
    {
        if (!$this->dir) {
            throw new RuntimeException('Please specify a directory using directory(string)');
        }

        /** @var array<SplFileInfo> */
        $files = $this->filesystem->allFiles($this->dir);

        return MatchCollection::fromFiles(
            collect($files)
                ->map(fn (SplFileInfo $fileInfo): FileMatch => new FileMatch($fileInfo->getPathname()))
                ->all(),
        );
    }
}

```

You can use `MatchCollection@fromFiles` if you know you will feed an array of `FileMatch` .


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://juampi92.gitbook.io/phecks/advanced/making-sources.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
